-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (71 loc) · 2.53 KB
/
Copy pathmain.cpp
File metadata and controls
95 lines (71 loc) · 2.53 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
#define VOLK_IMPLEMENTATION
#define VMA_IMPLEMENTATION
#include "src/desu.h"
#include <portaudio.h>
#include <fftw3.h>
const int SAMPLE_RATE = 44100;
const int BUFFER_SIZE = SAMPLE_RATE/25;
const int NUM_CHANNELS = 2;
float* fft_input;
fftwf_complex* fft_output;
fftwf_plan fft_plan;
std::vector<glm::vec4> spectrum(BUFFER_SIZE);
int audioCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData) {
float* samples = (float*)inputBuffer;
//Right channel
for (size_t i = 0; i < BUFFER_SIZE; i++) {
fft_input[i] = samples[i*2];
}
fftwf_execute(fft_plan);
for (size_t i = 0; i < BUFFER_SIZE; i++) {
spectrum[i].x = fft_output[i][0];
spectrum[i].y = fft_output[i][1];
if(i > BUFFER_SIZE/2) {
spectrum[i].x = samples[i*2];
}
}
//Left channel
for (size_t i = 0; i < BUFFER_SIZE; i++) {
fft_input[i] = samples[i*2+1];
}
fftwf_execute(fft_plan);
for (size_t i = 0; i < BUFFER_SIZE; i++) {
spectrum[i].z = fft_output[i][0];
spectrum[i].w = fft_output[i][1];
if(i > BUFFER_SIZE/2) {
spectrum[i].z = samples[i*2+1];
}
}
Desu* desu = reinterpret_cast<Desu*>(userData);
vmaCopyMemoryToAllocation(desu->allocator, spectrum.data(), desu->spectrumStageAllocation, 0, sizeof(glm::vec4)*spectrum.size());
return paContinue;
}
int main(int argc, char* argv[]) {
std::cout << "Yeah, bitch!" << std::endl;
PaError err;
err = Pa_Initialize();
if (err != paNoError) return 1;
Desu desu;
desu.init();
fftwf_init_threads();
fftwf_plan_with_nthreads(16);
fft_input = (float*)fftwf_malloc(sizeof(float) * BUFFER_SIZE);
fft_output = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * BUFFER_SIZE);
fft_plan = fftwf_plan_dft_r2c_1d(BUFFER_SIZE, fft_input, fft_output, FFTW_MEASURE);
PaStream *stream;
err = Pa_OpenDefaultStream(&stream, NUM_CHANNELS, 0, paFloat32, SAMPLE_RATE, BUFFER_SIZE, audioCallback, &desu);
if (err != paNoError) return 1;
err = Pa_StartStream(stream);
if (err != paNoError) return 1;
desu.start();
err = Pa_StopStream(stream);
if (err != paNoError) return 1;
err = Pa_CloseStream(stream);
if (err != paNoError) return 1;
Pa_Terminate();
// Clean up FFT resources
fftwf_destroy_plan(fft_plan);
fftwf_free(fft_input);
fftwf_free(fft_output);
desu.destroy();
}