How do I play a sound in Octave?

2019-02-06 15:52发布

Octave appears to assume that a specific sound playing utility will be available on a system but doesn't seem to provide the ability to specify an alternate. In the error below, Octave is looking for ofsndplay, which is not a utility available on all systems.

octave:38> sound(beamformed_20)

sh: ofsndplay: command not found

Is there an Octave configuration setting or code fragment that I can use to specify an system-appropriate utility?

7条回答
等我变得足够好
2楼-- · 2019-02-06 16:05

I've overridden the playaudio function from octave with the following function. This will work only after installing sox.

sudo apt-get install sox

(in ubuntu)

function [ ] = playaudio (x, sampling_rate)

    if nargin == 1
        sampling_rate = 8000
    end
    file = tmpnam ();
    file= [file, '.wav'];
    wavwrite(x, sampling_rate, file);
    ['play ' file ]
    system(['play ' file ]);
    system(['rm ' file]);
end

A similar approach will allow you to record too:

% Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003, 2004, 2005,
%               2006, 2007 John W. Eaton
%
% This file is part of Octave.
%
% Octave is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 3 of the License, or (at
% your option) any later version.
%
% Octave is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Octave; see the file COPYING.  If not, see
% <http://www.gnu.org/licenses/>.

% -*- texinfo -*-
% @deftypefn {Function File} {} record (@var{sec}, @var{sampling_rate})
% Records @var{sec} seconds of audio input into the vector @var{x}.  The
% default value for @var{sampling_rate} is 8000 samples per second, or
% 8kHz.  The program waits until the user types @key{RET} and then
% immediately starts to record.
% @seealso{lin2mu, mu2lin, loadaudio, saveaudio, playaudio, setaudio}
% @end deftypefn

% Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
% Created: 19 September 1994
% Adapted-By: jwe
% And adapted again 11/25/2010 by Rob Frohne    
function X = record (sec, sampling_rate)


  if (nargin == 1)
    sampling_rate = 8000;
  elseif (nargin != 2)
    print_usage ();
  endif

  file = tmpnam ();
  file= [file,".wav"];

  input ("Please hit ENTER and speak afterwards!\n", 1);

  cmd = sprintf ("rec -c1 -r%d %s trim 0 %d",
                   sampling_rate, file, sec)

  system (cmd);

  X = wavread(file);

end
查看更多
迷人小祖宗
3楼-- · 2019-02-06 16:07

Install alsa-utils or pulseaudio-utils and put the following in your ~/.octaverc:

global sound_play_utility = 'aplay';

or

global sound_play_utility = 'paplay';
查看更多
够拽才男人
4楼-- · 2019-02-06 16:08

I am on a Mac (Yosemite), and discovered a simpler solution than what others have suggested. Just in case this is still relevant for anybody:

First install SoX: http://sox.sourceforge.net/

(via Homebrew)

brew install sox

Now on the terminal command line you can use:

play “/path/to/sound file.wav"

...and you will hear beautiful music.

But that command does not work from within Octave. This does work:

system(‘play “/path/to/sound file.wav”’);
查看更多
We Are One
5楼-- · 2019-02-06 16:14

On Octave 4.2.1. You can play a wav file as follows

Save the following code in a file playWav.m

function playWav(inputFilePath)
  [y, fs] = audioread(inputFilePath);
  player  = audioplayer(y, fs);
  playblocking(player)
end

Then you can call the function as playWav('/path/to/wavfile'); from Octave commandline.

Tested on Windows 7.

查看更多
Ridiculous、
6楼-- · 2019-02-06 16:17

On one of my Linux machines, I created the following ofsndplay script to work around the hard-wired dependency:

$ cat /usr/bin/ofsndplay

#!/bin/sh
## Coping with stupid dependency on ofsndplay in octave
play -t au -

This particular script uses the SoX play utility.

Admittedly, the comment is unnecessary for the functionality but it certainly made me feel better....

查看更多
一纸荒年 Trace。
7楼-- · 2019-02-06 16:18

On OSX, this is what I did to get sound working:

from the sound command help:

This function writes the audio data through a pipe to the program "play" from the sox distribution. sox runs pretty much anywhere, but it only has audio drivers for OSS (primarily linux and freebsd) and SunOS. In case your local machine is not one of these, write a shell script such as ~/bin/octaveplay, substituting AUDIO_UTILITY with whatever audio utility you happen to have on your system: #!/bin/sh cat > ~/.octave_play.au SYSTEM_AUDIO_UTILITY ~/.octave_play.au rm -f ~/.octave_play.au and set the global variable (e.g., in .octaverc) global sound_play_utility="~/bin/octaveplay";

I named the following script "octaveplay" and put it in ~/bin:

cat > ~/.octave_play.aif
afplay ~/.octave_play.aif
rm -f ~/.octave_play.aif

Then I created .octaverc and added: global sound_play_utility="~/bin/octaveplay";

Voila!

查看更多
登录 后发表回答