-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwasm.cpp
executable file
·142 lines (113 loc) · 3.31 KB
/
wasm.cpp
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
#include <emscripten.h>
#include <StftPitchShift/CLI.h>
#include <StftPitchShift/StftPitchShift.h>
#include <StftPitchShift/Version.h>
using namespace stftpitchshift;
void INFO(const std::string& message)
{
EM_ASM({ console.info(UTF8ToString($0)); }, message.c_str());
}
void ERROR(const std::string& message)
{
EM_ASM({ console.error(UTF8ToString($0)); }, message.c_str());
}
EM_JS(void, shiftpitch_version, (),
{
const size = Module._shiftpitch_version_str(0, 0);
const data = Module._malloc(size + 1);
Module._shiftpitch_version_str(data, size);
const value = UTF8ToString(data);
Module._free(data);
return value;
});
EM_JS(void, shiftpitch_help, (),
{
const size = Module._shiftpitch_help_str(0, 0);
const data = Module._malloc(size + 1);
Module._shiftpitch_help_str(data, size);
const value = UTF8ToString(data);
Module._free(data);
return value;
});
EM_JS(void, shiftpitch, (int buffer, int args),
{
console.assert(typeof(buffer) === 'object', buffer);
console.assert(Object.prototype.toString.call(buffer).slice(8, -1) === 'AudioBuffer', buffer);
console.assert((typeof(args) === 'undefined') || (typeof(args) === 'string'), args);
const newbuffer = new AudioBuffer({
sampleRate: buffer.sampleRate,
length: buffer.length,
numberOfChannels: buffer.numberOfChannels
});
const samplerate = buffer.sampleRate;
const samples = buffer.length;
const channels = buffer.numberOfChannels;
const meminput = Module._malloc(samples * Float32Array.BYTES_PER_ELEMENT);
const memoutput = Module._malloc(samples * Float32Array.BYTES_PER_ELEMENT);
const input = new Float32Array(Module.HEAPF32.buffer, meminput, samples);
const output = new Float32Array(Module.HEAPF32.buffer, memoutput, samples);
const memargs = allocateUTF8(args ? args : "");
for (let channel = 0; channel < channels; channel++)
{
buffer.copyFromChannel(input, channel);
if (!Module._shiftpitch_f32(samplerate, samples, meminput, memoutput, memargs))
{
break;
}
newbuffer.copyToChannel(output, channel);
}
Module._free(memargs);
Module._free(meminput);
Module._free(memoutput);
return newbuffer;
});
extern "C"
{
int EMSCRIPTEN_KEEPALIVE shiftpitch_version_str(char* data, int size)
{
const std::string value = StftPitchShiftVersion;
value.copy(data, size);
return value.size();
}
int EMSCRIPTEN_KEEPALIVE shiftpitch_help_str(char* data, int size)
{
const std::string value = CLI("-h").usage();
value.copy(data, size);
return value.size();
}
bool EMSCRIPTEN_KEEPALIVE shiftpitch_f32(double samplerate, int samples, float* input, float* output, char* args)
{
CLI cli(args);
if (cli.help)
{
INFO(cli.usage());
return false;
}
if (cli.version)
{
INFO(StftPitchShiftVersion);
return false;
}
try
{
StftPitchShift stft(
cli.framesize,
std::get<1>(cli.framesize) / cli.hoprate,
samplerate,
cli.normalization,
cli.chronometry);
stft.shiftpitch(
{ input, static_cast<size_t>(samples) },
{ output, static_cast<size_t>(samples) },
cli.factors,
cli.quefrency,
cli.distortion);
}
catch (const std::exception& exception)
{
ERROR(exception.what());
return false;
}
return true;
}
}