-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
147 lines (87 loc) · 2.04 KB
/
main.cpp
File metadata and controls
147 lines (87 loc) · 2.04 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
#include<wiringPi.h>
#include<dirent.h>
#include<unistd.h>
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#define BITS 8
using namespace std;
int numberOfSongs = 0;
char * path;
vector<string> songList;
string getFileExt(const string& s) {
size_t i = s.rfind('.', s.length());
if (i != string::npos) {
return(s.substr(i+1, s.length() - i));
}
return("");
}
int play_audio()
{
// Build the path for the audio list.
string formedPath(path);
int randNum = rand()%numberOfSongs + 1;
string songExt(getFileExt(songList[randNum]));
char *playCall = new char[500];
if(strcmp(songExt.c_str(),"mp3") == 0 )
snprintf(playCall,500,"mpg123 %s",songList[randNum].c_str());
else if(strcmp(songExt.c_str(), "wav") == 0 )
snprintf(playCall,500,"aplay %s", songList[randNum].c_str());
int ret = 0;
cout << "Calling bash command: " << playCall << endl;
system(playCall);
delete(playCall);
return 0;
}
void initialize_audio(){
DIR *d;
struct dirent *dir;
d = opendir("./Apeture");
string formedPath(path);
formedPath = formedPath + "/Apeture";
strcpy(path, formedPath.c_str());
if (d)
{
while((dir = readdir(d)) != NULL)
{
string formedPath(path);
string songName(dir->d_name);
formedPath = formedPath + "/" + songName;
songList.push_back(formedPath);
printf("%s\n", formedPath.c_str());
numberOfSongs++;
}
closedir(d);
cout << "Number of Songs: " << numberOfSongs << endl;
}
}
int main(){
path = new char[200];
for(int i =0; i < 200; i++)
path[i] == '\0';
getcwd(path, 200);
printf("%s\n", path);
string s(path);
s = s + "/Apeture/";
cout << endl << s;
initialize_audio();
system("espeak 'Alert. glad pi is now online.'");
srand(5);
play_audio();
wiringPiSetup();
pinMode(7,INPUT);
while (true){
if(digitalRead(7) >0){
cout << "I DETECT YOU\n";
play_audio();
usleep(10000000);
}
}
return 0;
}