Main Content

audiocapture

Connection between audio input device and NVIDIA hardware

Since R2021a

    Add-On Required: This feature requires the MATLAB Coder Support Package for NVIDIA Jetson and NVIDIA DRIVE Platforms add-on.

    Description

    This object represents a connection between an audio input device and a NVIDIA® hardware. You can interact with the audio device using the functions listed in Object Functions.

    Creation

    Description

    example

    audioCaptureObj = audiocapture(hwObj,DeviceName) creates an object that represents the connection to the audio input device, DeviceName, connected to the NVIDIA hardware, hwobj. The NVIDIA hardware is represented by a jetson or drive object.

    example

    audioCaptureObj = audiocapture(___,Name,Value) sets Properties using name-value pairs. For example, audioCaptureObj = audiocapture(hwobj,"plughw:1,0","SampleRate",44100) reads audio data at a sample rate of 44100. You can specify multiple name-value pairs.

    Input Arguments

    expand all

    Connection to a specific NVIDIA hardware board, specified as a jetson or drive object.

    The name of the audio capture device attached to the hardware. To get the list of audio input devices connected to the hardware, use the listAudioDevices function.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

    There are a number of name-value pairs that can be used when you create the modbus object, including the two shown here. Some can only be used with either TCP/IP or Serial RTU, and some can be used with both transport types.

    Example: audioCaptureObj = audiocapture(hwobj,"plughw:1,0","SampleRate",44100)

    The sample rate used by the audio capture device to read data. To find the sample rates supported by the audio input device, use the listAudioDevices function.

    Example: audioCaptureObj = audiocapture(hwobj,"plughw:1,0","SampleRate",44100)

    This property is read-only.

    The number of samples per audio channel. To find the number of channels supported by the audio input device, use the listAudioDevices function.

    Example: audioCaptureObj = audiocapture(hwobj,"plughw:1,0","SampleRate",44100,"SamplesPerFrame",4410)

    This property is read-only.

    The number of channels supported by the audio capture device. To find the number of channels supported by the audio input device, use the listAudioDevices function.

    Example: audioCaptureObj = audiocapture(hwobj,"plughw:1,0","SampleRate",44100,"NumberOfChannels",1)

    Output Arguments

    expand all

    Connection to an audio capture device that is attached to the NVIDIA DRIVE® or Jetson™ hardware. To capture audio signals from the device, use the audioCaptureObj object with the capture function.

    Properties

    expand all

    This property is read-only.

    The name of the audio capture device attached to the hardware. To get the list of audio input devices connected to the hardware, use the listAudioDevices function.

    This property is read-only.

    The sample rate used by the audio capture device to read data.

    This property is read-only.

    The number of samples per audio channel.

    This property is read-only.

    The number of channels to capture by the audio capture device.

    Object Functions

    captureCapture data from audio device connected to NVIDIA hardware

    Examples

    collapse all

    This example shows how to create an application to add echo effect to an audio signal captured from the microphone of an NVIDIA Jetson board. The application then sends this output to the playback device connected to the Jetson board.

    Prerequisites

    Target Board Requirements

    • NVIDIA Jetson embedded platform.

    • USB audio device for recording and playback of audio signals.

    • Sound eXchange (SoX) utility and its development and format libraries.

    • Environment variables on the target for the compilers and libraries. For more information, see Install and Setup Prerequisites for NVIDIA Boards.

    Connect to NVIDIA Board

    Create a live hardware connection from the MATLAB® software to the NVIDIA Jetson hardware by using the jetson function. This example reuses the settings from the most recent successful connection to a NVIDIA Jetson board.

    hwobj = jetson;
    Checking for CUDA availability on the Target...
    Checking for 'nvcc' in the target system path...
    Checking for cuDNN library availability on the Target...
    Checking for TensorRT library availability on the Target...
    Checking for prerequisite libraries is complete.
    Gathering hardware details...
    Checking for third-party library availability on the Target...
    Gathering hardware details is complete.
     Board name              : NVIDIA Jetson TX2 Developer Kit
     CUDA Version            : 10.2
     cuDNN Version           : 8.2
     TensorRT Version        : 8.2
     GStreamer Version       : 1.14.5
     V4L2 Version            : 1.14.2-1
     SDL Version             : 1.2
     OpenCV Version          : 4.1.1
     Available Webcams       :  
     Available GPUs          : NVIDIA Tegra X2
     Available Digital Pins  : 7  11  12  13  15  16  18  19  21  22  23  24  29  31  32  33  35  36  37  38  40
    

    If this is your first time connecting to the target board,you must provide the host name or IP address, user name, and password of the target board. For example:

    hwobj = jetson('jetson-board-name','ubuntu','ubuntu');
    

    Identify Audio Devices Connected to NVIDIA Board

    To find the list of audio capture devices connected to the target, use the listAudioDevices function. The output contains the Device IDs according to ALSA standards, the bit-depths, and the sample rates supported by the devices.

    captureDevices = listAudioDevices(hwobj,"capture")
    captureDevices = struct with fields:
                Name: 'USB-Audio-PolyBlackwire3325Series↵PlantronicsPolyBlackwire3325Seriesatusb-3530000.xhci-2.3,fullspeed↵'
              Device: '3,0'
            Channels: {'1'}
            BitDepth: {'16-bit integer'}
        SamplingRate: {'8000'  '16000'}
    
    

    Similarly, to find the list of audio playback devices connected to the target, run the command.

    playbackDevices = listAudioDevices(hwobj,"playback")
    playbackDevices = struct with fields:
                Name: 'USB-Audio-PolyBlackwire3325Series↵PlantronicsPolyBlackwire3325Seriesatusb-3530000.xhci-2.3,fullspeed↵'
              Device: '3,0'
            Channels: {'2'}
            BitDepth: {'16-bit integer'}
        SamplingRate: {'8000'  '48000'}
    
    

    The echoGenerator Entry-Point Function

    Open the echoGenerator() function. This function takes audio input from a device connected to an Nvidia board, adds an echo effect to the input and then sends this output to the playback device connected to the Nvidia board. The algorithm models the echo effect by delaying the audio signal and adding it back. Feedback is added to the delay line to give a fading effect. The echo effect is implemented in the audioexample.Echo class.

    The echo algorithm has four parameters:

    • Gain - Linear gain of the delayed audio

    • Delay - Delay applied to audio signal, in seconds

    • FeedbackLevel - Feedback gain applied to delay line

    • WetDryMix - Ratio of wet signal added to dry signal

    type echoGenerator.m
    function echoGenerator(gain,feedbackLevel,delay,wetdrymix)
    % echoGenerator Entry-point function to deploy an echo generator to NVIDIA
    % hardware
    
    % Copyright 2023 The MathWorks, Inc.
    %#codegen
    
    hwobj = jetson();           % Create connection to Nvidia board.
    deviceName = "plughw:3,0";  % Change to device connected to your board.
    
    % Create capture and playback objects for audio processing on the Nvidia
    % board. 
    captureObj = audiocapture(hwobj,deviceName,"SampleRate",8000, ...
        "SamplesPerFrame",800);
    playbackObj = audioplayer(hwobj,deviceName,"SampleRate",8000);
    
    Fs = 8192;  % Sampling Frequency
    
    echoObj = audiopluginexample.Echo;
    setSampleRate(echoObj,Fs);
    
    for k = 1:3000
        % Capture audio input from the input device.
        input = capture(captureObj);
            
        echoSignal = zeros(size(double(input)),"like",double(input)); %#ok<PREALL>
        
        echoObj.Gain = gain;
        echoObj.FeedbackLevel = feedbackLevel;
        echoObj.Delay = delay;
        echoObj.WetDryMix = wetdrymix;
        
        [echoSignal] = process(echoObj,double(input));
        
        % Playback audio output using the output device output data needs to be
        % of type int16.
        play(playbackObj,int16(echoSignal));
    end
    end
    

    Generate C++ Code for the Jetson Board

    To generate a C++ executable that you can deploy on to an NVIDIA target, create a code configuration object for generating an executable.

    cfg = coder.config("exe");
    cfg.TargetLang = "C++";

    To create a configuration object for the Jetson platform and assign it to the Hardware property of the code configuration object cfg, use the coder.hardware function.

    cfg.Hardware = coder.hardware("NVIDIA Jetson");

    To specify the folder for performing remote build process on the target board, use the BuildDir property. If the specified build folder does not exist on the target board, then the software creates a folder with the given name. If no value is assigned to cfg.Hardware.BuildDir, the remote build process occurs in the last specified build folder. If there is no stored build folder value, the build process takes place in the home folder.

    cfg.Hardware.BuildDir = "~/remoteBuildDir";

    Set the GenerateExampleMain property to generate an example C++ main file and compile it. This example does not require modifications to the generated main files.

    cfg.GenerateExampleMain = "GenerateCodeAndCompile";

    To generate C++ code, use the codegen function and pass the code configuration and the size of the inputs for echoGenerator entry-point function. After the code generation takes place on the host, the generated files are copied over and built on the target.

    inputArgs = {coder.Constant(0.5),coder.Constant(0.35), ...
        coder.Constant(0.1),coder.Constant(0.5)};
    codegen("-config ",cfg,"echoGenerator","-args",inputArgs,"-report");
    Code generation successful: View report
    

    Run echoGenerator on Target Board

    To run the generated executable on the target board, use the MATLAB runApplication function.

    pid = runApplication(hwobj,"echoGenerator");
    ### Launching the executable on the target...
    Executable launched successfully with process ID 13545.
    Displaying the simple runtime log for the executable...
    
    Note: For the complete log, run the following command in the MATLAB command window:
    system(hwobj,'cat /home/ubuntu/remoteBuildDir/MATLAB_ws/R2023b/home/lnarasim/Documents/MATLAB/ExampleManager/lnarasim.Bdoc23b.j2313133.1/nvidia-ex27597808/echoGenerator.log')
    

    On successful deployment, hold the audio capture device close to your mouth and start speaking. You can hear the echo of your voice through the audio playback device.

    Terminate Application on Target Board

    To kill the executable launched:

    killApplication(hwobj,'echoGenerator')
    

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2021a