-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.c
109 lines (86 loc) · 2.72 KB
/
play.c
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
#include <ao/ao.h>
#include <mpg123.h>
#include <fftw3.h> // Inlcude the FFT
#include <iostream>
#include <fstream>
#include <math.h>
#define BITS 8
int main(int argc, char *argv[])
{
mpg123_handle *mh;
char *buffer;
size_t buffer_size;
size_t done;
int err;
int driver;
ao_device *dev;
ao_sample_format format;
int channels, encoding;
long rate;
if(argc < 2)
exit(0);
/* initializations */
ao_initialize();
driver = ao_default_driver_id();
mpg123_init();
mh = mpg123_new(NULL, &err);
buffer_size = mpg123_outblock(mh);
buffer = (char*)malloc(buffer_size * sizeof(unsigned char));
/* open the file and get the decoding format */
mpg123_open(mh, argv[1]);
mpg123_getformat(mh, &rate, &channels, &encoding);
/* set the output format and open the output device */
format.bits = mpg123_encsize(encoding) * BITS;
format.rate = rate;
format.channels = channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
dev = ao_open_live(driver, &format, NULL);
double time = 0;
// Initialise FFT
float *in;
fftwf_complex *out;
fftwf_plan p;
unsigned flag;
in = (float*) fftwf_malloc(sizeof(float) * buffer_size);
out = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * buffer_size);
//p = fftwf_plan_dft_r2c_1d(buffer_size, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
//p = fftwf_plan_dft_r2c_1d(buffer_size, in, out,flag);
p = fftwf_plan_dft_r2c_1d(buffer_size,in, out,flag);
/* decode and play */
while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK){
ao_play(dev, buffer, done);
time += 0.25*buffer_size/rate;
//mpg123_tellframe -- find current frame number
std::cout << mpg123_tellframe(mh) << std::endl;
// Copy buffer for FFT
for(int i=0;i<buffer_size/2;i++)
in[i] = buffer[i];
// Do the FFT
fftwf_execute(p);
// std::ofstream myfile;
// myfile.open ("fft_out.csv");
// for(int i=0;i<buffer_size/2;i++){
// //std::cout << 2.0f*sqrt(out[0][i]*out[0][i] + out[1][i]*out[1][i]) << " ";
// // Save FFT to a text file
// myfile << i << "," << 2.0f*sqrt(out[0][i]*out[0][i] + out[1][i]*out[1][i]) << "\n";
// }
// std:: cout << "rate=" << rate << " samples=" << buffer_size << std::endl;
// myfile.close();
// Try to do FFT on the buffer
//break;
}
//time += 0.25*buffer_size/rate;
std::cout << time << std::endl;
/* clean up */
fftwf_destroy_plan(p);
fftwf_free(out);
fftwf_free(in);
free(buffer);
ao_close(dev);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
ao_shutdown();
return 0;
}