View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Realtime audio input

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

1.0
1.0 | 1 rating Rate this file 6 Downloads (last 30 days) File Size: 2.4 KB File ID: #32304 Version: 1.0

Realtime audio input

by

 

Realtime audio input, flexible and glitch-free.

| Watch this File

File Information
Description

I explored different options for realtime audio input (see below for a summary) before having to settling for this option. Unfortunately this solution requires Windows and the Data Acquisition toolbox. I hope to be able to report at some point that this function has been superceded by something more generally useful, so please let me know if you come across something better.

Other possibilities:
PortAudio (http://www.portaudio.com/): seem to be the workhorse of audio input, but it's in C++, not Matlab code

PlayRec.m (http://www.playrec.co.uk/): An adaptation of PortAudio for Matlab. However, support seems to have ended around 2008, and I know several people who have spent many hours failing to compile it for more recent operating systems and/or Matlab versions.

PsychPortAudio (http://docs.psychtoolbox.org/PsychPortAudio): This may be the best option, although it requires PsychToolbox, which can be challenging to install itself. Can anyone confirm that PsychPortAudio works for realtime audio input?

In the meantime, the detailed function of tgrabaudio.m is described in its help section, copied below for convenience:

1/ Initialisation (mandatory)
    tgrabaudio('start', [fs, [maxduration]])

    fs = the sample-rate [default = 44100]
    maxduration = the maximum delay (in seconds) that is considered an
                  acceptable build-up of audio samples. If this buffer is
                  exceeded during auditory capture, an error will be generated.
    
2/ Audio capture
    wave=tgrabaudio(N);
    
    N = the number of samples to be "grabbed".

    wave = by default, stereo input [hard-wired: search the code for "addchannel" to adapt for mono input]

3/ Tidying up (mandatory)

    tgrabaudio('stop')

   This clears the persistent variables and stops the background audio
   input. Careful, if this step is omitted, Matlab is likely to crash or
   behave strangely the next time tgrabaudio is run. NB if a program that
   uses tgrabaudio crashes before calling tgrabaudio('stop'), don't forget
   to call tgrabaudio('stop') as soon as possible (either manually or using
   Matlab's try...catch keywords).

   EXAMPLE:

The following code shows how to use tgrabaudio to input sound in
realtime. For demo purposes, the audio is gathered over a finite period,
reconstructed into a single variable, and then simply played back.
However, the same construction could be used for continuous processing of
audio input.

>> framesamples=160; fs=16000; totalframes=100;
>> wave=zeros(framesamples*totalframes,2);
>> pointer=1;
>> tgrabaudio('start',fs);
>> for ii=1:totalframes
>> framedata=tgrabaudio(framesamples);
>> %do realtime processing here
>> wave(pointer:pointer+framesamples-1,:)=framedata;
>> pointer=pointer+framesamples;
>> end;
>> sound(wave,fs)

Acknowledgements

Real Time Microphone And Camera Data Acquisition And Audio Video Processing inspired this file.

Required Products Data Acquisition Toolbox
MATLAB release MATLAB 7.5 (R2007b)
Other requirements Currently set up for Windows. May be easy to adapt for other operating systems (keep me posted, and I'll update accordingly).
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
15 May 2016 Pilar Valencia

Genera linea de error en:
tgrabaudio ( "inicio", Fs);
NO SÉ QUE HACER, AYUDA.

Comment only
29 Mar 2016 PAVAN SN

Not working

06 Mar 2016 Trevor Agus

Hi Shruti,

You may be able to adapt it to work with that specific microphone. If you do, I would perhaps check that it is able to process the sound fast enough, otherwise you might get glitches. You could test it by recording (for example) a pure tone, for which glitches might be more obvious.

Trevor

Comment only
04 Mar 2016 Shruti Modak

Hi,

I need to use the inbuilt microphone of my laptop to get a signal which has to then be A-weighted to calculate the db(A)-SPL value of the sound.
My short-time goal is to take real-time input from the microphone, filter it by a-weighting and generate a noise map of the sounds in a classroom environment.

Can this code be used for my application?

Comment only
30 Jun 2014 Ramu Raut

??? Undefined function or method 'tgrabaudio' for input arguments of type 'char'.

Error in ==> Untitled at 4
tgrabaudio('start',fs);

Why this problem is showing while i am running this code..

Comment only

Contact us