-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaveOut.cpp
567 lines (476 loc) · 17.7 KB
/
waveOut.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#include <mmsystem.h>
#include <fstream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <iostream>
#include<vector>
#include <algorithm>
#include <ctime>
#include <chrono>
#include <array>
#include "MiniBpm.h"
#include <fftw3.h>
#include "filter.cpp"
#include "Keys.h"
#include "EFFECTS.h"
#include "Chunk.h"
#include "Util.h"
#include "HighQuality.h"
#include "GLOBAL.h"
#include "MidiMaker.h"
#include <iomanip>
#pragma comment(lib,"Winmm.lib")
using namespace std;
struct WAVE_HEADER
{
char Chunk[4];
int ChunkSize;
char format[4];
char Sub_chunk1ID[4];
int Sub_chunk1Size;
short int AudioFormat;
short int NumChannels;
int SampleRate;
int ByteRate;
short int BlockAlign;
short int BitsPerSample;
char Sub_chunk2ID[4];
int Sub_chunk2Size;
};
struct LIST
{
char ChunkID[4];
int ChunkSize;
char ListTypeID[4];
};
static int BPM = 151;
static vector<short int> getData(string file)
{
vector<short int> data;
struct WAVE_HEADER waveheader;
FILE* sound;
bool foundList = false;
sound = fopen(file.c_str(), "rb");
short int D;
fread(&waveheader, sizeof(waveheader), 1, sound);
cout << "Chunk: " << waveheader.Chunk << endl;
cout << "Chunk Size: " << waveheader.ChunkSize << endl;
cout << "Format: " << waveheader.format << endl;
cout << "SubChunk1ID: " << waveheader.Sub_chunk1ID << endl;
cout << "SubChunk1Size: " << waveheader.Sub_chunk1Size << endl;
cout << "Audio Format: " << waveheader.AudioFormat << endl;
cout << "Num of Channels: " << waveheader.NumChannels << endl;
cout << "SampleRate: " << waveheader.SampleRate << endl;
cout << "ByteRate: " << waveheader.ByteRate << endl;
cout << "Block Align: " << waveheader.BlockAlign << endl;
cout << "Bits Per Sample:" << waveheader.BitsPerSample << endl;
cout << "SubChunk2ID: " << waveheader.Sub_chunk2ID << endl;
cout << "SubChunk2Size: " << waveheader.Sub_chunk2Size << endl;
cout << "sizeof " << sizeof(waveheader.BitsPerSample) << " reg " << waveheader.BitsPerSample << endl;
wstring temp = wstring(file.begin(), file.end());
LPCWSTR ws = temp.c_str();
HANDLE h = CreateFile(ws, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); //get Size
int size = GetFileSize(h,NULL);
char lol[4];
int location=0;
ifstream read;
read.open(file);
read.seekg(0, read.beg);
for (int i = 0;i < size;i++)
{
try
{
read.seekg(i);
read.read(reinterpret_cast<char*>(&lol), sizeof(lol));
if (lol[0] == 'L' && lol[1] == 'I' && lol[2] == 'S' && lol[3] == 'T')
{
location = i;
foundList = true;
cout << "FOUND LIST AT: " << i << endl;
break;
}
}
catch (exception e)
{
}
}
cout << "Finished looking for LIST"<<endl;
int listSize=0;
int positon = 0;
if (foundList)
{
read.seekg(location+4);
read.read(reinterpret_cast<char*>(&listSize), sizeof(listSize));
cout << listSize << endl;
read.seekg(location + 8 + listSize);
positon = location + 8 + listSize;
}
else
{
positon = 44;
}
fseek(sound, positon, SEEK_SET);
std::chrono::system_clock::time_point now1 = std::chrono::system_clock::now();
/*read.seekg(0, read.beg);
location = 0;
for (int i = 0;i < size;i++)
{
try
{
read.seekg(i);
read.read(reinterpret_cast<char*>(&lol), sizeof(lol));
if (lol[0] == 'D' && lol[1] == 'A' && lol[2] == 'T' && lol[3] == 'A')
{
cout << "FOUND DATA at "<<i<<endl;
location = i;
break;
}
}
catch (exception e)
{
}
}
cout << "Finished finding DATA" << endl;
read.seekg(location + 4);
int sizeofdata = 0;
read.read(reinterpret_cast<char*>(&sizeofdata), 4);
char* datas = (char*)&data[0];
read.read(datas, sizeofdata);*/
while (!feof(sound))
{
fread(&D, waveheader.BitsPerSample/8, 1, sound);
data.push_back(D);
}
std::chrono::system_clock::time_point now2 = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed = now2 - now1;
cout << "Took " << elapsed.count() << " seconds" << endl;
cout << "Finished populating vector" << endl;
return data;
}
void writeAudioBlock(HWAVEOUT hWaveOut, vector<short int> block, DWORD size)
{
WAVEHDR header;
ZeroMemory(&header, sizeof(header));
header.dwBufferLength = size;
header.lpData = (LPSTR)&block[0];
MMRESULT jk = waveOutPrepareHeader(hWaveOut, &header, sizeof(WAVEHDR));
cout << "JK IS " << jk << endl;
MMRESULT lol = waveOutWrite(hWaveOut, &header, sizeof(WAVEHDR));
cout << "lol is " << lol << endl;
Sleep(500);
while (waveOutUnprepareHeader(hWaveOut, &header, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING)
{
Sleep(100);
}
}
LPSTR loadAudioBlock(string filename, DWORD* blockSize)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
DWORD size = 0;
DWORD readBytes = 0;
void* block = NULL;
wstring temp = wstring(filename.begin(), filename.end());
LPCWSTR ws = temp.c_str();
if ((hFile=CreateFile(ws, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
{
return NULL;
}
do
{
if ((size=GetFileSize(hFile, NULL)) == 0)
{
break;
}
cout <<"FILE SIZE RAW " << size << endl;
if ((block=HeapAlloc(GetProcessHeap(), 0, size)) == NULL)
{
break;
}
ReadFile(hFile, block, size, &readBytes, NULL);
} while (0);
CloseHandle(hFile);
*blockSize = size;
return (LPSTR)block;
}
static WAVE_HEADER getHDR(string path)
{
struct WAVE_HEADER hdr;
FILE* l;
l = fopen(path.c_str(), "rb");
short D;
fread(&hdr, sizeof(hdr), 1, l);
return hdr;
}
pair<vector<short int>, vector<short int>> LeftRight(vector<short int> origin)
{
cout << "Size of samples:" << origin.size() << endl;
vector<short int> left;
vector<short int> right;
int size = origin.size();
for (int i = 0;i < size;i++)
{
if (i % 2 == 0)
{
left.push_back(origin[i]);
}
else
{
right.push_back(origin[i]);
}
}
cout << "Size of left: " << left.size()<<endl;
cout << "Size of right: " << right.size()<<endl;
return make_pair(left, right);
}
vector<short int> Consolidate(vector<short int> left,vector<short int> right)
{
vector<short int> consolidated;
int size = (left.size() + right.size())/2;
for (int i = 0;i < size;i++)
{
consolidated.push_back((left[i] + right[i]) / 2);
}
cout <<"SIZE "<< consolidated.size() << endl;
return consolidated;
}
vector<short> Stereoize(vector<short> left, vector<short> right)
{
size_t num_samples = min(left.size(), right.size());
std::vector<short> interleaved(num_samples * 2);
for (size_t i = 0; i < num_samples; i++) {
interleaved[2 * i] = left[i];
interleaved[2 * i + 1] = right[i];
}
return interleaved;
}
int main(int argc, char* argv[])
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
std::cout << " ________ ___ ___ ________ ___ ________ _________ ________ _____ ______ ___ ________ ___ \n";
std::cout << "|\\ __ \\|\\ \\|\\ \\|\\ ___ \\|\\ \\|\\ __ \\ |\\___ ___\\\\ __ \\ |\\ _ \\ _ \\|\\ \\|\\ ___ \\|\\ \\ \n";
std::cout << "\\ \\ \\|\\ \\ \\ \\\\\\ \\ \\ \\_|\\ \\ \\ \\ \\ \\|\\ \\ \\|___ \\ \\_\\ \\ \\|\\ \\ \\ \\ \\\\\\__\\ \\ \\ \\ \\ \\ \\_|\ \\ \\ \\ \n";
std::cout << " \\ \\ __ \\ \\ \\\\\\ \\ \\ \\ \\\\ \\ \\ \\ \\ \\\\\\ \\ \\ \\ \\ \\ \\ \\\\\ \\ \\ \\ \\\\|__| \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \n";
std::cout << " \\ \\ \\ \\ \\ \\ \\\\\\ \\ \\ \\_\\\\ \\ \\ \\ \\ \\\\\\ \\ \\ \\ \\ \\ \\ \\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\_\\ \\ \\ \\ \n";
std::cout << " \\ \\__\\ \\__\\ \\_______\\ \\_______\\ \\__\\ \\_______\\ \\ \\__\\ \\ \\_______\\ \\ \\__\\ \\ \\__\\ \\__\\ \\_______\\ \\__\\\n";
std::cout << " \\|__|\\|__|\\|_______|\\|_______|\\|__|\\|_______| \\|__| \\|_______| \\|__| \\|__|\\|__|\\|_______|\\|__|\n";
SetConsoleTextAttribute(hConsole,7);
std::chrono::system_clock::time_point now1 = std::chrono::system_clock::now();
//C:/Users/winga/Music
string file = "Test/nightdrive.wav";
Key SONG_KEY = Key::F_SHARP_MAJOR;
GLOBAL::MUSICAL_KEY = SONG_KEY;
GLOBAL::isMonophonic = true; //WORK ON THIS NEXT ------------------------------------------------------------------------------------------ L()()K
cout << file << endl;
cout << "BPM: " << BPM << endl;
cout << "Song Key: " << Util::getEnumString(SONG_KEY) << endl;
HWAVEOUT hWaveOut;
LPSTR block;
DWORD blockSize;
WAVE_HEADER wav = getHDR(file);
WAVEFORMATEX format =
{
WAVE_FORMAT_PCM, //FORMAT
2, //CHANNELS
wav.SampleRate/1.0, //SAMPLE RATE
wav.ByteRate, //AVG BYTES PER SEC
wav.BlockAlign, //BLOCK ALIGN
wav.BitsPerSample, //BITS PER SAMPLE
0 //CBSIZE
};
MMRESULT m;
if ((m=waveOutOpen(&hWaveOut, WAVE_MAPPER, &format, 0, 0, CALLBACK_NULL)) != MMSYSERR_NOERROR)
{
printf("ERROR");
cout << m << endl;
ExitProcess(0);
}
printf("The Wave Mapper devicce was loaded\n");
vector<short int> pcmData = getData(file);
int pcmSize = sizeof(short int) * pcmData.size();
cout << "PCMDATA SIZE " << pcmSize <<endl;
blockSize = pcmSize;
printf("LOADED BLOCK\n");
vector<long double> coefficients = filter::yLcalculate_high_pass_filter_coefficients(wav.SampleRate,1000,1000 ); //crappy filter or coefficients after like 500 hz there is crackling; 550hz only one instance of fucked up audio
// 600hz kicks up the shit audio & 700hz nails the coffin, num_taps does not fix this at all // reason gives
pair<vector<short>, vector<short>> dat1 = LeftRight(pcmData); // cracks even at 500 hz at kicks
vector<short> left = dat1.first;
vector<short> right = dat1.second;
vector<double> leftD = filter::short_to_double(left);
vector<double> rightD = filter::short_to_double(right);
cout << "Finished oh yea \n";
vector<short int> data = Stereoize(left, right);
cout<<"Max Value is : " << *max_element(data.begin(), data.end()) << endl;
//writeAudioBlock(hWaveOut, data, blockSize);
//waveOutClose(hWaveOut);
//HighQuality l("Test/nightdrive.wav");
//l.Init();
int cuttoff_f = 200;
vector<short> leftLowPass = dat1.first;
vector<short> rightLowPass = dat1.second;
filter::lowPassFFTW_HannWindow(leftLowPass, rightLowPass, wav.SampleRate, cuttoff_f);
vector<short int> lowPassDat = Stereoize(leftLowPass, rightLowPass);
leftLowPass.clear();
rightLowPass.clear();
leftLowPass.shrink_to_fit();
rightLowPass.shrink_to_fit();
vector<short> leftHighPass = dat1.first;
vector<short> rightHighPass = dat1.second;
filter::highPassFFTW(leftHighPass, rightHighPass, wav.SampleRate, cuttoff_f);
vector<short int> highPassDat = Stereoize(leftHighPass, rightHighPass);
leftHighPass.clear();
rightHighPass.clear();
leftHighPass.shrink_to_fit();
rightHighPass.shrink_to_fit();
/////////////////////////LOW PASS CONVOLUTION////////////////////////////
vector<short> properL = dat1.first;
vector<short> properR = dat1.second;
filter::yLapply_high_pass_filter(properL, properR, coefficients);
vector<short> convolutionData = Stereoize(properL, properR);
//////////////////////////////////////////////////////////////////////////
/////////////////////EFFECT SIDECHAIN////////////////////////////////////
float twoBeatDuration = (1 / (BPM / 60.0)) / 0.5;
float qBeatDuration = (1.0 / (BPM / 60.0)) / 4.0;
GLOBAL::qBeatDuration = qBeatDuration;
GLOBAL::sampleRate = wav.SampleRate;
GLOBAL::twoBeatDuration = twoBeatDuration;
int sampleSizeQuart = qBeatDuration * wav.SampleRate;
int sampleSizeTwo = twoBeatDuration * wav.SampleRate;
//LOW PASS HAS MORE SAMPLES FOR FINER RES; HIGH PASS HAS LESS SAMPLES CUZ LESS RES NEEDED
int numOfChunkQuart = highPassDat.size() / sampleSizeQuart;
int numOfChunkTwo = lowPassDat.size() / sampleSizeTwo;
vector<double> lowPassDatDouble(lowPassDat.begin(), lowPassDat.end());
vector<double> highPassDatDouble(highPassDat.begin(), highPassDat.end());
vector<vector<double>> sampleChunkQuart;
vector<vector<double>> sampleChunkTwo;
pair<vector<short int>, vector<short int>> dat = LeftRight(data);//use pcmData for unfiltered fft, data for filtered fft
vector<short int> preProcData = Consolidate(dat.first, dat.second);
vector<double> audiodata(preProcData.begin(), preProcData.end());
cout << "BPM: " << BPM << endl;
cout << qBeatDuration<<endl;
int sampleSize = qBeatDuration * (wav.SampleRate/2);
int numOfChunks = audiodata.size() /( sampleSize*2);
cout <<"THIS IS SAMPLESIZE MAIN FUNC " << sampleSize << endl;
cout << "Length of Audio is " << numOfChunks * qBeatDuration << " seconds \n";
GLOBAL::SONG_LENGTH = numOfChunks * qBeatDuration;
vector<vector<double>> sampleChunks;
int inputSize = 1024;//4096 wont work; possible error in how the output data is being stored
int outputSize = (inputSize / 2) + 1;
int flags = FFTW_ESTIMATE;
cout << "HELLO THIS IS NUMOFCHUNKS MAIN FUNC " <<numOfChunks <<endl;
cout <<"THIS IS AUDIODATA SIZE MAIN FUNC " << audiodata.size() << endl;
cout << "THIS IS qBEAT MAIN " << std::setprecision(15) <<qBeatDuration << endl;
sampleChunks.resize(numOfChunks);
int N = 10;
vector<Chunk> chunkData;
cout << "starting chunk haha" << endl;
pair<vector<short int>, vector<short int>> lowP = LeftRight(lowPassDat);
pair<vector<short int>, vector<short int>> highP = LeftRight(highPassDat);
cout << "THIS IS LOWPASS DAT SIZE: " << lowPassDat.size() << endl;
vector<short int> lowPP = Consolidate(lowP.first, lowP.second);
vector<short int> highPP = Consolidate(highP.first, highP.second);
cout << "THIS IS LOWPP SIZE: " << lowPP.size()<<endl;
vector<Chunk> cDat = MidiMaker::lowPass(lowPP);
vector<Chunk> midPass = MidiMaker::highPass(highPP); //I THINK CHUNKS ARENT BEING DONE PROPERLY TIMING IS WRONG
std::cout << "This is chunk seperation time: " << GLOBAL::twoBeatDuration << "s" << endl;
for (Chunk c : cDat)
{
vector<Keys> p = c.getKeyVec();
vector<double> inten = c.getIntenVec();
for (int i = 0;i<p.size();i++)
{
cout << "Low Pass iteration: "<< c.getIter()<<" TIME: "<<c.getStart() <<" to "<<c.getEnd() <<" Intensity: "<< 20*log(inten[i]/32768) << " Key: " << Util::getEnumString(p[i]) << endl;
}
}
cout << endl;
for (Chunk q : midPass)
{
vector<Keys> p = q.getKeyVec();
vector<double> inten = q.getIntenVec();
vector<double> freqvec = q.getFreqV();
for (int i = 0;i<p.size();i++)
{
cout << "High Pass iteration: " << q.getIter() <<" TIME: "<<q.getStart()<<" to "<<q.getEnd() <<" Frequency: "<<freqvec[i]<<" Hz " << " Intensity: " << 20 * log(inten[i] / 32768) << " Key: " << Util::getEnumString(p[i]) << endl;
}
}
for (int i = 0;i < numOfChunks;i++)
{
for (int j = 0;j < sampleSize;j++)
{
sampleChunks[i].push_back(audiodata[(i*sampleSize)+j]);
}
// DO FFT get back n Frequencies
// 2D array ----> [ [0,FREQENCIES],[1,FREQUENCIES] ]
// ITERATE THROUGH ALL SAMPLES
//then the top N amount of frequencies will be logged
// this will be stored in an object that has the following member variables: a pair vector with frequency and intensity,
// also put in the object is the chunk iteration or corresponding time range
// there will be a vector of these objects
//then we will attempt to create a midi file from this
}
for (int i = 0;i < numOfChunks;i++)
{
vector<double>* pointer = &sampleChunks[i];
fftw_complex* output_buffer = static_cast<fftw_complex*>(fftw_malloc(outputSize * sizeof(fftw_complex)));
fftw_plan plan = fftw_plan_dft_r2c_1d(inputSize,&sampleChunks[i][0] , output_buffer, flags);
fftw_execute(plan);
vector<double> test;
for (int i = 0; i < outputSize - 1;i++)
{
test.push_back((double)output_buffer[i][0]);
}
auto lol = max_element(test.begin(), test.end());
//cout << i << " CHUNK: " << " Time: "<< qBeatDuration*(i+1) << " " << numOfChunks << " Largest frequency is " << distance(begin(test), lol) * (wav.SampleRate / inputSize) << endl;
double frequency = distance(begin(test), lol) * (wav.SampleRate / inputSize);
vector<double> freqVector = { frequency };
vector<double> intenVector = { 1.0 };
Chunk chunk = Chunk(make_pair(freqVector,intenVector), i, qBeatDuration * (i + 1), qBeatDuration * (i + 2));
chunkData.push_back(chunk);
//get nth top frequencies
vector<size_t> index(test.size());
iota(test.begin(), test.end(), 0);
std::partial_sort(index.begin(), index.begin() + N, index.end(),
[&](size_t A, size_t B) {
return test[A] > test[B];
});
}
cout << "Length of ChunkData " << chunkData.size() << " \n";
cout << "Finished\n";
//WRITE TO MIDI
//create Wav
string fName = "data.wav";
string lola = "high.wav";
string smar = "proper.wav";
string aja = "bigboi.wav";
string ooo = "jaj.wav";
string sid = "delay.wav";
Util::createWavFile(lowPassDat,wav.ChunkSize,fName);
Util::createWavFile(highPassDat, wav.ChunkSize, lola);
Util::createWavFile(convolutionData, wav.ChunkSize, smar);
cout << "Convolution Data Size: " << convolutionData.size() << " wav.Chunksize: " << wav.ChunkSize << endl;
//MidiMaker::doSomething();
vector<double> j = Util::normalizeVector16(preProcData, 16);
j[0] = 0;
j[1] = 0;
Util::createRawFile(preProcData, "jojo.raw");
Util::createRawFile(j, "hahaha.raw");
vector<double> deriv = Util::dX(j);
Util::createRawFile(deriv, "dderiv.raw");
std::cout << "AHAHAHA" << endl;
vector<double> integ = Util::integrate(j);
vector<short> integSc = Util::doubleToShortScaled(integ);
Util::createWavFileMono(Util::normalizeVector(integSc), wav.ChunkSize, ooo);
std::cout << "oha" << endl;
Util::saveVectorToFile(deriv, "hello.txt");
std::chrono::system_clock::time_point now2 = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed = now2 - now1;
cout << "Took " << elapsed.count() << " seconds" << endl;
vector<short>scaled = Util::doubleToShortScaled(deriv);
//Util::noteSegmentation(left, right, scaled);
Util::createWavFileMono(Util::normalizeVector(scaled),wav.ChunkSize, aja);
return 0;
}