I'd like to use the audioDeviceWriter object directly from a workspace variable instead of using an audioFileReader. At first, it appears that this should be possible using a regular frame loop, but because the audio is buffered in, the loop ends before playback has finished and the device is released. Typically, the "isDone" function is used with an audioFileReader to finish playback, but in this case, I can't seem to find a property that I can check to manually determine if playback has stopped. Additionally, I don't want to assume that beginning a timer before the frame loop begins and stopping after a given amount of time will work given the variance in time to acquire the device and begin playback. Ideally, this method would also work for an audioPlayerRecorder to use workspace variables for output and input without having to write to files.
% Note the use of a workspace variable instead of AudioFileReader
load handeldeviceWriter = audioDeviceWriter('SampleRate',Fs);framesize = deviceWriter.BufferSize;nframes = ceil(length(y)/framesize);for i = 1:nframes begSamp = 1 + framesize*(i-1); endSamp = framesize*i; if endSamp > length(y), endSamp = length(y); end % Avoid outOfBounds error
playFrame = y(begSamp:endSamp); if length(playFrame) < framesize % End of workspace variable
playFrame(framesize) = 0; % Pad with zeros to match the frame size
pause % Must pause in order to wait for the audio to finish
% Ideally, could instead use a function checkIfDone(deviceWriter) that returns when playback is complete
break end deviceWriter(playFrame);endrelease(deviceWriter);
If that isn't possible with the current architecture, that would also be useful to know. Thanks in advance!
Best Answer