Main Content

Acquire Audio Data from a Mobile Device Microphone

This example shows you how to connect from MATLAB® to your mobile device microphone and acquire audio signals. You can use the MATLAB code in this example to access your acquired audio data from any of the following when logged in to your MathWorks account:

  • MATLAB Mobile™ running on the same device

  • MATLAB Mobile running on a different device

  • MATLAB Online™

  • MATLAB on a desktop or laptop computer, with the installed add-on MATLAB Support Package for Android® Sensors

Set Up Mobile Device

Install and set up MATLAB Mobile on your mobile device. Then, sign in to the MathWorks® Cloud from the MATLAB Mobile Settings menu. For more information about these steps, see Install MATLAB Mobile on Your Device and Sign In to the Cloud.

Start MATLAB Mobile on your device.

Enable Microphone Access on Device

  1. Tap the menu and select Sensors.

  2. In the Sensor Setting section, tap More; and in the MATLAB Remote Access section, toggle Sensor Access on. Tap OK to confirm access. This allows programmatic access to the device sensors.

  3. Now a Microphone Access switch is available. Toggle this on, and tap OK to confirm access. This grants permission specifically for remote microphone access.

  4. Return to Sensor Settings, and tap Stream to and select MATLAB.

  5. Then toggle on the Microphone access. This turns on the microphone to receive audio input.

At this point, you should see a level bar changing in real time to indicate sound being received by the microphone. The device microphone is now ready to acquire data for streaming to your MATLAB session.

Create a Connection to Your Device

In your connected MATLAB session, create a mobiledev object m in the Command Window. If using MATLAB Mobile on this or another device, use the Commands screen.

m = mobiledev
m = 
mobiledev with properties:

                      Device: 'Galaxy Tab A (2017)'
                   Connected: 1
                     Logging: 0
            AvailableCameras: {'back' 'front'}
        AvailableMicrophones: {'SM-T380-bottom'}
          SelectedMicrophone: 'SM-T380-bottom'

            InitialTimestamp: ''

   AccelerationSensorEnabled: 0
AngularVelocitySensorEnabled: 0
       MagneticSensorEnabled: 0
    OrientationSensorEnabled: 0
       PositionSensorEnabled: 0
           MicrophoneEnabled: 1
Show all properties

The SelectedMicrophone property indicates which microphone is active. In this case, there is only one element in the AvailableMicrophones property. Also note the Connected and MicrophoneEnabled properties, each with a value of 1, indicating the setup is complete. You can set MicrophoneEnabled to either 1 or 0 to programmatically turn the microphone on or off.

This default display of the object m does not show all its properties. You can click the Show all properties link to see more. Among these is the object's Microphone property, which you can examine to get more information about the microphone and its settings.

m.Microphone
  Microphone with properties:

                    Name: 'SM-T380-bottom'
    AvailableSampleRates: [8000 11025 12000 16000 22050 24000 32000 44100 48000]
             NumChannels: 2
              SampleRate: 44100
               IsEnabled: 1

Record Audio Data

You can begin to capture audio data.

  1. Provide a source of audio input by speaking into your microphone.

  2. On the device Sensors display, tap Start.

  3. Let the recording proceed for a few seconds while you speak, and then tap Stop.

A programmatic alternative for stopping and starting the recording is to set the Logging property on the mobiledev object:

m.Logging = 1
⋮
m.Logging = 0

The acquired data is now available to be read into MATLAB.

Read and Plot Audio Data

When you look again at the connected mobiledev object m in MATLAB, the MicrophoneEnabled property indicates the number of audio data samples acquired.

m
m = 

mobiledev with properties:

                      Device: 'Galaxy Tab A (2017)'
                   Connected: 1
                     Logging: 0
            AvailableCameras: {'back' 'front'}
        AvailableMicrophones: {'SM-T380-bottom'}
          SelectedMicrophone: 'SM-T380-bottom'

            InitialTimestamp: ''

   AccelerationSensorEnabled: 0
AngularVelocitySensorEnabled: 0
       MagneticSensorEnabled: 0
    OrientationSensorEnabled: 0
       PositionSensorEnabled: 0
           MicrophoneEnabled: 1    (109224 Logged samples)

Read the data into a timetable in the MATLAB workspace, and examine the first few rows of the table.

audTT = readAudio(m,OutputFormat="timetable");
head(audTT)
         Time           Left         Right  
    ______________    _________    _________

    0 sec                     0            0
    2.2676e-05 sec    -0.030763    -0.030091
    4.5351e-05 sec    -0.091281      -0.0889
    6.8027e-05 sec     -0.14975     -0.14603
    9.0703e-05 sec     -0.20145     -0.19605
    0.00011338 sec     -0.24812     -0.24201
    0.00013605 sec     -0.28562     -0.27818
    0.00015873 sec     -0.31437     -0.30659

Use a stacked plot to view the data from the microphone's two channels.

stackedplot(audTT)

plots of captured audio data

See Also

Functions