-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.cc
271 lines (237 loc) · 7.63 KB
/
main.cc
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
/*
* =====================================================================================
*
* Filename: main.cc
*
* Description: This is our main file.
*
* Version: 1.0
* Created: Sunday 05 September 2010 09:44:39 IST
* Revision: none
* Compiler: g++/gcc
*
* Author: Dilawar (nuts), dilawar[AT]ee[dot]iitb[dot]ac[dot]in
* Institute: Indian Institute of Technology, Bombay
*
* This material is released under GNU Lesser Public License.
* You are free to copy, distribute or use it for any non-commercial activity.
* But you are not allowed to modify it. If you are a student, you can use its
* part in your work with or without mentioning it.
*
* For additional details, please see the GNU Lesser Public license.
*
* NOTE : No propriety software is used in this material.
* Almost all of the code is written and modified in VIM editor with c-support
* plugin which is more awesome than Kung Fu Panda. Just kidding, no one is more
* awesome than Kung Fu Panda with or without a light saber.
*
* This program is made using a bit for here, a bit from there under the influence
* of a lot of burnt out neurons.
* Report bugs : dilawar.in@gmail.com
* =====================================================================================
*/
#define EXIT_SUCCESS 1
#define EXIT_FAILURE 0
#define TRUE 1
#define FALSE 0
#include <iostream>
#include "include/wav-file.h"
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv)
{
#if DEBUG
std::cout<<"Entering the main()\n";
std::cout<<"\nNo of args are: "<<argc;
#endif
/* Here, we parse our argument passed to this application from command line*/
if(argc < 2)
{
std::cout<<"\nOh Snap!"
<<"\nWassup, Fellow Human.."
<<"\nAt least, you need to specify wav file. Use --help to see your options.\n";
return EXIT_FAILURE;
}
int c;
int do_help = 0;
int do_verbose = 0;
bool ifInvalid = FALSE; /* flag variables. */
char inputFile[80];
while(1)
{
int option_index = 0;
static struct option long_options[] =
{
{"file", required_argument, NULL, 'f'},
{"help", no_argument, &do_help, 1},
{"verbose", no_argument, &do_verbose, 1},
{0, 0, 0 , 0}
};
c = getopt_long(argc, argv, "f:hv", long_options, &option_index);
if (c == -1)
break;
switch(c)
{
case 'f':
#if DEBUG
std::cout<<"\nFrom main() - Option --file with value '"
<<optarg<<"'\n";
#endif
strcpy(inputFile, optarg);
break;
case 0:
#ifdef DEBUG
std::cout<<"\noption "<<long_options[option_index].name;
if (optarg)
std::cout<<"with arg "<<optarg;
//std::cout<<"Bad option. x-(\n";
#endif
break;
case 'v':
do_verbose = 1;
#ifdef DEBUG
std::cout<<"\nFrom main() : Verbose output is set.\n";
#else /* ----- not DEBUG ----- */
#endif /* ----- not DEBUG ----- */
break;
case 'h':
do_help = 1;
break;
case ':': /* missing option arguement. */
fprintf(stderr, " option %s requires an argument. \n",
argv[1]);
ifInvalid = TRUE;
break;
case '?':
default:
fprintf(stderr, "option %s is invalid : ignored\n",
argv[1]);
ifInvalid = TRUE;
break;
}
}
/* We got the options. Do the stuff. */
if(TRUE == ifInvalid || 1 == do_help)
{
std::cout<<"\n-------------------------------------------------------";
std::cout<<"\nUSAGE :";
std::cout<<"\n\t --file <name>"
<<"\n\t\t provide the wav file."
<<"\n\t --help"
<<"\n\t\t to see the help. Should be used alone."
<<"\n\t --verbose"
<<"\n\t\t for verbose output.";
std::cout<<"\n-------------------------------------------------------\n";
return EXIT_FAILURE;
}
WavFile* pObjWavFile;
printf("Input file : %s", inputFile);
pObjWavFile = new WavFile;
if (EXIT_SUCCESS != pObjWavFile->openWavFile(inputFile))
{
std::cout<<"\nCan't load wav file.";
exit(-1);
}
pObjWavFile->displayInformation(inputFile);
/* read data and do something */
#if ALSA
double data;
while(pObjWavFile->ifMoreDataAvailable())
{
data = pObjWavFile->readCurrentInput();
//std::cout<<"\t"<<data;
/* can we play it here. */
}
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val, val2;
int dir;
snd_pcm_uframes_t frames;
long loop;
int size;
char* buffer;
/* open PCM device for playback */
rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if( rc < 0)
{
fprintf(stderr, "unable to open PCM device : %s\n", snd_strerror(rc));
exit(-1);
}
/* allocate HW parameters objects. */
snd_pcm_hw_params_alloca(¶ms);
/* fill in the default values. */
snd_pcm_hw_params_any(handle, params);
/* set the desired hw parameters */
//interleaved mode
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* signed 16 bit little endian format */
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
/* Set the channels values. */
snd_pcm_hw_params_set_channels(handle, params, pObjWavFile->getNumChannels());
/* sampling rate. */
//val = pObjWavFile->getSampleRateHz();
val = 44100;
//std::cout<<"sample:"<<val;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
/* set period size. */
frames = 32;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
/* write the parameters to driver. */
rc = snd_pcm_hw_params(handle, params);
if(rc < 0)
{
fprintf(stderr, "Unable to set HW parameters : %s\n", snd_strerror(rc));
exit(-1);
}
/* use a buffer large enough to hold one period. */
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
size = frames*4; /* 2 bytes/sample, 2 channels */
buffer = (char *) malloc(size);
/* we want to loop for 5 seconds. */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
/* 5 seconds in microseconds divided by period time. */
loop = 5000000/val;
while(loop > 0)
{
loop--;
rc = read(0, buffer, size);
if(rc == 0)
{
fprintf(stderr, "end of file on input.\n");
break;
}
else if (rc != size)
{
fprintf(stderr, "short read: read %d bytes\n", rc);
}
// buffer = "ab";
rc = snd_pcm_writei(handle, buffer, frames);
if(rc == -EPIPE)
{
/* EPIPE means underrun */
fprintf(stderr, "unknown occurred.\n");
snd_pcm_prepare(handle);
}
else if (rc < 0)
{
fprintf(stderr, "error from writei : %s\n", snd_strerror(rc));
}
else if(rc != (int) frames)
{
fprintf(stderr, "short write, write %d frames\n", rc);
}
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
#endif
/* write data in a file. */
char outfile[80] = {};
strcat( outfile, inputFile);
strcat( outfile, ".csv");
pObjWavFile->writeDataToFile( outfile );
return 0;
}