Real time audio signal from microphone
Show older comments
Hello everyone, I am working with Matlab 2018b.
Im trying to develop a software wich aims to process in realtime a voice audio signal coming from the microphone.
This is the way I create an object and I ask to process the signal every "2*round(0.04*44100)" samples using a function called Auditive2
global f0l; f0l=[];
% global frames; frames=[];
d = daq.getDevices;
dev = d(2);
s = daq.createSession('directsound');
addAudioInputChannel(s, dev.ID, 1);
s.IsContinuous = true
procesamiento = @(src, event) Auditive2(event.Data, src.Rate);
hl = addlistener(s, 'DataAvailable', procesamiento);
s.NotifyWhenDataAvailableExceeds =2*round(0.04*s.Rate);
startBackground(s);
pause(10)
stop(s);
s.IsContinuous = false;
The next is Auditive 2 function wich returns the f0l value
function Auditive2(frame, fs)
global f0l
% global frames
frame = frame - mean (frame );
windowframe=2*round(0.04*fs);
[rxx,~] = xcorr(frame, frame);
% find peaks to measure the freq
rxx(rxx < 0) = 0; %eliminar la parte negativa
center_peak_width = find(rxx(windowframe:end) == 0 ,1); %find first zero after center
rxx(windowframe-center_peak_width : windowframe+center_peak_width ) = min(rxx);%Eliminar el primer pico
[~, loc] = max(rxx); % encontrar el siguiente pico y su localizacion
period = abs(loc - length(frame)+1); %el periodo en muestras
clc;
f0=fs/period;
% convert pitch into MIDI numer asociated
f0=12*log2(f0/440)+69;
f0l =[f0l f0'];
% frames =[frames];
% voice =[voice frame'];
Im trying to Run this code in MAC computer but its not posible.
Does anyone know how to process microphone signal in real time?
1 Comment
Walter Roberson
on 24 Jul 2020
Note; for real-time audio work, do not use audiorecorder: use the Audio Processing Toolbox tools instead. See for example https://www.mathworks.com/matlabcentral/answers/569854-issue-with-trying-to-play-and-record-audio-simultaneously
Answers (1)
Walter Roberson
on 26 Jul 2020
Edited: Walter Roberson
on 26 Jul 2020
0 votes
Sound card support in DAQ is available only on Windows. Also, directsound is Windows only.
The link I already posted shows using the Audio System Toolbox to read sound.
2 Comments
Danny Lasso
on 26 Jul 2020
Walter Roberson
on 26 Jul 2020
Edited: Walter Roberson
on 26 Jul 2020
Demo.
The filter was put together pretty arbitrarily.
% The following code was used to design the filter coefficients:
% % Equiripple Bandpass filter designed using the FIRPM function.
%
% % All frequency values are in Hz.
% Fs = 22100; % Sampling Frequency
%
% Fstop1 = 3000; % First Stopband Frequency
% Fpass1 = 4000; % First Passband Frequency
% Fpass2 = 6000; % Second Passband Frequency
% Fstop2 = 7000; % Second Stopband Frequency
% Dstop1 = 0.31622776602; % First Stopband Attenuation
% Dpass = 0.057501127785; % Passband Ripple
% Dstop2 = 0.0001; % Second Stopband Attenuation
% dens = 20; % Density Factor
%
% % Calculate the order from the parameters using FIRPMORD.
% [N, Fo, Ao, W] = firpmord([Fstop1 Fpass1 Fpass2 Fstop2]/(Fs/2), [0 1 ...
% 0], [Dstop1 Dpass Dstop2]);
%
% % Calculate the coefficients using the FIRPM function.
% b = firpm(N, Fo, Ao, W, {dens});
Hd = dsp.FIRFilter( ...
'Numerator', [0.00237477459350913 0.0064614843733655 ...
-0.00103059936849055 -0.030238372524698 -0.0585826389852291 ...
-0.0532937837200882 -0.0240881134086325 -0.00228288514751351 ...
0.0142103378085535 0.028903897155177 0.0118187515150954 ...
-0.0288463509933308 -0.0321916763899556 -6.61268554359109e-05 ...
0.00952714002962012 0.011524554854245 0.0367211697541932 ...
0.0210377239851772 -0.0473798975515859 -0.0516403286130514 ...
0.0104132441650136 0.0113350605525577 0.00111067355551197 ...
0.0871711535781902 0.0841395148868027 -0.138981148118066 ...
-0.225937282476684 0.0684005595135966 0.296849246242398 ...
0.0684005595135966 -0.225937282476684 -0.138981148118066 ...
0.0841395148868027 0.0871711535781902 0.00111067355551197 ...
0.0113350605525577 0.0104132441650136 -0.0516403286130514 ...
-0.0473798975515859 0.0210377239851772 0.0367211697541932 ...
0.011524554854245 0.00952714002962012 -6.61268554359109e-05 ...
-0.0321916763899556 -0.0288463509933308 0.0118187515150954 ...
0.028903897155177 0.0142103378085535 -0.00228288514751351 ...
-0.0240881134086325 -0.0532937837200882 -0.0585826389852291 ...
-0.030238372524698 -0.00103059936849055 0.0064614843733655 ...
0.00237477459350913]);
fs = 22100/4;
FrameSize = 512;
adr = audioDeviceReader(fs, FrameSize, 'NumChannels', 2);
adw = audioDeviceWriter(fs, 'SupportVariableSizeInput', true, 'BufferSize', FrameSize);
outbuf = zeros(FrameSize, 2);
while true
inbuf = adr();
outbuf = step(Hd, double(inbuf));
adw(outbuf);
end
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!