Can MATLAB play audio and then listen to and react depending on a certain response.

3 Ansichten (letzte 30 Tage)
Looking to cause a chain reaction based on a certain answer to question. The question should be a voice recording that is played. Simple variations of yes or no are expected response, then depending on answer will contact people or cancel. Interested in finding the best way to accomplish this.

Antworten (1)

Pratyush Swain
Pratyush Swain am 31 Jan. 2024
Hi Bekah,
I understand you want to play an audio in MATLAB which is to be followed by a response,and then the response has to be processed for deciding further course of actions.
The whole process here can be broken down into 3 parts:
1- Playing a recording containing a question.
2- Gathering response for the question through audio.
3- Processing the audio response and classifying it
Step 1: Playing a recording containing a question:
% Load the pre-recorded question audio file
questionFilename = 'recordedQuestion.wav'; % Replace with your question file
[playbackAudio, playbackFs] = audioread(questionFilename);
questionPlayer = audioplayer(playbackAudio, playbackFs);
% Play the question
disp("Playing the recorded question...");
playblocking(questionPlayer);
Step 2: Gathering response for the question through audio.
recFs = 44100; % Sample rate for recording
nBits = 16; % Number of bits per sample
nChannels = 1; % Number of audio channels (1 for mono, 2 for stereo)
% Create an audiorecorder object for the response
responseRecDuration = 2; % Duration to record the response (in seconds)
responseRecObj = audiorecorder(recFs, nBits, nChannels);
% Prompt the user to respond after the question has been played
disp("Please respond now.");
% Start recording the user's response
recordblocking(responseRecObj, responseRecDuration);
% Indicate that recording has stopped
disp("Thank you for your response.");
% Save the response audio
responseFilename = 'response.wav';
responseAudio = getaudiodata(responseRecObj);
audiowrite(responseFilename, responseAudio, recFs)
% Optional - Play the user's response again
[playbackAudio, playbackFs] = audioread(responseFilename);
responsePlayer = audioplayer(playbackAudio, playbackFs);
playblocking(responsePlayer);
Step 3- Processing the audio response and classifying it
We will be using a pre-trained model to perform speech command recognition.This is in reference with the example implementation in speech-command-recognition.It will be recognizing the following speech commands: yes, no, up, down, left, right, on, off, stop, and go, and to otherwise classify audio as an unknown word or as background noise.
% Load the pretrained models
load("commandNet.mat");
labels = trainedNet.Layers(end).Classes';
% Read Response Data
responseFilename="response.wav";
data=audioread(responseFilename);
audioData = {data,length(data)}
%Using the function extractAuditorySpectrogram to extract the spectrogram and classify the audio based on its auditory spectrogram.
auditorySpectrogram = extractAuditorySpectrogram(audioData{1},audioData{2});
prediction = classify(trainedNet,auditorySpectrogram); % Gather prediction
Please note the implementation of the supporting function "extractAuditorySpectrogram" is performed in the example reference mentioned above. There is an option to open the example directly in MATLAB. The "commandNet" file is also present in the example directory.
This is a sample implementation, but you can also train your own custom speech recognition model by referring to https://www.mathworks.com/help/audio/ug/train-speech-command-recognition-model-using-deep-learning.html
Once the "prediction" is obtained, its value can be utilized to perform conditional actions as required in your usecase.
For more information on performing operation on audio files on MATLAB, please refer to:
Hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by