Patch is attached.
I also noticed that the logic for checking the global
sound_play_utility variable in sound.m wasn't working for me.
The existing code uses the 'exist' function to see if the users has
already set sound_play_utility:
function sound()
## ...
if (exist("sound_play_utility", "var"))
## The user is always right, so just declare the set variable as global
global sound_play_utility;
else ## check path for known audio players...
exist always returns false for me inside the function, regardless of
whether I declare sound_play_utility global in my octaverc or
interpreter. After reading the docs on global variables, I think it
should be:
function sound()
## ...
global sound_play_utility;
if ~isempty(sound_play_utility),
## User has defined a play command
else ## check path for known audio players ...
--Roman
Index: sound.m
===================================================================
--- sound.m (revision 5480)
+++ sound.m (working copy)
@@ -13,11 +13,13 @@
## You should have received a copy of the GNU General Public License
## along with this program; If not, see <http://www.gnu.org/licenses/>.
-## usage: sound(x [, fs])
+## usage: sound(x [, fs, bs])
##
## Play the signal through the speakers. Data is a matrix with
## one column per channel. Rate fs defaults to 8000 Hz. The signal
-## is clipped to [-1, 1].
+## is clipped to [-1, 1]. Buffer size bs controls how many audio samples
+## are clipped and buffered before sending them to the audio player. bs
+## defaults to fs, which is equivalent to 1 second of audio.
##
## Note that if $DISPLAY != $HOSTNAME:n then a remote shell is opened
## to the host specified in $HOSTNAME to play the audio. See manual
@@ -55,12 +57,20 @@
## as your sound_play_utility. Things you may want to do are resample
## so that the rate is appropriate for your machine and convert the data
## to mulaw and output as bytes.
-function sound(data, rate)
+##
+## If you experience buffer underruns while playing audio data, the bs
+## buffer size parameter can be increased to tradeoff interactivity
+## for smoother playback. If bs=Inf, then all the data is clipped and
+## buffered before sending it to the audio player pipe. By default, 1
+## sec of audio is buffered.
- if nargin<1 || nargin>2
- usage("sound(x [, fs])");
+function sound(data, rate, buffer_size)
+
+ if nargin<1 || nargin>3
+ usage("sound(x [, fs, bs])");
endif
if nargin<2 || isempty(rate), rate = 8000; endif
+ if nargin<3 || isempty(buffer_size), buffer_size = rate; endif
if rows(data) != length(data), data=data'; endif
[samples, channels] = size(data);
@@ -114,7 +124,19 @@
fwrite(fid, 3, 'int32', 0, 'ieee-be');
fwrite(fid, rate, 'int32', 0, 'ieee-be');
fwrite(fid, channels, 'int32', 0, 'ieee-be');
- fwrite(fid, 32767*clip(data,[-1, 1])', 'int16', 0, 'ieee-be');
+
+ if isinf(buffer_size),
+ fwrite(fid, 32767*clip(data,[-1, 1])', 'int16', 0, 'ieee-be');
+ else
+ ## write data in blocks rather than all at once
+ nblocks = ceil(samples/buffer_size);
+ block_start = 1;
+ for i=1:nblocks,
+ block_end = min(size(data,1), block_start+buffer_size-1);
+ fwrite(fid, 32767*clip(data(block_start:block_end,:),[-1, 1])', 'int16', 0, 'ieee-be');
+ block_start = block_end + 1;
+ end
+ endif
pclose(fid);
endif
end
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Octave-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/octave-dev