This repository was archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathfunctions.cxx
More file actions
132 lines (112 loc) · 4.15 KB
/
functions.cxx
File metadata and controls
132 lines (112 loc) · 4.15 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
#include <stdio.h>
#include <string.h>
#include <memory>
#ifndef _WIN32
#include <libgen.h>
#include <dirent.h>
#endif
#include <stdlib.h>
#include <stdexcept>
#include "AudioStreamInput.h"
#include "Metadata.h"
#include "Codegen.h"
#include <string>
#include "functions.h"
using namespace std;
// deal with quotes etc in json
std::string escape(const string& value) {
std::string s(value);
std::string out = "";
out.reserve(s.size());
for (size_t i = 0; i < s.size(); i++) {
char c = s[i];
if ((unsigned char)c < 31)
continue;
switch (c) {
case '"' : out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b" ; break;
case '\f': out += "\\f" ; break;
case '\n': out += "\\n" ; break;
case '\r': out += "\\r" ; break;
case '\t': out += "\\t" ; break;
// case '/' : out += "\\/" ; break; // Unnecessary?
default:
out += c;
// TODO: do something with unicode?
}
}
return out;
}
codegen_response_t *codegen_file(char* filename, int start_offset, int duration, int tag) {
// Given a filename, perform a codegen on it and get the response
// This is called by a thread
double t1 = now();
codegen_response_t *response = (codegen_response_t *)malloc(sizeof(codegen_response_t));
response->error = NULL;
response->codegen = NULL;
auto_ptr<FfmpegStreamInput> pAudio(new FfmpegStreamInput());
pAudio->ProcessFile(filename, start_offset, duration);
if (pAudio.get() == NULL) { // Unable to decode!
char* output = (char*) malloc(16384);
sprintf(output,"{\"error\":\"could not create decoder\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}",
tag,
escape(filename).c_str());
response->error = output;
return response;
}
int numSamples = pAudio->getNumSamples();
if (numSamples < 1) {
char* output = (char*) malloc(16384);
sprintf(output,"{\"error\":\"could not decode\", \"tag\":%d, \"metadata\":{\"filename\":\"%s\"}}",
tag,
escape(filename).c_str());
response->error = output;
return response;
}
t1 = now() - t1;
double t2 = now();
Codegen *pCodegen = new Codegen(pAudio->getSamples(), numSamples, start_offset);
t2 = now() - t2;
response->t1 = t1;
response->t2 = t2;
response->numSamples = numSamples;
response->codegen = pCodegen;
response->start_offset = start_offset;
response->duration = duration;
response->tag = tag;
response->filename = filename;
return response;
}
char *make_json_string(codegen_response_t* response, bool human_readable_code) {
if (response->error != NULL) {
return response->error;
}
// Get the ID3 tag information.
auto_ptr<Metadata> pMetadata(new Metadata(response->filename));
// preamble + codelen
char* output = (char*) malloc(sizeof(char)*(16384 + strlen(response->codegen->getCodeString(human_readable_code).c_str()) ));
sprintf(output,"{\"metadata\":{\"artist\":\"%s\", \"release\":\"%s\", \"title\":\"%s\", \"genre\":\"%s\", \"bitrate\":%d,"
"\"sample_rate\":%d, \"duration\":%d, \"filename\":\"%s\", \"samples_decoded\":%d, \"given_duration\":%d,"
" \"start_offset\":%d, \"version\":%2.2f, \"codegen_time\":%2.6f, \"decode_time\":%2.6f}, \"code_count\":%d,"
" \"code\":%s, \"tag\":%d}",
escape(pMetadata->Artist()).c_str(),
escape(pMetadata->Album()).c_str(),
escape(pMetadata->Title()).c_str(),
escape(pMetadata->Genre()).c_str(),
pMetadata->Bitrate(),
pMetadata->SampleRate(),
pMetadata->Seconds(),
escape(response->filename).c_str(),
response->numSamples,
response->duration,
response->start_offset,
response->codegen->getVersion(),
response->t2,
response->t1,
response->codegen->getNumCodes(),
response->codegen->getCodeString(human_readable_code).c_str(),
response->tag
);
return output;
}