play
Play audio from audio device connected to Raspberry Pi
Add-On Required: This feature requires the MATLAB Support Package for Raspberry Pi Hardware add-on.
Syntax
Description
play(
plays audio from the audio output device connected to the Raspberry Pi® hardware. This function uses the Advanced Linux Sound Architecture
(ALSA) driver framework to read audio data.audioPlayObj
,audio
)
Simulating this function results in an error. The function is supported only for deployment.
Note
To play audio data with more than two channels, you must have Audio Toolbox™ license.
Input Arguments
audioPlayObj
— Connection between audio output device and Raspberry Pi Pi hardware
audioplayer object
Audio output device connection, specified as a audioplayer
object.
audio
— Audio data to be played
int16
Audio data to be played.
Examples
Capture and Play Audio Data
Create a connection from the MATLAB® to the Raspberry Pi board. If you encounter errors after running the above
command, try using additional arguments (as listed in raspi
) or refer to Troubleshoot Connecting Issues to Raspberry Pi Hardware.
Note
This example uses Audio Toolbox.
mypi = raspi;
Create a 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.
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
Limitations
MATLAB Connected I/O on the player
function is not
supported.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)