How to Play a Sound in Octave

How do I play a sound in Octave?

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....

Using soundsc in Octave, on Windows 7

The file you linked to includes documentation at the top, which includes the following:

## This function writes the audio data through a pipe to the program
## "play" from the sox distribution.

So you need to install sox, for starters. This will include play.exe. Then you have to either set the value of the global variable sound_play_utility in Octave to reflect where you installed sox, or make sure the file_in_path(EXEC_PATH) stuff can find play.exe. Looking a few lines down from the snippet you posted, it looks like the file passes that variable to popen, which is supposed to launch a new process. I've never done anything like that on Octave on a Windows machine, so I can't help you on the exact details.

How can I wait for an (audio play) command to finish before executing the next one, in Octave?

You could do a busy loop on the play status:

while strcmpi(player.Running, 'on')
pause(.1);
endwhile

The pause isn't required, or could be doing something else instead - I just use in the example to limit the CPU usage.

Or

while isplaying(player)
pause(.1);
endwhile

You could also use playblocking(player) instead


From here:

 33.3.1 Playback

The following methods are used to control player playback.
...
playblocking (player)
playblocking (player, start)
playblocking (player, limits)

Play audio stored in the audioplayer object player with blocking.

Given optional argument start, begin playing at start samples in
the recording. Given a two-element vector limits, begin and end
playing at the number of samples specified by the elements of the
vector.

isplaying (player)

Return true if the audioplayer object player is currently playing
back audio and false otherwise.


Related Topics



Leave a reply



Submit