Audio I/O Demo 2 – Recording and playing back microphone input

This demo records in real-time the audio from the microphone and plays it with a given delay (the minimum possible delay being 1 ms at 48 000 Samples per seconds (48 kSPS) or 0.5 ms at 96 kSPS). The minimum delay possible is defined by 2 audio samples.

Note

You can only play audio on the full version of a device; the lite version does not have audio playback capabilities.

This demo is done in precise steps:

  • Compute feedback delay

  • Configure audio acquisition

Datapixx('SetMicrophoneSource', 1, 100); Here the 1 is the source of the audio input: 1 signifies microphone, 2 would be Audio In. The 100 is the gain for the microphone, which is always a value between 1 and 1000.

  • Configure record buffer

Datapixx('SetMicrophoneSchedule', 0, sampleRate, 0, 3, 0, 20e6); No delay on the Microphone Schedule (first zero), we sample at sampleRate, 2nd zero zero signifies we are continuously acquiring data, the 3 signifies we are in stereo mode, the third zero is the base address we will use for this and, we will use a 20 megabytes buffer (20e6)

  • Set up audio playback schedule

Datapixx('SetAudioSchedule', feedbackDelay, sampleRate, 0, 3, 0, 20e6); Here, instead of zero we have a feedbackDelay as explained before. We start at the same place as the Microphone schedule such that we playback the same audio that was recorded with the microphone, in the same mode.

  • Start record and playback schedules at the same time, but playback has the feedback delay before it starts.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function DatapixxAudioFeedbackDemo(feedbackDelay)
% DatapixxAudioFeedbackDemo([feedbackDelay=1])
%
% Records audio from the microphone input,
% and simultaneously plays back the recording on the audio outputs.
% For ultimate precision, this demo takes into consideration both the audio input
% group delay, and the audio output group delay. The DATAPixx is capable of
% implementing mouth-to-ear feedback delays of under 1 millisecond at 48 kSPS,
% and under half a millisecond at 96 kSPS.
%
% Optional argument:
%
% feedbackDelay is the delay in seconds between when sound is recorded from the
% microphone, and when the same sound appears at the audio outputs.
%
% Also see: DatapixxMicrophoneDemo
%
% History:
%
% Nov 1, 2009  paa     Written
% Oct 29, 2014 dml     Revised 

AssertOpenGL;   % We use PTB-3

% OS-independant keyCodes
KbName('UnifyKeyNames');
Escape = KbName('ESCAPE');

% Open DATAPixx, and stop any schedules which might already be running
Datapixx('Open');
Datapixx('StopAllSchedules');
Datapixx('InitAudio');
Datapixx('RegWrRd');    % Synchronize DATAPixx registers to local register cache

sampleRate = 96000;     % Use highest rate to permit smallest possible feedback delays

% Get feedback delay argument
if nargin < 1
    feedbackDelay = [];
end
if isempty(feedbackDelay)
    feedbackDelay = 1;
end

% We'll subtract out the CODEC input and output group delays to implement the "real-world" feedback delay.
inputDelay = Datapixx('GetMicrophoneGroupDelay', sampleRate);
outputDelay = Datapixx('GetAudioGroupDelay', sampleRate);
feedbackDelay = feedbackDelay - inputDelay - outputDelay;

% We'll require a minimum delay of 2 audio samples,
% just to ensure that the audio input schedule can write its datum to DRAM
% before the audio output schedule tries to read that location.
minimumDelay = 2 / sampleRate;
if (feedbackDelay < minimumDelay)
    feedbackDelay = minimumDelay;
    fprintf('Clamping feedback delay to minimum value of %d microseconds\n', (minimumDelay + inputDelay + outputDelay) * 1e6);
end;

% Configure audio acquisition
Datapixx('SetMicrophoneSource', 1, 100);    % Always record from microphone
Datapixx('DisableAudioLoopback');
Datapixx('RegWrRd');

% We'll record into address 0, with a 20 megabyte buffer for up to 100 seconds of feedbackDelay at 48kSPS.
Datapixx('SetMicrophoneSchedule', 0, sampleRate, 0, 3, 0, 20e6);
Datapixx('StartMicrophoneSchedule');

% We'll playback from the same buffer, but with a schedule onset delay.
Datapixx('SetAudioVolume', 0.25);    % Not too loud
Datapixx('SetAudioSchedule', feedbackDelay, sampleRate, 0, 3, 0, 20e6);
Datapixx('StartAudioSchedule');

% Start both schedules at exactly the same time.
% The microphone will record its first sample immediately,
% but the audio will only playback its first sample after feedbackDelay.
Datapixx('RegWrRd');

% When user hits a key, we'll stop the feedback.
% Note that this feedback is all being done in hardware.
% No realtime software overhead. No glitches. You could even quit MATLAB.
fprintf('\nAudio feedback has begun\n');

HitKeyToContinue('Hit any key to terminate...');
Datapixx('StopMicrophoneSchedule');
Datapixx('StopAudioSchedule');
Datapixx('RegWrRd');

% Job done
Datapixx('Close');
fprintf('\nDemo completed\n\n');