-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectAndNotify.js
231 lines (198 loc) · 7.88 KB
/
detectAndNotify.js
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
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const axios = require('axios');
const winston = require('winston');
require('dotenv').config();
// Change this to 'true' to enable logging to console, but don't forget to change it back
// before invoking via Asterisk - because Asterisk determines success based on the first stdout line.
const DEBUG__LOG_TO_CONSOLE = false;
// Constants/args
const LOG_LEVEL = 'debug'; // Winston log level
const SHAZAM_RESPONSES_DIR = path.join(__dirname, 'responses'); // Directory for json responses from Shazam API
const LOGS_DIR = path.join(__dirname, 'logs'); // Directory for log files
const audioClipPath = process.argv[2]; // Path to the recorded file passed from Asterisk
const callerNumber = process.argv[3]; // Caller ID passed from Asterisk
const callerName = process.argv[4]; // Caller ID name passed from Asterisk (optional)
const audioClipFilename = path.basename(audioClipPath, path.extname(audioClipPath)); // Filename without path or extension
// Create winston logger
const logger = winston.createLogger({
level: LOG_LEVEL,
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.printf(
({ timestamp, message, level }) =>
`${timestamp} - ${level.toUpperCase().padEnd(7)} ` +
`[${audioClipFilename}] ${message}`
)
),
// Log to files (`full.log` and `<audioClipFilename>.log` in `./logs`)
// Or, if debug mode is set, log to console only
transports: !DEBUG__LOG_TO_CONSOLE
? [
new winston.transports.File({
filename: path.join(LOGS_DIR, `${audioClipFilename}.log`)
}),
new winston.transports.File({ filename: LOGS_DIR + '/full.log' })
]
: [new winston.transports.Console()]
});
// Indicate beginning of log of current execution
logger.info('---');
// Ensure clip path and caller number were provided
if (!audioClipPath || !callerNumber) {
logger.error('Missing required CLI parameters');
if (!DEBUG__LOG_TO_CONSOLE) {
process.stdout.write('missing_parameters');
}
return;
} else {
logger.debug(`Processing audio file: ${audioClipPath}`);
logger.debug(`Caller number: ${callerNumber}`);
if (callerName) logger.debug(`Caller name (CNAM): ${callerName}`);
}
// Ensure responses dir exist, create if not
if (!fs.existsSync(SHAZAM_RESPONSES_DIR)) {
logger.debug(`Creating directory for Shazam API response files: ${SHAZAM_RESPONSES_DIR}`);
fs.mkdirSync(SHAZAM_RESPONSES_DIR, { recursive: true });
}
// Ensure logs dir exist, create if not
if (!fs.existsSync(LOGS_DIR)) {
logger.debug(`Creating directory for invocation logs: ${LOGS_DIR}`);
fs.mkdirSync(LOGS_DIR, { recursive: true });
}
async function convertAudio(filePath) {
return new Promise((resolve, reject) => {
logger.info('Converting to raw audio file...');
const convertedPath = filePath.replace(path.extname(filePath), '.dat');
const ffmpeg = spawn('ffmpeg', [
'-i',
filePath,
'-ac',
'1',
'-f',
's16le',
'-acodec',
'pcm_s16le',
'-ar',
'44100',
'-y',
convertedPath
]);
ffmpeg.on('close', (code) => {
if (code === 0) {
logger.debug('Conversion complete.');
resolve(convertedPath);
} else {
reject(new Error(`ffmpeg process exited with code ${code}`));
}
});
});
}
async function identifyAudioFile(filePath) {
logger.info('Uploading to Shazam API...');
// Ensure audio file is less than 700KB (Shazaam API limit more or less)
const audioFileSizeKB = Math.round(fs.statSync(filePath).size / 1024);
if (audioFileSizeKB > 700) {
throw new Error(
`Audio file is too large. Must be less than 700KB. Current size: ${audioFileSizeKB}`
);
} else {
logger.debug(`Audio file size: ${audioFileSizeKB}KB`);
}
// Convert audio file to base64
const audioBase64 = fs.readFileSync(filePath, 'base64');
// Upload to Shazam API
try {
const response = await axios.post(
'https://shazam.p.rapidapi.com/songs/v2/detect',
audioBase64,
{
headers: {
'x-rapidapi-host': 'shazam.p.rapidapi.com',
'x-rapidapi-key': process.env.SHAZAM_API_KEY,
'content-type': 'text/plain'
}
}
);
const data = response.data;
// Check if audio was identified
if (!data?.track) {
return null;
}
// Return title and artist
return data;
} catch (error) {
logger.error(`Error uploading to Shazam API: ${error.message}`);
throw error;
}
}
async function sendSms(callerNumber, shazamResult) {
const song = shazamResult?.track?.title || 'unknown';
const artist = shazamResult?.track?.subtitle || 'unknown';
//! NOT YET IMPLEMENTED, A2P 10DLC GRRRR.....
//* Feel free to implement this part yourself, considering you're approved for A2P 10DLC
//* You can use axios or any other library (that may be provided by the provider) to send your SMS
// Send SMS, assuming not in debug mode
if (DEBUG__LOG_TO_CONSOLE !== true) {
// TODO: Send text message with song and artist to `callerNumber`
// Example:
// await axios.post('https://api.example.com/sms', {
// to: callerNumber,
// message: `You just heard "${song}" by "${artist}"!`
// });
logger.info(
'Text message not sent; not yet implemented. User notified falsely via voice response :('
);
} else {
logger.info('Text message not sent; debug mode enabled.');
}
}
(async () => {
// Ensure audio file exists
if (!fs.existsSync(audioClipPath)) {
logger.error(`Audio file does not exist: ${audioClipPath}`);
process.stdout.write('error_audio_file_not_found');
process.exit(1);
}
try {
let isIdentified = null;
const rawAudioPath = await convertAudio(audioClipPath);
const shazamResult = await identifyAudioFile(rawAudioPath);
if (shazamResult === null) {
isIdentified = false;
logger.info('Audio could not be identified by Shazam');
if (!DEBUG__LOG_TO_CONSOLE) {
// Indicate no detect (as expected by Asterisk)
process.stdout.write('no_detect');
}
} else {
const song = shazamResult?.track?.title || 'unknown';
const artist = shazamResult?.track?.subtitle || 'unknown';
isIdentified = true;
logger.info(`Song identified as: "${song}" by "${artist}"`);
if (!DEBUG__LOG_TO_CONSOLE) {
// Indicate success (as expected by Asterisk)
process.stdout.write('success');
}
// Send SMS (not yet implemented)
await sendSms(callerNumber, shazamResult);
}
// Delete raw audio file
if (fs.existsSync(rawAudioPath)) {
fs.unlinkSync(rawAudioPath);
}
// Save Shazam API response to a unique JSON file
const jsonFilePath = path.join(
SHAZAM_RESPONSES_DIR,
(isIdentified ? '^' : '') + audioClipFilename + '.json'
);
fs.writeFileSync(jsonFilePath, JSON.stringify(shazamResult, null, 2), 'utf-8');
logger.debug(`Shazam API response saved to ${jsonFilePath}`);
} catch (error) {
logger.error(`Error processing or identifying audio: ${error.message}`);
if (!DEBUG__LOG_TO_CONSOLE) {
process.stdout.write(`error_processing_or_identifying:${error.message}`);
}
}
})();