-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
211 lines (176 loc) · 4.88 KB
/
main.c
File metadata and controls
211 lines (176 loc) · 4.88 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
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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include "./pcm.h"
#include "./con_msg.h"
#include "./main.h"
#include "./mp3decode.h"
#define MAX_LINES 100
verbosity_t verbosity = VERBOSE_OFF;
void display_playlist(char * plist);
int main(int argc, char * argv[])
{
typedef enum
{
YES_PLAYLIST = 0,
NO_PLAYLIST = 1
}PLAYLIST_t;
char * filetype;
char * filename;
char * temp;
char * read = "/mnt/usb/MUSIC/02.wav";
char * plist = NULL;
char newfile[MAX_LINES];
int fd=0;
int i=0;
unsigned char * pdecoded = NULL;
struct filepaths file_paths;
pthread_t thread0;
FILE * playlist = NULL;
PLAYLIST_t PLAYLIST = NO_PLAYLIST;
file_paths.filepath = "/mnt/usb/MUSIC/02.wav";
file_paths.temppath = "/tmp/tempwav";
//READ IN CMD LINE OPERATORS
for(i=1; i<argc; i++)
{
if( strcmp(argv[i],"-v")==0 )
{
verbosity = VERBOSE_ON;
}
if( strcmp(argv[i], "-f")==0 )
{
file_paths.filepath=argv[i+1];
}
if( strcmp(argv[i], "-p")==0 )
{
PLAYLIST = YES_PLAYLIST;
plist = argv[i+1];
playlist = fopen(argv[i+1], "r");
}
}
con_msg(MSG_INFO, "\n###############################\n");
con_msg(MSG_INFO, "\n***** Welcome to WagAudio *****\n");
con_msg(MSG_INFO, "\n###############################\n");
if(PLAYLIST == YES_PLAYLIST)
{
display_playlist(plist);
}
while(read != NULL)
{
if(PLAYLIST == YES_PLAYLIST)
{
memset(newfile, 0, MAX_LINES);
read = fgets(newfile, (MAX_LINES-1), playlist);
if(read == NULL)
{
con_msg(MSG_INFO, "\nEnd of Playlist");
break;
}
/*fgets reads in new line char. must remove for determining filetype, etc*/
for(i = 0; i < strlen(newfile); i++)
{
if(newfile[i] == '\n' || newfile[i] == '\r')
{
newfile[i] = '\0';
}
}
file_paths.filepath = newfile;
}
else
{
read = NULL;
}
//Determine file type
filetype=(file_paths.filepath+(strlen(file_paths.filepath)-3));
if( strcmp(filetype, "mp3") && strcmp(filetype, "wav") )
{
con_msg(MSG_BAD, "\nUnrecognised file type %s for %s", filetype, file_paths.filepath);
continue;
}
//Determine filename
if( strchr(file_paths.filepath, '/') != NULL )
{
filename=(file_paths.filepath+(strlen(file_paths.filepath)));
do{
temp=strchr(filename, '/');
filename -= 1;
}while(temp == NULL);
filename=filename+2;
}
else
{
filename=file_paths.filepath;
}
//If mp3, decode
if( !strcmp(filetype, "mp3") )
{
pthread_create(&thread0, NULL, decode, &file_paths);
sleep(1); //decoder head-start
//decode(&file_paths);
file_paths.filepath = file_paths.temppath;
}
fd=open(file_paths.filepath, O_RDONLY);
if(fd < 0)
{
con_msg(MSG_BAD, "\nFailed to Open File %s", file_paths.filepath);
continue;
}
con_msg(MSG_GOOD, "Playing %s %s", filetype, filename);
//Play wav or decoded mp3
pcm_config();
pcm_setup_and_play(fd);
//Close file and free decoded memory
if( close(fd) != 0 )
{
con_msg(MSG_BAD, "\nError Closing %s", file_paths.filepath);
}
else
{
con_msg(MSG_VERBOSE, "\nClosed %s", file_paths.filepath);
}
pthread_join(thread0, NULL);
}
if(PLAYLIST == YES_PLAYLIST)
{
fclose(playlist);
}
con_msg(MSG_INFO, "\n");
return 0;
}
void display_playlist(char * plist)
{
char buffer[MAX_LINES];
char * read = NULL;
FILE * playlist = NULL;
char * filename = NULL;
char * temp = NULL;
playlist = fopen(plist, "r");
con_msg(MSG_INFO, "\nPlaying:\n");
memset(buffer, 0, MAX_LINES);
do
{
read = fgets(buffer, (MAX_LINES-1), playlist);
if(read == NULL)
{
break;
}
//Determine filename
if( strchr(read, '/') != NULL )
{
filename=(read+(strlen(read)));
do{
temp=strchr(filename, '/');
filename -= 1;
}while(temp == NULL);
filename=filename+2;
}
else
{
filename=read;
}
con_msg(MSG_INFO, "%s", filename);
}while(read != NULL);
con_msg(MSG_INFO, "\n");
fclose(playlist);
}