MATLAB Answers

How to resolve "Invalid sample rate" error when using audioPlayerRecorder

183 views (last 30 days)
Ed Smith
Ed Smith on 26 Oct 2020
Answered: Saurav Chaudhary on 30 Oct 2020
I want to play & record synchronously but this code fails.
Perhaps the error is caused because a driver is not installed or Windows device parameters are not set appropriately.
Forgive the longish progam; I'm not sure how much context is needed for a specific diagnosis.
% PR_trouble.m Matlab 2019b(9.9.0.1467703) 3.6GHz i9 Dell Win10 PC
clear; close all force; clc;
% Make & verify a wav file
file_name = 'soundfile.wav';
fullfile_name = fullfile ( pwd, file_name );
fs = 44100;
tone = sin ( 2 * pi * 1000 * (1:51200)/fs );
audiowrite ( fullfile_name, tone, fs );
wav_file_info = audioinfo ( fullfile_name )
% Make AudioFileReader, AudioFileWriter & audioPlayerRecorder
my_Reader = dsp.AudioFileReader ...
( fullfile_name, 'SamplesPerFrame',512 );
fs = my_Reader.SampleRate
my_Writer = dsp.AudioFileWriter ...
( fullfile(pwd,'output.wav'), 'SampleRate',fs )
available_playRecs = audioPlayerRecorder
my_aPR = audioPlayerRecorder ( 'SampleRate', fs );
devices = getAudioDevices ( my_aPR )
% Play & record
while ~ isDone ( my_Reader )
audioToPlay = my_Reader ();
audioRecorded = my_aPR ( audioToPlay ); %%% ERROR HAPPENS HERE -- FIRST EXECUTION
my_Writer ( audioRecorded );
end
wav_file_info =
struct with fields:
Filename: 'C:\Users\psycuser\Desktop\MatlabPrograms\native_egg_sound\soundfile.wav'
CompressionMethod: 'Uncompressed'
NumChannels: 1
SampleRate: 44100
TotalSamples: 51200
Duration: 1.160997732426304
BitsPerSample: 16
fs =
44100
my_Writer =
dsp.AudioFileWriter with properties:
Filename: 'C:\Users\psycuser\Desktop\MatlabPrograms\native_egg_sound\output.wav'
FileFormat: 'WAV'
SampleRate: 44100
Compressor: 'None (uncompressed)'
DataType: 'int16'
available_playRecs =
audioPlayerRecorder with properties:
Device: 'Default'
SampleRate: 44100
devices =
1×2 cell array
{'Default'} {'Realtek ASIO'}
Error using audioDeviceReader/setup
PortAudio Error: Unanticipated host error
Error using audioDeviceReader/setup
PortAudio Error: Invalid sample rate
Error in audioDeviceReader/setupImpl
Error in audioPlayerRecorder/setupImpl
Error in PR_trouble (line 31)
audioRecorded = my_aPR ( audioToPlay ); - Show complete stack trace

  0 Comments

Sign in to comment.

Answers (1)

Saurav Chaudhary
Saurav Chaudhary on 30 Oct 2020
As can be seen the first error you get is :
PortAudio Error: Unanticipated host error
Go through the following MATLAB answer which aims at resolving this issue-
The next error you are getting is :
PortAudio Error: Invalid sample rate
For this one workaround would be to look at the maximum sample rate supported, you can go through the below MATLAB answer to find out about this -
Hope, this helps.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by