Modifying the example "Read from Microphone and Write to Audio File". Help!

14 views (last 30 days)

Hi everyone!

I'm trying to modify the example "Read from Microphone and Write to Audio File" (

Firstly, I succeeded to identify what my devices are with :

deviceReader = audioDeviceReader;
devices_reader = getAudioDevices(deviceReader)
deviceWriter = audioDeviceWriter;
devices_writer = getAudioDevices(audioDeviceWriter)
playRec = audioPlayerRecorder;
devices_player = getAudioDevices(playRec)

and then,

devices_reader =
    1×3 cell array
      {'Default'}    {'주 사운드 캡처 드라이버'}    {'Analog in 01-12(AudioFire 12)'}
devices_writer =
    1×5 cell array
      {'Default'}    {'주 사운드 드라이버'}    {'Analog out 01-08…'}    {'Analog out 09-12…'}    {'스피커(Realtek Hi…'}
devices_player =
    1×2 cell array
      {'Default'}    {'ASIO Echo FireWire'}

I connected my smartphone which are playing music with an AUX cable to Analog input #2.

And I expect 'myRecording.wav' should have 5 seconds part of the music playing by my phone.

But I don't know which value is the corresponding one.

So I modified the example "Read from Microphone and Write to Audio File" with 3 cases like below :

% deviceReader = audioDeviceReader;                                          % case 1
% deviceReader = audioDeviceReader('Device','Analog in 01-12(AudioFire 12)');% case 2
% deviceReader = audioDeviceReader('Driver','ASIO');                         % case 3
fileWriter = dsp.AudioFileWriter('myRecording.wav');
while toc < 5
    acquiredAudio = deviceReader();
disp('Recording complete.')

but I can't hear anything with the result 'myRecording.wav' in all 3 cases! I attach the returned messages by


case 1.

ans = 
    struct with fields:
                    Driver: 'DirectSound'
                DeviceName: '주 사운드 캡처 드라이버'
      MaximumInputChannels: 2
>> Ex6_Read_from_Mic_and_wirte_Audio_file
Recording complete.

case 2.

ans = 
    struct with fields:
                    Driver: 'DirectSound'
                DeviceName: 'Analog in 01-12(AudioFire 12)'
      MaximumInputChannels: 12

case 3.

ans = 
    struct with fields:
                    Driver: 'ASIO'
                DeviceName: 'ASIO Echo FireWire'
      MaximumInputChannels: 12

what should I change ?

Accepted Answer

Gabriele Bunkheila
Gabriele Bunkheila on 2 Jul 2018
Edited: Gabriele Bunkheila on 3 Jul 2018
Hi Nuri,
You code looks fine on the surface and this should indeed work with no issues. Since you are using channel #2 as input, can I double-check that you tried setting that on the object? You can use either of
deviceReader.NumChannels = 2;
deviceReader.ChannelMapping = 2;
The first will return a Nx2 matrix, the second only a N-long column vector if you keep NumChannels equal to 1. Please let us know how this goes.
Good luck!
  1 Comment
nuri an
nuri an on 3 Jul 2018
Edited: nuri an on 3 Jul 2018
Thanks, Gabriele! I solved the problem by getting your answer about ChannelMapping. The result of your code was same as your expectation.
And by your help, I could go to the next step : using "audioPlayerRecorder".
But I have another problem of ChannelMapping.
When I modified ChannelMapping from [1,2] to [2,1], then it works.
And when I increase the # of channels, i.e from [1,2,3,4] to [2,1,3,4], then it works as well.
But from [1,2,3,4] to [2,3, 1,4], then it doesn't work. the result is same as [1,2,3,4] to [2, 1,3,4].
And this phenomena happen when [1,2,3,4] to [2,3,4, 1] case too.
I wrote another question post. below is the link of it. Please see the detail and give me some advice. ( )
Thank you for your help.
Best regards,

Sign in to comment.

More Answers (1)

Ajey Pandey
Ajey Pandey on 29 Jun 2018
Hi, Nuri!
I tried your code on my machine (with a microphone-headset combo), and I managed to get a .wav file with audio.
I'd check the hardware on your computer. Try checking the sound settings of your computer. There should be a tab in system settings for audio devices, and it will probably have a little meter to show whether there's audio coming into that input. There's a decent chance that your audio input (especially if it's designed for headsets) might not be reading the audio from your phone.
I'd also try extending the length of time you record by. When I tried, the first three seconds of audio got cut off the .wav file.
Hope this helps!
  1 Comment
nuri an
nuri an on 30 Jun 2018
Edited: nuri an on 30 Jun 2018
Hi Ajey, Thank you for testing and answering my question!
I tried your advise, but I checked the meter whether the input signal from my phone to audio card is going well. and my MATLAB code still doesn't work.
Here I attach the picture of my audio-interface. you can see the Audiofire12 console.
You can see the audio signal is going well.
From your testing, I found out that my code has no wrong point but the problem is my device.
As I know the Audio system toolbox is supporting the devices using AISO driver, But I don't know why MATLAB can't identify my devices..
Thank you

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!