% read/WriteAudioVoice.m
%copyright 2020 by Thomas P. Weldon
% all rights reserved
% This script requires the AudioToolBox https://www.mathworks.com/products/audio.html
%reads5-second audio from built-in mic
% writes recording to built-in speakers, looping twice
% using the built-in mic and speaker on a computer should work best
% after using internal, you may try bluetooth devices (more delay)
%see https://www.mathworks.com/help/audio/gs/real-time-audio-in-matlab.html
%see https://www.mathworks.com/help/audio/gs/audio-io-buffering-latency-and-throughput.html
%see https://www.mathworks.com/help/audio/gs/audio-input-and-audio-output.html
%see https://www.mathworks.com/help/audio/ref/audioplayerrecorder.getaudiodevices.html
%see https://www.mathworks.com/help/audio/ref/audiodevicewriter-system-object.html#d120e46033
clear all
close all hidden
%microphone input initialization ===============================
deviceReader = audioDeviceReader; % device to red audio
micdevices = getAudioDevices(deviceReader) %prints what input devices are available
%deviceReader=audioDeviceReader(44100,'Device','Built-in Microphone')
%deviceReader.Device='Built-in Microphone';
deviceReader.Device='Default'; % try this if you have input problems
deviceReader.SampleRate=44100;
deviceReader.SamplesPerFrame=1024*4*10;
%speaker output initialization ===============================
deviceWriter = audioDeviceWriter;
spkrdevices = getAudioDevices(deviceWriter)
%audioDeviceWriter=audioDeviceWriter(44100,'Device','Built-in Output')
%audioDeviceWriter=audioDeviceWriter(44100,'Device','Default', 'SampleRate',fileReader.SampleRate)
%deviceWriter.Device='Built-in Output';
deviceWriter.Device='Default'; % try this if you have output problems
deviceWriter.SampleRate=deviceReader.SampleRate; %set output same as input rate
fig = uifigure;
selection = uiconfirm(fig,'Click OK to record 5 second audio','Confirm Close','CloseFcn',@(h,e) close(fig));
if selection(1)=='C' clear all; close all hidden; return; end
%read in 5 one-second intervals from input
sig1 = deviceReader();
sig2 = deviceReader();
sig3 = deviceReader();
sig4 = deviceReader();
sig5 = deviceReader();
%write 5 one-second intervals to input (looping twice)
for n2=1:2
deviceWriter(sig1);
deviceWriter(sig2);
deviceWriter(sig3);
deviceWriter(sig4);
deviceWriter(sig5);
pause(5)
end
pause(2)
release(deviceReader)
release(deviceWriter)
%finally plot it on an oscilloscope
scope = dsp.TimeScope( ...
'SampleRate',deviceReader.SampleRate, ...
'TimeSpan',5, ...
'BufferLength',deviceReader.SampleRate*10, ...
'YLimits',[-1,1], ...
'TimeSpanOverrunAction',"Scroll");
scope(sig1);
scope(sig2);
scope(sig3);
scope(sig4);
scope(sig5);
%at the very end, print out your audio devices
disp('Audio input devices on your machine:')
micdevices
disp('Audio output devices on your machine:')
spkrdevices
Copyright 2020 T. Weldon