Error using sound function

66 visualizaciones (últimos 30 días)
Andre Aroyan
Andre Aroyan el 1 de Nov. de 2024 a las 17:23
Comentada: Andre Aroyan el 4 de Nov. de 2024 a las 17:24
I'm have an odd issue with a function I'm using for a Matlab App. I've been using this funciton for over a year without any issues and I have not changed it since originally writing it. However, I am now getting an error when I use Matlab 2022b. I get the error when I run the app using the App designer or running the App after the installation in the App tab of Matlab. However, I do not get this error on the same computer when I use Matlab 2020b. I also do not get this error when I install the App on other computers using Matlab 2022b. Does anyone know why this happening?
Thanks
The error:
Error using sound
Device Error: Invalid number of channels
The Function:
function playWarning()
%
persistent warningSoundY
persistent warningSoundFs
if isempty(warningSoundY)
addpath('Assets\Sounds\')
[ warningSoundY, warningSoundFs ] = audioread('chord.wav');
end
sound(warningSoundY, warningSoundFs)
end

Respuesta aceptada

Subhajyoti
Subhajyoti el 3 de Nov. de 2024 a las 0:06
The error is occurring due to channel support limitation on the system. Stereo playback is available only if your system supports it.
Here, in the following implementation, I have determined the number of channels in a WAV file using the ‘audioread’ function to read the file and then inspect the dimensions of the audio data.
% Read the audio file
[warningSoundY, ~] = audioread(chord.wav);
% Determine the number of channels
numChannels = size(warningSoundY, 2);
% Display the number of channels
disp(['Number of channels in the WAV file: ', num2str(numChannels)]);
You can find if output device supports the sample rate, number of bits per sample, and number of channels specified by the values of ‘Fs’, ‘nBits’, and ‘nChannels’, respectively. If no supporting device is found, then ‘suppDevID’ is -1.
info = audiodevinfo
suppDevID_32 = audiodevinfo(0,44100,32,2)
Refer to the following resources to know sending audio signals to the speaker in MATLAB:
Additionally, you can refer to the following resources to know more about playing audio files in MATLAB:
  2 comentarios
Star Strider
Star Strider el 3 de Nov. de 2024 a las 6:30
The error is occurring due to channel support limitation on the system. Stereo playback is available only if your system supports it.
That is the essence of this problem. The audiodevinfo functiion is the only function that can address this issue, annd it does not return that information.
Andre Aroyan
Andre Aroyan el 4 de Nov. de 2024 a las 17:24
I think I figured out the problem and a couple possible solutions. It comes down to my headset, which does not support 2 channels. I wrote the folloing code.
%% Audio Test
clc; clear all;
soundFile = 'D:\Backup\Debugging\chord.wav';
[warningSoundY, warningSoundFs] = audioread(soundFile);
wavInfo = audioinfo(soundFile);
warningSoundBpS = wavInfo.BitsPerSample;
warningSoundNumChan = wavInfo.NumChannels;
disp(['Warning Sound Sample Rate = ' num2str(warningSoundFs)]);
disp(['Warning Sound Bits Per Sample = ' num2str(warningSoundBpS)]);
disp(['Warning Sound Number of Channels = ' num2str(warningSoundNumChan)]);
audioInfo = audiodevinfo;
numberOfOutput = length(audioInfo.output);
numberOfInput = length(audioInfo.input);
disp(['Number of Output Device = ' num2str(numberOfOutput)]);
for i = numberOfInput : numberOfInput + numberOfOutput - 1
outputSupports = audiodevinfo(0,i,warningSoundFs,warningSoundBpS,warningSoundNumChan);
outputName = audiodevinfo(0,i);
disp(['Output Device ' num2str(i) ' ( ' outputName ...
') Supports .wav file = ' num2str(outputSupports) ]);
end
sound(warningSoundY, warningSoundFs)
disp('Sound Played Successfully!')
If my defuault output device in windows is set to "Headset Earphone (2- Plantronics Blackwire 3215 Series) (Windows DirectSound))" when I open Matlab, and I run the code in Matlab 2022b I get this:
  • Warning Sound Sample Rate = 44100
  • Warning Sound Bits Per Sample = 16
  • Warning Sound Number of Channels = 2
  • Number of Output Device = 3
  • Output Device 3 ( Primary Sound Driver (Windows DirectSound)) Supports .wav file = 0
  • Output Device 4 ( Headset Earphone (2- Plantronics Blackwire 3215 Series) (Windows DirectSound)) Supports .wav file = 0
  • Output Device 5 ( Speakers (Realtek(R) Audio) (Windows DirectSound)) Supports .wav file = 1
  • Error using sound
  • Device Error: Invalid number of channels
  • Error in AudioTest (line 28)
  • sound(warningSoundY, warningSoundFs)
If I run in 2020b, I get this:
  • Warning Sound Sample Rate = 44100
  • Warning Sound Bits Per Sample = 16
  • Warning Sound Number of Channels = 2
  • Number of Output Device = 3
  • Output Device 3 ( Primary Sound Driver (Windows DirectSound)) Supports .wav file = 1
  • Output Device 4 ( Headset Earphone (2- Plantronics Blackwire 3215 Series) (Windows DirectSound)) Supports .wav file = 0
  • Output Device 5 ( Speakers (Realtek(R) Audio) (Windows DirectSound)) Supports .wav file = 1
  • Sound Played Successfully!
If I remove the headset, I get the same results, but if I remove it and restart Matlab 2022b, I get:
  • Warning Sound Sample Rate = 44100
  • Warning Sound Bits Per Sample = 16
  • Warning Sound Number of Channels = 2
  • Number of Output Device = 2
  • Output Device 2 ( Primary Sound Driver (Windows DirectSound)) Supports .wav file = 1
  • Output Device 3 ( Speakers (Realtek(R) Audio) (Windows DirectSound)) Supports .wav file = 1
  • Sound Played Successfully!
And I restart Matlab 2022b with the headset plugged in, and defuault output device in windows set to "Speakers (Realtek(R) Audio", I get this:
  • Warning Sound Sample Rate = 44100
  • Warning Sound Bits Per Sample = 16
  • Warning Sound Number of Channels = 2
  • Number of Output Device = 3
  • Output Device 3 ( Primary Sound Driver (Windows DirectSound)) Supports .wav file = 1
  • Output Device 4 ( Speakers (Realtek(R) Audio) (Windows DirectSound)) Supports .wav file = 1
  • Output Device 5 ( Headset Earphone (2- Plantronics Blackwire 3215 Series) (Windows DirectSound)) Supports .wav file = 0
  • Sound Played Successfully!
So I have 2 possible solutions that I need to decide between.
Solution 1:
function playWarning()
%
persistent warningSoundY
persistent warningSoundFs
if isempty(warningSoundY)
addpath('Assets\Sounds\')
[ warningSoundY, warningSoundFs ] = audioread('chord.wav');
wavInfo = audioinfo(soundFile);
warningSoundBpS = wavInfo.BitsPerSample;
warningSoundNumChan = wavInfo.NumChannels;
end
audioInfo = audiodevinfo;
numberOfOutput = length(audioInfo.output);
numberOfInput = length(audioInfo.input);
if audiodevinfo(0,numberOfInput,warningSoundFs,warningSoundBpS,warningSoundNumChan)
sound(warningSoundY, warningSoundFs)
else
if warningSoundNumChan == 2
try
sound(warningSoundY(:,1), warningSoundFs)
catch
end
end
end
end
Or a simpler solution 2:
function playWarning()
%
persistent warningSoundY
persistent warningSoundFs
if isempty(warningSoundY)
addpath('Assets\Sounds\')
[ warningSoundY, warningSoundFs ] = audioread('chord.wav');
end
try
sound(warningSoundY, warningSoundFs)
catch
try
sound(warningSoundY(:,1), warningSoundFs)
catch
end
end
end
Thanks for the help!
Andre

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Audio I/O and Waveform Generation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by