Clear Filters
Clear Filters

How to stop playback with soundsc() in MATLAB?

30 views (last 30 days)
I am currently working on an equalizer, and I want playback to stop at the click of a button.I have experimented with the audiorecorder method, but unlike soundsc, audiorecorder objects do not play at the maximum possible amplitude (without distortion).How can I either stop playback initiated from a soundsc() command or use audiorecorder in a way similar to soundsc?

Answers (2)

Walter Roberson
Walter Roberson on 16 May 2016
soundsc() cannot be stopped.
To play a sound at the maximum possible amplitude like soundsc does, you should use
centered_x = x - repmat(mean(x), size(x,1), 1);
maxx = max(abs(centered_x(:));
scaled_centered_x = centered_x ./ maxx;
Now you can use audioplayer's play() method on scaled_centered_x .
Note that when you are building an equalizer, you do not want your sound to be continually readjusting its volume. Suppose, for example that one of the performers is tuning their guitar: you do not want the tuning plucks to be automatically full volume. You should only be doing this kind of automatic scaling on completed sounds that have the entire dynamic range represented.
  2 Comments
Matlab Student
Matlab Student on 17 May 2016
Works like a charm! Could you please explain how this works? Especially the
centered_x = x - repmat(mean(x), size(x,1), 1);
Walter Roberson
Walter Roberson on 18 May 2016
That code is just subtracting off the mean to center around 0, to match the behaviour of soundsc which does that. It is complicated because I did not assume that there is only one channel. Another approach would have been
centered_x = bsxfun(@minus, x, mean(x));

Sign in to comment.


Image Analyst
Image Analyst on 18 May 2016
Don't use soundssc(). To it this way:
player = audioplayer(yShort, Fs);
play(player);
The, to stop it:
stop(player);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!