Why isn't all of preload data sent to sound card being played when using Data Acquisition Toolbox

6 views (last 30 days)
I am trying to migrate my code from the old DataAcquisiton interface (Pre R2016) to the one used in R2021. I am a little confused in the terminology becase I can't find the definition of a "scan", however as best as I can tell a Scan is a Sample. In the following code, I generate 50000 noise samples and try to play them through the sound card. Here is my code example:
Noise = 2*rand(50000,1)-1; % generate 50,000 random noise samples between -1 and 1
d = daq("directsound"); % Create DAQ object
ch1 = addoutput(d,"Audio3","1","Audio");
d.Rate = 44100; % set samplerate to 44100 Hz
preload(d,Noise); % Preload output buffer with sample
d % look at d structure
start(d) % start audio playback
d
When I run this code, from the console I can see that the NumScansQueued is 50000. This is in agreement with the size of the data array that I sent to output using the preload command. But, after the start(d) command executes, I hear a short burst of noise, less than 1 second, and it appears that only 5120 samples were sent (see the NumScansOutputByHardware)
>> DAQ_Test
---- DataAcquisition Object d before Start
d =
DataAcquisition using DirectSound hardware:
Running: 0
Rate: 44100
NumScansAvailable: 0
NumScansAcquired: 0
NumScansQueued: 50000
NumScansOutputByHardware: 0
RateLimit: [8000 192000]
Show channels
Show properties and methods
Error occurred during audio output: Device 'Audio3' dropped 1024 samples.
Please verify that enough data is being enqueued in the callback for the 'DataRequired' listener.
---- DataAcquisition Object d After completion
d =
DataAcquisition using DirectSound hardware:
Running: 0
Rate: 44100
NumScansAvailable: 0
NumScansAcquired: 0
NumScansQueued: 0
NumScansOutputByHardware: 5120
RateLimit: [8000 192000]
Show channels
Show properties and methods
What's even more confusing is when I rerun this code with Rate = 8000. Now I get the full duriation of 6.2 seconds of playback, but the numbers presented after the second query of the object suggest only 15104 samples were sent to the sound card for playback and 34896 are still sitting in the buffer waiting to be played.don't make sens to me.
>> DAQ_Test
---- DataAcquisition Object d before Start
d =
DataAcquisition using DirectSound hardware:
Running: 0
Rate: 8000
NumScansAvailable: 0
NumScansAcquired: 0
NumScansQueued: 50000
NumScansOutputByHardware: 0
RateLimit: [8000 192000]
Show channels
Show properties and methods
---- DataAcquisition Object d After completion
d =
DataAcquisition using DirectSound hardware:
Running: 1
Rate: 8000
NumScansAvailable: 0
NumScansAcquired: 0
NumScansQueued: 34896
NumScansOutputByHardware: 15104
RateLimit: [8000 192000]
Show channels
Show properties and methods
Can someone please explain whats happening. I would like to simply be able to send data to the sound card and have it all play each time I send it. I have not been able to find any limitations on the size of the buffer (how much data I can send at any given time.) With the older DAQ interface, I used to beable to manage the data buffer sitting between my code and the sound card.
Thanks in advance

Answers (1)

Karan Singh
Karan Singh on 8 Nov 2023
Hi,
The preload function is used to load data into the output buffer of the DataAcquisition object before starting it. However, the "start function begins the operation of the DataAcquisition object and immediately returns control to the command line. This means that the start function does not wait for the operation to complete before returning.
In your case, it seems that the audio playback starts but does not have time to finish before the start function returns and the script ends. When the script ends, MATLAB may be clearing the DataAcquisition object and stopping the audio playback before it has time to finish.
One way to ensure that the audio playback finishes is to use the wait function after the start function. The wait function blocks the MATLAB command line until the operation of the DataAcquisition object completes.
Here is how you could modify your code:
Noise = 2*rand(50000,1)-1; % generate 50,000 random noise samples between -1 and 1
d = daq("directsound"); % Create DAQ object
ch1 = addoutput(d,"Audio3","1","Audio");
d.Rate = 44100; % set samplerate to 44100 Hz
preload(d,Noise); % Preload output buffer with sample
d % look at d structure
start(d) % start audio playback
wait(d) % wait for audio playback to finish
d
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati

Categories

Find more on Multichannel Audio Input and Output in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!