-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime_test.m
154 lines (121 loc) · 4 KB
/
realtime_test.m
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function realtime_test
%% AUDIO INPUT
% Choose input mode: either 'FILE' or 'DEVICE'
inputMode = 'FILE';
% Then make sure to check/set the parameters for that input mode
% in the AUDIO INPUT section.
% Number of samples to process with each loop iteration
% Note: If using ASIO, set output buffer size equal to this.
frameSize = 1024;
if strcmp(inputMode, 'FILE')
% Input mode 'FILE' : read audio from file
% Parameters
fileName = 'wonderwall_48k_32bit.flac';
file = strcat('audio_samples/', fileName);
nPlays = 1; % Number of times to play file
readRange = [1 inf]; % Range of samples to be read
% Create audio source
src = dsp.AudioFileReader(file, ...
'PlayCount', nPlays, ...
'SamplesPerFrame', frameSize, ...
'ReadRange', readRange);
elseif strcmp(inputMode, 'DEVICE')
% Input mode 'DEVICE' : read audio from device
% Parameters
sampleRate = 48000; % Samples/sec
inputDriver = 'ASIO'; % N/A for Mac/Linux
inputDevice = 'Analogue 1 + 2 (Focusrite Usb Audio)'; % List w/ getAudioDevices
chanMap = 1; % Which channel to use
% Create audio source
src = audioDeviceReader(sampleRate, ...
'Driver', inputDriver, ...
'Device', inputDevice, ...
'SamplesPerFrame', frameSize, ...
'ChannelMappingSource', 'Property', ...
'ChannelMapping', chanMap);
else
% Input mode not valid/selected
disp('Please set the input mode to either FILE or DEVICE')
end
%% AUDIO OUTPUT
% Parameters
outputDriver = 'ASIO'; % N/A for Mac/Linux
outputDevice = 'Focusrite USB ASIO'; % List w/ getAudioDevices
buffSize = frameSize; % Buffer size, must = frame size for ASIO
% Create audio sink
snk = audioDeviceWriter(...
'Driver', outputDriver, ...
'Device', outputDevice, ...
'SampleRate', src.SampleRate, ...
'SupportVariableSizeInput', true, ...
'BufferSize', buffSize);
% This might be all that's needed for built-in soundcards:
% snk = audioDeviceWriter(src.SampleRate);
%% SCOPES + VISUALIZATION
% Oscilloscope
scope = timescope(...
'SampleRate', src.SampleRate, ...
'BufferLength', src.SampleRate*4, ...
'YLimits', [-1, 1]);
% Spectrum Analyzer
spectrum = dsp.SpectrumAnalyzer(...
'NumInputPorts', 1, ...
'SpectrumType', 'Power', ...
'ViewType', 'Spectrum', ...
'SampleRate', src.SampleRate, ...
'PlotAsTwoSidedSpectrum', false, ...
'FrequencyScale', 'Log');
% Spectrogram
spectrogram = dsp.SpectrumAnalyzer(...
'NumInputPorts', 1, ...
'SpectrumType', 'Power', ...
'ViewType', 'Spectrogram', ...
'SampleRate', src.SampleRate, ...
'PlotAsTwoSidedSpectrum', false, ...
'FrequencyScale', 'Log');
%% FX BLOCKS
% Reverb (from example)
reverb = reverberator(...
'SampleRate', src.SampleRate, ...
'PreDelay', 0, ...
'WetDryMix', 0.4);
%% AUDIO STREAM LOOP
loopTime = 15; % (seconds), Used only for DEVICE mode
if strcmp(inputMode, 'FILE')
while ~isDone(src)
swag()
end
elseif strcmp(inputMode, 'DEVICE')
disp(strcat('Begin signal input... (', inputDevice, ...
", CH. ", num2str(chanMap), ')'))
tic
while toc < loopTime
swag()
end
disp('End signal input.')
end
%% RELEASE OBJECTS
release(src)
release(snk)
release(reverb)
release(scope)
release(spectrum)
release(spectrogram)
%% SPECTRAL WAVETABLE ANALOG/GUITAR ALGORITHM
function swag()
inputAudio = src();
% Algorithm goes here
%outputAudio = ammod(inputAudio, 300, src.SampleRate);
outputAudio = reverb(inputAudio);
snk(outputAudio);
% Optional visualization
% scope(mean(inputAudio, 2))
% scope(mean(outputAudio, 2))
% scope([mean(inputAudio, 2), mean(outputAudio, 2)])
% spectrum(mean(inputAudio, 2))
% spectrum(mean(outputAudio, 2))
% spectrum([mean(inputAudio, 2), mean(outputAudio, 2)])
% spectrogram(mean(inputAudio, 2))
spectrogram(mean(outputAudio, 2))
end
end