How to produce a continuous sound?

30 views (last 30 days)
I need to produce a continuous sound output composed by a finite number of periodic samples but endlessly and without gaps or glitches. Is it possible to make in MATLAB running under Windows? Assume that the reproduction time makes it impossible to construct an array long enough to pass it to the sound generation routines.
Thanks in advanced.
Joe Ercolino

Accepted Answer

Tucker McClure
Tucker McClure on 28 Apr 2012
Hi Jose and Daniel,
In MATLAB r2012a with DSP System Toolbox, there's some new stuff to enable exactly what you're talking about.
First, the DSP System Toolbox can use ASIO, which as Daniel describes is a low-latency audio streaming format used for professional audio on regular old Windows machines. You can set the toolbox to use this standard in File > Preferences > DSP System Toolbox (select "ASIO"). This requires an ASIO driver for your soundcard. Fortunately, there's a free one called ASIO4All (www.asio4all.com) that works about as well as my professional interfaces.
Second, the dsp.AudioPlayer object is meant to stream audio out, and the documentation gives good examples of using it. You can set the buffer fairly low. I've used only 128 samples with a 48kHz sampling rate (2.7ms latency, well within professional standards).
Using these methods (and using dsp.AudioRecorder), I've managed to make entire real-time MATLAB synthesizers and effect processors with MIDI input! Pretty fun. (I'll post the MIDI input code and a framework I use for the audio processors here as soon as I think it's smooth on other peoples' machines.) So anyway, yes, this kind of work is definitely possible.
Hope that helps!
- Tucker
  3 Comments
Tucker McClure
Tucker McClure on 30 Aug 2012
Edited: Tucker McClure on 30 Aug 2012
Hi Daniel,
Sorry I didn't see this until now! The DSP System Toolbox does not require Simulink. It works fine reading and writing audio data to/from a sound card from MATLAB directly. I also comes with Simulink blocks, so you can add those to a Simulink model as audio inputs and outputs.
So, you don't need to do anything fancy to replace "sound". Here's a quick example of using dsp.AudioPlayer to output frames of sound in a loop. It's also using dsp.AudioFileReader to acquire the sound in frames.
hafr = dsp.AudioFileReader(my_file_name);
hap = dsp.AudioPlayer('SampleRate',48000);
while ~isDone(hafr)
audio = step(hafr);
step(hap,audio);
end
pause(hap.QueueDuration); % Wait until audio plays to the end
release(hafr); % close input file, release resources
release(hap); % close audio output device, release resources
I was also asked recently if dsp.AudioPlayer and dsp.AudioRecorder can work with different data simultaneously. The answer is yes. The AudioRecorder might be reading in your live data while the AudioPlayer is playing back your favorite football game. They are completely separate except for the fact that most sound cards require their input and output samplerate to be the same.
Hope that helps!
Tucker McClure
Tucker McClure on 5 Sep 2012
As a quick follow-up, here's the (soft) real-time audio processor code I put together on File Exchange. It's meant specifically to take audio inputs through processing of whatever kind and over to outputs on a Windows machine in real time with very low latency.

Sign in to comment.

More Answers (2)

Daniel Shub
Daniel Shub on 12 Dec 2011
Continuous sound is difficult for three reasons: you must continuously generate the sound samples, then push those samples to the sound card driver, and then the sound card hardware needs to fetch them.
Assuming you can generate your sound samples faster than real-time and have enough memory to buffer say 10s worth of sound, then even if the computer does something (like defragment your hard drive or scan for viruses), you should be okay. The more you buffer, the safer you are.
Transferring the data from the soundcard driver to the hardware can be problematic and is out of your control. USB and firewire devices have limited bandwidth. High sample rates and high channel counts can tax the system and result in dropped frames. You cannot guarantee that frames will not get dropped without specialized hardware. Under Microsoft Windows, ASIO cards can let you know if a frame gets dropped. If your computer is not doing anything special, your sample rate is 44.1 kHz, and you have 2 channel audio, then you will not drop very many frames. When your computer scans for viruses, you will drop more frames. You will also drop more frames if generating the sounds is time consuming.
You can more or less continuously stream a sound (up to about 2^32 samples) with the right interface. Neither sound nor audioplayer let you do this. You can use the winsound device in the Data Acquisition Toolbox. Even better is to use a port audio sound interface. I prefer the one with psychtoolbox, but pawavplay and playrecord also work.

Walter Roberson
Walter Roberson on 12 Dec 2011
It is not possible in user mode under Windows 7. Windows is not designed for real-time work, and in user mode you never know when the kernel is going to interrupt you or for how long. If Windows decides that (say) it is a good time to defragment your disc, then there isn't anything you can do about that in user mode.

Products

Community Treasure Hunt

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

Start Hunting!