-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpectrographRenderer.cpp
More file actions
180 lines (137 loc) · 6.13 KB
/
SpectrographRenderer.cpp
File metadata and controls
180 lines (137 loc) · 6.13 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
==============================================================================
SpectrographRenderer.cpp
Created: 20 Apr 2023 11:41:10am
Author: Alex Mitchell
==============================================================================
*/
#include "SpectrographRenderer.h"
using namespace juce;
SpectrographRenderer::SpectrographRenderer(WaveFile::Ptr file, int blockSize) : waveFile(file), spectrographWindow(spectrographFFT.getSize() + 1, juce::dsp::WindowingFunction < float >::hann, false)
{
formatManager.registerBasicFormats();
waveFile = file;
spectroGraphBlockSize = blockSize * 2;
// read from file
auto activeReaderSource = file->CreateReaderSource(formatManager);
if (activeReaderSource)
{
formatReader = activeReaderSource->getAudioFormatReader();
int numChannels = formatReader->numChannels;
scopeData.setSize(numChannels, spectroGraphBlockSize);
}
auto totalNumSamples = formatReader->lengthInSamples;
numTimesToRunFFT = std::ceil(totalNumSamples / spectroGraphBlockSize);
complete = false;
}
int SpectrographRenderer::useTimeSlice()
{
auto activeReaderSource = waveFile->CreateReaderSource(formatManager);
if (activeReaderSource)
{
formatReader = activeReaderSource->getAudioFormatReader();
int numChannels = formatReader->numChannels;
scopeData.setSize(numChannels, spectroGraphBlockSize);
}
if (!complete)
{
doRender();
}
constexpr int timeUntilNextTimeslice = 0;
return timeUntilNextTimeslice;
}
void SpectrographRenderer::doRender()
{
int sampleIndex = 0;
auto totalNumSamples = formatReader->lengthInSamples;
auto numSamplesLeft = totalNumSamples;
const int numChannels = scopeData.getNumChannels();
numSamplesInBuffer = scopeData.getNumSamples();
int timesExecuted = 1;
// process one large FFT block size here (e.g. 4096 samples)....
int numSamplesToRead = spectroGraphBlockSize;
AudioBuffer<float> summedBuffer(1, scopeData.getNumSamples());
while(numSamplesLeft > 0)
{
if(numSamplesLeft < spectroGraphBlockSize)
{
numSamplesToRead = static_cast<int>(numSamplesLeft);
}
for(int channel = 0; channel < numChannels; channel++)
{
AudioBuffer<float> singleChannel(1, scopeData.getNumSamples());
singleChannel.copyFrom(0, 0, scopeData, channel, 0, numSamplesToRead);
// Read samples from wav file
formatReader->read(&singleChannel, 0, numSamplesToRead, sampleIndex, true, true);
summedBuffer.copyFrom(0, 0, singleChannel, 0, 0, numSamplesToRead);
}
auto summedBufferArray = summedBuffer.getReadPointer(0);
// render pixels to image
drawNextBlockOfSpectrogram(0, summedBufferArray, timesExecuted, numSamplesToRead);
sampleIndex += numSamplesToRead;
numSamplesLeft -= numSamplesToRead;
if(numSamplesLeft == 0)
{
endOfFile = true;
}
timesExecuted++;
}
// when file is run, end == true
if (endOfFile)
{
complete = true;
listeners.call([this](Listener& l) { l.OnComplete(); });
}
}
const float* SpectrographRenderer::ApplyFFT(const float* bufferData, const size_t numSamples, int channel)
{
unsigned long int UL = 1;
const int fftSize = spectrographFFT.getSize();
std::vector<float> fftBuffer(fftSize * (2 * UL));
// ...copy the frequency information from fftBuffer to the spectrum
std::memcpy(fftBuffer.data(), bufferData, fftSize * sizeof(float));
// Apply Windowing function
spectrographWindow.multiplyWithWindowingTable(fftBuffer.data(), fftSize);
// Spectrograms are created by extracting that frequency data from an audio signal. We are not modifying the data, so no need for an iFFT.
spectrographFFT.performFrequencyOnlyForwardTransform(fftBuffer.data());
std::memcpy(scopeData.getWritePointer(channel), fftBuffer.data(), fftSize * sizeof(float));
scopeData.copyFrom(channel, 0, fftBuffer.data(), static_cast<int>(numSamples));
return scopeData.getReadPointer(channel);
}
void SpectrographRenderer::drawNextBlockOfSpectrogram(int channel, const float *bufferChannel, int timesExecuted, int numSamplesToProcess)
{
auto imageHeight = spectrographImage.getHeight();
float imageWidth = spectrographImage.getWidth() - 1;
float widthFraction = numSamplesToProcess / numSamplesInBuffer;
float widthOfImageToCoverThisRun = float(imageWidth / numTimesToRunFFT);
widthToCover = std::ceil(widthOfImageToCoverThisRun * widthFraction);
// first, shuffle our image leftwards by 1 pixel..
spectrographImage.moveImageSection (0, 0, 1, 0, widthToCover, imageHeight);
// do fft
auto scopeDataChannel = ApplyFFT(bufferChannel, fftSize, channel);
// find the range of values produced, so we can scale our rendering to
// show up the detail clearly
auto maxLevel = juce::FloatVectorOperations::findMinAndMax (scopeData.getReadPointer(channel), fftSize / 2);
for (auto y = 1; y < imageHeight; ++y)
{
auto skewedProportionY = 1.0f - std::exp (std::log ((float) y / (float) imageHeight) * 0.2f);
auto scopeDataIndex = (size_t) juce::jlimit (0, (int) fftSize / 2, (int) (skewedProportionY * fftSize / 2));
auto level = juce::jmap (scopeDataChannel[scopeDataIndex], 0.0f, juce::jmax (maxLevel.getEnd(), 1e-5f), 0.0f, 1.0f);
auto color = juce::Colour::fromHSV (level, 1.0f, level, 1.0f);
if(currentXPosition != lastXPosition)
{
spectrographImage.setPixelAt (currentXPosition, y, color);
}
}
currentXPosition = lastXPosition;
lastXPosition += widthToCover;
}
void SpectrographRenderer::setImageSize(int newWidth, int newHeight)
{
auto imageType = SoftwareImageType();
spectrographImage = Image(juce::Image::RGB, newWidth, newHeight, true, imageType);
}
int SpectrographRenderer::getNumTimesToRunFFT()
{
return numTimesToRunFFT;
}