-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwav2mp3.cpp
125 lines (101 loc) · 2.76 KB
/
wav2mp3.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
#include <lame/lame.h>
#include <dirent.h>
#include <iostream>
#include <regex>
#include <string>
#include <thread>
#include <vector>
#include "threadPool.h"
using namespace std;
#ifdef _WIN32
#define SEPARATOR "\\"
#else
#define SEPARATOR "/"
#endif
void encode(string);
class convertTask: public Task{
public:
string fileName;
void run()
{
encode(fileName);
}
};
bool listWavFiles(string fileName, string dirName, ThreadPool* tPool){
regex wavfile(".{1,}\\.wav");
if (regex_match(fileName,wavfile))
{
cout<<"############### Found a wav file[ "<<fileName<<" ] encoding it to mp3"<<endl;
convertTask t ;
t.fileName = dirName+fileName;
tPool->addTask(&t);
//encode(dirName+fileName);
}
//create a regex for *.wav
}
void listDirs(string dirName, ThreadPool* tPool){
DIR *dir;
struct dirent *ent;
if ((dir = opendir (dirName.c_str())) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
if((strcmp(ent->d_name,"..") != 0) && (strcmp(ent->d_name,".") != 0)){
if(ent->d_type == DT_DIR){
// cout<<"Got a directory : "<<dirName+ent->d_name<<endl;
listDirs(dirName+ent->d_name+SEPARATOR,tPool);
}
listWavFiles(ent->d_name,dirName,tPool);
}
}
closedir (dir);
} else {
/* could not open directory */
cout<<"Error opening direcotry"<<endl;
}
}
void encode(string wavFile)
{
int read, write;
FILE *pcm = fopen(wavFile.c_str(), "rb");
std::size_t found = wavFile.rfind(".wav");
string mp3file = wavFile.substr(0,found)+".mp3";
FILE *mp3 = fopen(mp3file.c_str(), "wb");
const int PCM_SIZE = 8192;
const int MP3_SIZE = 8192;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
lame_t lame = lame_init();
//lame_set_in_samplerate(lame, 44100);
//lame_set_VBR(lame, vbr_default);
lame_init_params(lame);
do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
if (read == 0)
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
else if (read > 0)
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
else
break;
fwrite(mp3_buffer, write, 1, mp3);
} while (read != 0);
if (read >=0 && write >=0)
cout<<"Encoding Finished for [ "<< wavFile<<" ]"<<endl;
else
cout<<"Encoding Error for [ "<< wavFile<<" ]"<<endl;
lame_close(lame);
fclose(mp3);
fclose(pcm);
}
int main(int argc, char *argv[])
{
if ( argc != 2 )
// We print argv[0] assuming it is the program name
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
ThreadPool* tPool = new ThreadPool(5);
listDirs(argv[1],tPool);
tPool->stop();
delete tPool;
}
return 0;
}