Matlab Audio Output. Need Help

Moderator
Joined 2004
Paid Member
Does any one here know about routing audio in Matlab? I'm running a program that does HRTFs and I can't get audio out any soundcard. The program opens and functions fine, but when loading tells me that it does not know how to output audio on my computer.

It's old software from UC Davis that was last updated in 2012, before Windows 10, which is what I'm using using. Originally it ran on Matlab 5. I am running Matlab R2020b on Windows 10

Two scripts seem to be blocking me, and as I've never before used Matlab, I don't know how to edit the files for my system. check_sound.m runs first and tells me "I'm sorry, I do not know who to output sounds on your operating system"
Code:
% Script to check the OS to see if sound output is possible
% Copyright (C) 2001 The Regents of the University of California

play_sound_flag = 1;

switch computer,
   
case 'PCWIN'                            % MS Windows
   if (exist('wavplay') ~= 2) & (exist('sound') ~= 2),
      play_sound_flag = 0;
      if show_warning,
         fprintf('\n  I''m sorry, but I could not find either wavplay.m\n');
         fprintf('  or sound.m on your MATLAB system.\n');
         fprintf('  You can still use the graphics features,\n');
         fprintf('  but the ''play'' actions will be unavailable.\n\n');
         fprintf('Press any key to continue ... ');
         pause;
         fprintf('\n');
      end;
   end;

case 'LNX86'                             % Linux Intel for Matlab 5
   if exist('sound') ~= 2,
      play_sound_flag = 0;
      if show_warning,
         fprintf('\n  I''m sorry, but I could not find sound.m\n');
         fprintf('  on your MATLAB system.\n');
         fprintf('  You can still use the graphics features,\n');
         fprintf('  but the ''play'' actions will be unavailable.\n\n');
         fprintf('Press any key to continue ... ');
         pause;
         fprintf('\n');
      end;
   end;
   
case 'GLNX86'                            % Linux Intel for Matlab 6 R12
   if exist('sound') ~= 2,
      play_sound_flag = 0;
      if show_warning,
         fprintf('\n  I''m sorry, but I could not find sound.m\n');
         fprintf('  on your MATLAB system.\n');
         fprintf('  You can still use the graphics features,\n');
         fprintf('  but the ''play'' actions will be unavailable.\n\n');
         fprintf('Press any key to continue ... ');
         pause;
         fprintf('\n');
      end;
   end;


otherwise
   play_sound_flag = 0;
   if show_warning,
      fprintf('\n  I''m sorry, I do not know how to output sounds\n');
      fprintf(['  on your operating system -- ' computer '.\n']); 
      fprintf('  You can still use the graphics features,\n');
      fprintf('  but the ''play'' actions will be unavailable.\n\n');
      fprintf('  If you know what MATLAB function to call\n');
      fprintf('  to output stereo sound, do the following:\n');
      fprintf('    1.  Edit the case statement in ''check_sound.m''\n');
      fprintf(['        to add the additional case ''' computer '''.\n']);
      fprintf('    2.  Edit the function ''play_sound_array'' to include\n');
      fprintf('        an appropriate call to your sound output function.\n\n');
      fprintf('Press any key to continue ... ');
      pause
      fprintf('\n');
   end;
end;

It seems that check_sound.m calls play_sound_array.m to do the routing but I have no idea what to change there.

Code:
function play_sound_array(sound_array, fs)
% function play_sound_array(sound_array [fs])
% 
% Plays the sound data at a sampling frequency fs,
% which defaults to fs = 44100.
% Assumes that sound_array(:,1) is the left channel
% and that sound_array(:,2) is the right channel,
% and that the maximum absolute value of the values
% in sound_array is less than 1.
% Copyright (C) 2001 The Regents of the University of California

%% Check for valid arguments

if nargin < 1,
   fprintf('Format: play_sound_array(sound_array [fs])\n');
   return;
end;

if nargin < 2,
   fs = 44100;
end;

num_channels = size(sound_array,2);
if num_channels > 2,
   error('sound_array cannot have more than two channels.');
elseif num_channels == 1,
   sound_array = [sound_array sound_array];
end;


%% Use proper sound output function

switch computer,
   
case 'PCWIN'                                    % MS-windows OS
   vernum = version_number;
   if vernum < 6,
      sound_array = sound_array(:,[2,1]);       % Swap left and right channels for 5.x
   end;
   if exist('wavplay') == 2,
      wavplay(sound_array,fs,'sync');
   elseif exist('sound') == 2,
      sound(sound_array,fs);
   else
      fprintf('\n  This message should not appear.\n');
      fprintf('  Unable to find either wavplay.m or sound.m\n\n');
   end;
   
case 'LNX86'                                    % Linux Intel OS for MATLAB 5.x
   if exist('sound') == 2,
      sound(sound_array,fs);
   else
      fprintf('\n  This message should not appear.\n');
      fprintf('  Unable to find sound.m\n\n');
   end;

case 'GLNX86'                                   % Linux Intel OS for MATLAB 6 R12
   if exist('sound') == 2,
      sound(sound_array,fs);
   else
      fprintf('\n  This message should not appear.\n');
      fprintf('  Unable to find sound.m\n\n');
   end;
   
   %%%  Insert other cases here %%%
   
otherwise                                       % Unknown OS
   fprintf('\n  This message should not appear.\n');
   fprintf('  If playing sound has been enabled,\n');
   fprintf('  you need to edit play_sound_array\n');
   fprintf('  to specify how sound_array is\n');
   fprintf('  to be played.\n');
   
end;
Can anyone help with editing this so that it outputs sound on Windows 10? Default soundcard is fine. If I can choose the USB card, even better.
 
I made some progress by creating a new case called PCWIN64 and by calling audioplayer instead of wavplay. But still get errors farther down the line. 🙁

I used this little script to direct calls to wavplay to audioplayer instead
Code:
function wavplay(signal, sampleRate)
% WAVPLAY  Plays an audio signal using the audioplayer object.
%   WAVPLAY(x, Fs) plays the audio signal x at the specified sample rate Fs.

    % Instantiate object to play audio data.
    player = audioplayer(signal, sampleRate);
    
    % Play signal from beginning to end (no overlap with other code possible).
    playblocking(player);
end
and made a little more progress, but am stalled at:
Error using wavplay
Too many input arguments.


Ah well. That' what happens when you are a MATLAB newb. I'll keep at it and might learn something. 🙂