record sound Synchronaouslly from 3 separate Microphone - matlab

I try to record sound from 3 separate USB-Microphones. Using (Matlab 2008)
I use this Command:
%% Definr audio Channel
r1 = audiorecorder(44100, 16,1,1);
r2 = audiorecorder(44100, 16,1,2);
r3 = audiorecorder(44100, 16,1,3);
%% Start record
record(r1); % speak into microphone...
record(r2);
record(r3);
%% Stop record
stop(r1);
stop(r2);
stop(r3);
I want to compare between recorder files from 3 microphone, but Microphons dont start and stop record in same time. and alwayes there are about (1500 to 3000 sample) deferance between recorder files.
so the problem:
I want to start record (in 3 microphone) in same time. and Stop all in same time.
are there any command to start record in same time (or constant time, not same time exactly).
I hope I could exolain what i need
and hope find a help...................................Thanx

Rather than using three separate audiorecorder objects, just use one and call its constructor with 3 in the third argument (nChannels - see http://www.mathworks.co.uk/help/matlab/ref/audiorecorder.html ). This will instruct it to record three channels simultaneously. That is,
r = audiorecorder(44100, 16, 3, 1);

Related

How do I control which channel sound is played through using MATLAB?

I'm a novice MATLAB user, so apologies if the question is very basic. I need a .wav sound file to be played in a single, specific channel -- let's say the left channel. As it is, my code reads in the sound file, and I add in a column of zeros to nullify the channel I don't want, like so:
currentDir = pwd;
soundFile = [currentDir '\sound1.wav']; % load the file
[y, Fs] = audioread(soundFile); % read the file in
soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = 0; % nullifies the right channel
sound = audioplayer(soundData,Fs);
play(sound);
As it stands, the code currently produces a sound that is full volume in the left speaker, and half volume (but still very much audible) in the right speaker. I have tried this with at least 20 .wav files, with the same result.
In case it's relevant, this happens even when I write in code that explicitly matches the length of the sound variable in 0s, like so:
[y, Fs] = audioread(soundFile);
silentChannel = zeros(size(y));
soundData(:,1) = y(:,1); % keeps sound for the left channel
soundData(:,2) = silentChannel(:,2); % nullifies the right channel
Does anybody know what I'm doing wrong, or have any thoughts?
Your code is definitely correct and it should only play audio in the left channel. I suspect that the problem is caused by a sound-card/driver issues. Allow me suggest the following troubleshooting steps:
Save your output as a wav file using audiowrite('output.wav', soundData, Fs). Play this using a different audio player, such as Audacity. If you still hear output in both channels, it must be a sound-card/driver issue.
Assuming that you are using a Windows PC (going by the syntax in your file path), make sure all sound enhancements are disabled. How to do this depends on the PC. If there is a third-party app controlling the playback settings, you'd have to use that. Otherwise find the settings shown in the picture below in the Control Panel.
In MatLab the expected method for playing sound is the method sound(data,Fs)
To control the channel the sound emits on, you'll want to know how sound() reads data.
data is a matrix with the columns representing channels, and with the rows holding the samples of the waveform for a given sampling fequency Fs
here is a simple implementation.
function treismanwolfe()
close all
clear all
clc
Fs = 40000;
tau = 2*pi();
t = 0:tau/(Fs-1):tau/2;
left = sin(t).*(sin(t*200)+sin(t*1600));
left= left/max(abs(left));
left = left'; %turn column vector into row
right = sin(t).*(sin(t*800)+sin(t*400));
right= right/max(abs(right));
right = right'; %turn column vector into row
data = [left,right*0]; %multiply either by 0 to nullify
sound(data,Fs); %so you can hear it.
end
I hope this works for you. Enjoy!
When I run your code the audio output is only audible in the left channel as you have specified.
#Austin Kootz' version with the sound()-function is just as good and also produces what you're looking for, but with audioplayer-object you have the ability to stop the playback in the middle of the playback (as you probably know)
Have you tried converting your .wav to another format to see if that makes a change?

Use audioread or equivalent to play audio in a specific timestamp

I am using audioread to play audio, now I would like play the track in different timestamps. What I have so far is:
[testSound,Fs] = audioread('test.wav');
sound(testSound,Fs);
Is it possible to somehow specify that the audio-track shall start at for example second 5? To be more specific, my audio sample test.wav is 45 second long, instead of playing the sound from the beginning, I would like to define where it should start playing.
Any help is much appreciated!
You can extract out a portion of the signal so that you're starting at the 5 second mark, then play it. Simply put, you'd start sampling at 5 times the sampling rate as the starting index all the way to the end then play the sound:
[testSound,Fs] = audioread('test.wav'); % From your code
beginSecond = 5; % Define where you want to start playing
beginIndex = floor(beginSecond*Fs); % Find beginning index of where to sample
soundExtract = testSound(beginIndex:end, :); % Extract the signal
sound(soundExtract, Fs); % Play the sound
Alternatively, since you're using audioread, you can actually specify where to start sampling the sound. You'd use the same logic above and specify the beginning and end of where to sample the sound in terms of samples. However, you'd need to know what the sampling rate is first so you'd have to call audioread twice to get the sampling rate, then finally the signal itself:
beginSecond = 5; % Define where you want to start playing
[~, Fs] = audioread('test.wav'); % Get sampling rate
beginIndex = floor(beginSecond*Fs); % Find beginning index of where to sample
[soundExtract, ~] = audioread('test.wav', [beginIndex inf]); % Extract out the signal from the starting point to the end of the file
sound(soundExtract, Fs); % Play the sound

playing song on a specific earplug (channel)

I have a device that is like 3 headphones in one ( so 6 earplugs) . My goal is to play one diferent song on each earplug ( 6 songs). So I starded by playing one song on an earplug. For me one channel means one earplug (but maybe I am wrong) .I am using Psychtoolbox:
function BasicSoundOutputDemo( wavfilename)
AssertOpenGL;
% Read WAV file from filesystem:
[y, freq] = psychwavread(wavfilename);
aux = y' ;
wavedata = aux(1,:);
InitializePsychSound;
devices = PsychPortAudio('GetDevices' );
pahandle = PsychPortAudio('Open', [], [], 0, freq, 1);// nr channels = 1
PsychPortAudio('FillBuffer', pahandle, wavedata);
t1 = PsychPortAudio('Start', pahandle, 1, 0);
KbReleaseWait;
while ~KbCheck
% Wait a seconds...
WaitSecs(1);
end
PsychPortAudio('Stop', pahandle);
PsychPortAudio('Close', pahandle);
fprintf('Demo finished, bye!\n');
But it didn’t worked. Instead of playing the sound on just one earplug, it was playing on 2 earplugs.
I get this warnings
PTB-INFO: Using specially modified PortAudio engine, based on offical
version: PortAudio V19-devel WITH-DIM
Will use ASIO enhanced Portaudio driver DLL. See
Psychtoolbox/PsychSound/PortAudioLICENSE.txt for the exact terms of
use for this dll.
Disclaimer: "ASIO is a trademark and software of Steinberg Media
Technologies GmbH."
PTB-Warning: Although using the ASIO enabled Psychtoolbox sound
driver,
PTB-Warning: could not find any ASIO capable soundcard in your system.
PTB-Warning: If you think you should have an ASIO card, please check
your
PTB-Warning: system for properly installed and configured drivers and
retry.
PTB-Warning: Read "help InitializePsychSound" for more info about ASIO
et al.
PTB-INFO: New audio device with handle 0 opened as PortAudio stream:
PTB-INFO: For 1 channels Playback: Audio subsystem is MME, Audio
device name is Microsoft Sound Mapper - Output
PTB-INFO: Real samplerate 44100.000000 Hz. Input latency 0.000000
msecs, Output latency 464.399093 msecs.
Then I decided to try another aproch. Lets play the song on other 2 earplugs
I used PsychPortAudio('GetDevices') to find the id of the earplugs pair. Strange is that instead of 3 devices with 2 channels I found 4.
And I used PsychPortAudio('Open' for id 7,9,18 and 20 but every time the song was played on the same earplug pair, the same pair from when I tried to play on just one earplug.
This is a picture with the 4 devices
function BasicSoundOutputDemo( wavfilename)
AssertOpenGL;
% Read WAV file from filesystem:
[y, freq] = psychwavread(wavfilename);
wavedata = y' ;
nrchannels = size(wavedata,1); % Number of rows == number of channels.
InitializePsychSound;
devices = PsychPortAudio('GetDevices' );
pahandle = PsychPortAudio('Open', 18, [], 0, freq, nrchannels);
PsychPortAudio('FillBuffer', pahandle, wavedata);
t1 = PsychPortAudio('Start', pahandle, 1, 0);
KbReleaseWait;
while ~KbCheck
% Wait a seconds...
WaitSecs(1);
end
PsychPortAudio('Stop', pahandle);
PsychPortAudio('Close', pahandle);
fprintf('Demo finished, bye!\n');
Now the only thing that is different at warnigs it this
PTB-INFO: For 2 channels Playback: Audio subsystem is Windows
DirectSound, Audio device name is Speakers (USB Multi-Channel Audio
Device)
Sorry for this long post but i wanted to give you all the informations.
Can you tell were I am wrong. How can I play one song on a specific earplug. I think if I know that then I just copy the code and put another song and so I will play one song on each earplug
1) You likely don't want to run AssertOpenGL every time you present a sound.
2) Your code looks correct, though interestingly on my Apple laptop and built-in sound, sending a single channel signal is also playing from both headphone channels.
3) What audio device are you using? From your device listing, it looks like the 4 you have listed might be different interfaces to the same devices (2 outputs (one digital, one analog) X two APIs (one MME, one DirectSound). Are there any other entries in your device list?
a partial answer to my question. I found how to play a song in an earplug and another in the other. psychwavread gives me a 2 rows array. so i put in an array the first row of one song and the first row of the other song. so now i have a 2 row array simillar to the one from when i play one song, but now i play 2 songs
[y, freq] = psychwavread(wavfilename1);
[y1, freq1] = psychwavread(wavfilename2);
aux = y';
aux1 = y1';
wavedata = [aux1(1,:) ; aux(1,:)];
so i manage to play on 4 channels i am sure that i can play on 6 and 8. I installed ASIO4ALL and I choose the ASIO4ALL device id when open. when ASIO4all opens I can choose my 6 channels device and after that I just choose in the open function on which channels to play the sounds

How to play an audio signal in matlab controlling the left and right channel of earphone on Matlab individually?

I am doing audio signal processing in matlab. As a part of my project, I am playing recording audio signal, processing it, and playing it in real-time. Now, the output that I am sending through the two channels, I want that to be processed differently, and want to plot the graphs as well.
Basically, I want the left ear to hear a differently processed signal than the right ear and plot it as well.
Even if it is not in real time i.e. any stored signal (.wav,etc) will be helpful.
Any help will be appreciated (don't have much time :().
If you're using a stored .wav file, you can use wavread to import, which will import the file as a two-column array. If you call this array A, you could manipulate the left channel using A(:, 1) and the right channel using A(:, 2).
If you're using audiorecorder to record the audio, you'll have to change the number of channels from 1 to 2 so that you record in stereo. The default is mono.
EDIT: To plot in realtime, you can work off of the following function I wrote. The function takes the amount of time you want to record as its input runtime. It creates a timer timerID and continuously acquires the audio data from the recorder object and plots it, using drawnow to refresh the figure. If you wanted to do any processing, you could do it in the loop right before the plot commands.
function audioPlot(runtime)
timerID = tic;
recObj = audiorecorder(44100, 24, 2);
record(recObj);
h(1) = subplot(2, 1, 1);
h(2) = subplot(2, 1, 2);
while (toc(timerID) < runtime)
if recObj.TotalSamples > 0
audioData = getaudiodata(recObj);
plot(h(1), audioData(:, 1))
plot(h(2), audioData(:, 2))
xlabel(h(1), 'Left Channel')
xlabel(h(2), 'Right Channel')
drawnow
end
end
stop(recObj);
end
Happy to answer any questions!

Audiorecorder in matlab

I'm new to matlab. Essentially i want to get audio signal of fixed length (10 seconds) from microphone, perform some operations and play output sound. I was trying to use audiorecorder something like this:
y = audiorecorder(44100, 16, 1)
record(y, 10);
% signal processing;
play(output);
The problem is it's asking for a user prompt to stop recording first and then go to next stage. I just want it to record (on user prompt) whatever it gets for 10 sec and stop automatically. Then proceed to next stages and play final output, all without further user prompt. Is there any way to get around this?
You can use audiorecorders recordblocking method to record for a specfied amount of time, and wait until that time has elapsed.
So, your new code would look like:
a = audiorecorder(44100, 16, 1)
% record for 10 seconds before moving on
recordblocking(a, 10);
% signal processing;
play(a);