-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.cpp
More file actions
115 lines (83 loc) · 2.67 KB
/
sound.cpp
File metadata and controls
115 lines (83 loc) · 2.67 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
#include <iostream>
#include "wav.h"
#include <portaudio.h>
#include <unistd.h>
#include <math.h>
using namespace std;
char * name;
/**int integer_value(char * bytes, bool little_endian = true) {
int rest = 0;
if (little_endian) {
for (int n = sizeof(int); n >= 0, --n) {
rest = (rest << 8) + bytes[n];
}
} else {
}
}*/
float hanning_window(short input, size_t pos, size_t size) {
float i = (float)pos;
float n = (float)size;
return input * 0.5f * (1.0f - cos(2.0f * M_PI * i / (n - 1.0f)));
}
void read() {
Wav file(name);
/*ifstream ifs("/Users/molayab/Desktop/La Boda.wav", ios::in | ios::binary);
wav_header header;
ifs.read(header.chunk_id, 4);
ifs.seekg(0, 4);
ifs.read((char *) &header.chunk_size, 4);
printf("%d\n", header.chunk_size);*/
wav_header * h = file.get_header();
cout << " >> File Read:" << endl;
cout << " > Container -> " << h->chunk_id << endl;
cout << " > Format -> " << h->format << endl;
cout << " > Audio Format (1 = PCM) -> " << h->audio_format << endl;
cout << " > Bits Per Sample -> " << h->bits_sample << endl;
cout << " > Channels -> " << h->channels << endl;
cout << " > Byte rate -> " << h->byte_rate << endl;
cout << " > Sample rate -> " << h->sample_rate << endl;
cout << " >> Reading samples..." << endl;
Pa_Initialize();
PaStreamParameters p;
p.device = Pa_GetDefaultOutputDevice();
p.channelCount = (int) h->channels;
switch(h->bits_sample) {
case 8: p.sampleFormat = paUInt8; break;
case 16: p.sampleFormat = paInt16; break;
case 32: p.sampleFormat = paInt32; break;
default: break;
}
p.suggestedLatency = Pa_GetDeviceInfo(p.device)->defaultHighOutputLatency;
p.hostApiSpecificStreamInfo = NULL;
PaStream * stream;
Pa_OpenStream(&stream, NULL, &p, (double)h->sample_rate, paFramesPerBufferUnspecified, 0, NULL, NULL);
Pa_StartStream(stream);
unsigned int samples = 4096;
size_t sample_len = samples * (h->bits_sample / 8) * h->channels;
// Buffer
uint8_t * buffer = new uint8_t[sample_len];
size_t pointer = 0;
for(;;) {
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
// Leer de archivo el buffer y hacer seek al siguiente data.
size_t n = fread(buffer, sizeof(uint8_t), sample_len, file.stream);
// Escribir en HW el buffer y los samples a reproducir.
Pa_WriteStream(stream, buffer, samples);
pointer += n;
cout << "Read: " << pointer << " bytes";
if (n < sample_len) {
break;
}
}
delete[] buffer;
Pa_CloseStream(stream);
}
int main(int argc, char ** argv) {
if (argc < 2) {
perror("No se especifico el archivo");
return 1;
}
name = argv[1];
read();
return 0;
}