audiocapture

Connection between audio input device and Raspberry Pi hardware

Description

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

Creation

Description

example

audioCaptureObj = audiocapture(mypi,DeviceName) creates an object that represents the connection to the audio input device, DeviceName, connected to the Raspberry Pi hardware, mypi. The Raspberry Pi hardware is represented by a raspi object.

example

audioCaptureObj = audiocapture(mypi,DeviceName,Name,Value) sets Properties using name-value pairs. For example, audioCaptureObj = audiocapture(mypi,'plughw:1,0','SampleRate',44100) reads audio data at a sample rate of 44100. You can specify multiple name-value pairs. Enclose each property name in single quotes.

Input Arguments

expand all

Connection to a specific Raspberry Pi hardware board, specified as a raspi 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 as described in List Available ALSA Audio Input Devices.

Data Types: string

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 as described in List Available ALSA Audio Input Devices.

Example:

>> audioCaptureObj.DeviceName
ans = 
      plughw:1,0

Data Types: string

This property is read-only.

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 as described in List Available ALSA Audio Input Devices.

Example:

>> audioCaptureObj.SampleRate
ans = 
      44100 

Data Types: double

This property is read-only.

The number of samples per audio channel.

Example:

>> audioCaptureObj.SamplesPerFrame
ans = 
      4410 

Data Types: double

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 as described in List Available ALSA Audio Input Devices.

Example:

>> audioCaptureObj.NumberOfChannels
ans = 
      2 

Data Types: double

Object Functions

captureCapture data from audio device connected to Raspberry Pi

Examples

collapse all

Create a Raspberry Pi™ connection.

mypi = raspi
mypi = 
  raspi with properties:

         DeviceAddress: '172.18.182.234'              
                  Port: 18734                         
             BoardName: 'Raspberry Pi 4 Model B'      
         AvailableLEDs: {'led0'}                      
  AvailableDigitalPins: [2,3,4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
  AvailableSPIChannels: {'CE0','CE1'}                 
     AvailableI2CBuses: {}                            
      AvailableWebcams: {'Microsoft® LifeCam Cinema(TM): (usb-0000:01:00.0-1.1)'}
           I2CBusSpeed:                               

  Supported peripherals

Open the raspi_pitchshiftdeployment() function. In this function, connections to the audio input and output devices are created using the audiocapture and audioplayer objects. Modify the properties of the audio objects to match the properties of the device that you are using.

type raspi_pitchshiftdeployment
function raspi_pitchshiftdeployment()
% This function is used to deploy an audio processing application to a
% Raspberry Pi. The specific application of interest is pitch-shifting.
% This function takes audio input from a device connected to the Raspberry
% Pi, pitch-shifts this audio input to the desired pitch and then sends
% this output to the playback device connected to the Raspberry Pi

%#codegen
% Copyright 2019 The MathWorks, Inc.

% Create capture and playback system objects for audio processing on
% Raspberry Pi Hardware.

r = raspi();
captureObj = audiocapture(r,'plughw:2,0','SampleRate', 48000, 'SamplesPerFrame', 4800);

playbackObj = audioplayer(r,'plughw:2,0', 'SampleRate', 48000);

% Settings for pitch shifting operation
pitch = -5;         % Pitch shift in semi-tones
overlap = 0.2;      % Overlap of delay line
Fs = 8192;          % Sampling Frequency


pitchShifter = audiopluginexample.PitchShifter('PitchShift',8,'Overlap',0.3);
setSampleRate(pitchShifter,Fs);

for k = 1:3000
    % capture audio input from the input device
    input = capture(captureObj);
    
    % pitch shift the audio input.
    % input is of type int16 and needs to be converted to type double
    % before processing the data. This is because the function shiftPitch
    % expects all its inputs to be of the same data type which in this case
    % is type double
    
    pitchShifted = zeros(size(double(input)),'like',double(input)); %#ok<PREALL>
    
    pitchShifter.PitchShift = pitch;
    pitchShifter.Overlap = overlap;
    
    [pitchShifted] = pitchShifter(double(input));
    
    % playback audio output using the output device
    % output data needs to be of type int16 and thus pitchShifted
    % which is a double is cast to be of type int16 before being sent to
    % the device
    play(playbackObj,int16(pitchShifted));
end
end

Create a Raspberry Pi configuration object, board.

board = targetHardware('Raspberry Pi')
board = 
targetHardware with properties:

               Name: 'Raspberry Pi'
      DeviceAddress: '172.18.182.234'
           Username: 'pi'
           Password: '*********'
           BuildDir: '/home/pi'
    EnableRunOnBoot: 0
        BuildAction: 'Build, load, and run'
        CoderConfig: [1×1 coder.CodeConfig]

Deploy the raspi_pitchshiftdeployment() function on Raspberry Pi using the configuration object, board. On successful deployment, hold the audio capture device close to your mouth and start speaking. You can hear your pitch shifted voice through the audio playback device.

deploy(board,'raspi_pitchshiftdeployment')
Code generation successful: View report

Introduced in R2019b