Problems with Generating Continous(Un Broken) Sound on Matlab
4 views (last 30 days)
Show older comments
Machine: Windows 64bit, MATLABR2015a
SoundCard:IDT HD Audio Codec
code snippets of my attempt
%length
T=1;
%Sampling Freq
Fs = 48000;
%samples
N = T*Fs;
%samples
t = 0 : 1/Fs : T;
%Wanted Frequency [Hz]
Fn = 800;
%Signal to Generate
y = sin(Fn*2*pi*t);
%Play sound
while 1
sound(y,Fs);
end
i also tried to do but this also did not help
Obj1 = audioplayer(y,Fs);
Obj2 = audioplayer(y,Fs);
while 1
playblocking(Obj1);
playblocking(Obj2);
end
what i observe is the wave being not that continuous the first approach was quite better when compared to second, any suggestions to generate continuous sound would help in achieving my objective
i also find a similar problem statement here also but did not end in any constructive solutions, where the accepted answer says to use dsp.AudioPlayer
hafr1 = dsp.AudioFileReader('filename.wav');
hap1 = dsp.AudioPlayer('SampleRate',48000);
while 1
if ~isDone(hafr1)
audio = step(hafr1)
step(hap,audio);
end
Why Matlab is not providing a feature to generate sound continous, if any does have a hack kindly suggest
0 Comments
Answers (3)
Walter Roberson
on 22 Jul 2015
Using dsp.audioplayer is a constructive solution.
Mathworks is not required to put less-used functionality into the main MATLAB product. You have a specialized requirement, so you can either obtain the Mathworks Toolbox, or you can build the interfaces yourself using mex and loadlibrary() and the like.
I posted one of the Answers there, in which I noted that continuous sound is not possible in user mode in MS Windows. Daniel posted a longer Answer giving more details of the limitations; notice that he too indicated that frames may be dropped depending on system load. That is an operating system and hardware difficulty: in order to be able to guarantee that frames will not be dropped, the operating system needs to provide a real-time mode. MATLAB itself is not designed for real-time work, but you can use Simulink and MATLAB Coder and various toolboxes to generate code for real-time operating systems that you can run on a different real-time device.
Possibly you are unaware that modern MS Window is based upon the technologies of Windows NT. Prior to Windows NT, MS DOS and MS Windows were single user, permitted direct access to hardware, and releases were highly dependent on the system being run on. Windows NT redesigned so that the higher levels are more hardware independent and the higher levels call into a micro-kernel that has the hardware dependent portions. Direct access to the hardware ports is restricted by design -- and part of the design is for built-in security. You do not, for example, want random user programs to be able to turn on the microphone and record everything around: you want there to be a protocol for who can turn on the microphone and what they can do with it.
Restrictions on direct access to the hardware has been designed into Windows since 1993 (more than 20 years), so MATLAB can't just grab the audio port and start outputting data to it. MATLAB has to follow the access mechanisms provided by MS Windows. At present, the fastest such access for user-mode programs is provided by using ASIO drivers, but ASIO cannot promise continuous sound for user-mode programs. If you need continuous sound then you are using the wrong operating system and the wrong software.
Daniel has done a bunch of testing with the ASIO drivers. My recollection is that the best he has been able to achieve is 8 ms latency under peak conditions. At 44100 samples per second, that is over 3500 samples.
0 Comments
Cliff Bradshaw
on 22 Jul 2015
There is a set of four .m files called "MATLAB JukeBox" that allows users to play songs by typing "JukeBox()" into the console. If you look at the file for the Star Wars Imperial March, there's code for continuous sound!!!
Check out the program on GitHub:
Hope this helps a lot!
0 Comments
Dinesh Iyer
on 22 Jul 2015
Shyam,
I am not sure what you mean by continuous unbroken sound. The audioplayer and sound functions have a limitation that you have to queue in all the data you have to playback and once the playback starts, there is little you can do.
The dsp.AudioPlayer however can play audio till you want it to. Consider the following psuedo code
hap = dsp.AudioPlayer;
while true
if ~isAudioAvailable()
break;
end
data = someFuncThatGeneratesAudioSamples;
step(hap, audio);
end
If you are referring to glitches in the audio output, that could be related to the latency settings that are controlled by the BufferSize, QueueDuration and FrameSize properties of the system object.
Hope this helps.
Dinesh
3 Comments
Walter Roberson
on 22 Jul 2015
You can put the dsp code in a loop that resets to the beginning of the source and then plays it, until you terminate.
Dinesh Iyer
on 22 Jul 2015
Shyam,
The dsp.AudioFileReader system object has a property called PlayCount that you can set to Inf to read data from the file forever. This will avoid any startup delay that you are experiencing.
Dinesh
See Also
Categories
Find more on Measurements and Spatial Audio in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!