You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: In this example we've used a folder called `src` for the library and modules folder but you can choose whatever you like.
42
+
Note2: Currently (v0.9.6) there is no Javascript modules support yet. Feel free to create a request via the issues section if you need it ;-).
43
+
44
+
#### Create the audio processor
45
+
46
+
After importing the library you should see `SepiaFW.webAudio` in your scope. We have used this already in the head to set the modules folder.
47
+
The `Processor` class is our main interface to handle the audio pipeline, but first we need to define the modules we want to use.
48
+
Modules come in two main flavours, "switch" and "worker" with the main difference that switches are based on 'AudioWorklet' and can pass through data on a lower level (more details later).
49
+
50
+
For this first example we only look at raw microphone data:
51
+
52
+
```javascript
53
+
var myModules = [];
54
+
55
+
functionbufferCallback(data){
56
+
//handle samples here using: data.samples
57
+
}
58
+
myModules.push({
59
+
name:'buffer-switch',
60
+
settings: {
61
+
onmessage: bufferCallback,
62
+
options: {
63
+
processorOptions: {
64
+
bufferSize:512, //size of samples generated
65
+
passThroughMode:0, //0: none, 1: original (float32 array)
66
+
}
67
+
}
68
+
}
69
+
});
70
+
```
71
+
72
+
After the module is set up we can create the processor:
73
+
74
+
```javascript
75
+
var processor =newSepiaFW.webAudio.Processor({
76
+
onaudiostart:console.log,
77
+
onaudioend:console.log,
78
+
onrelease:console.log,
79
+
onerror:console.error,
80
+
modules: myModules
81
+
82
+
}, function(info){
83
+
//Processor ready
84
+
console.log(info); //Use 'info' to get details about source, sample-rate etc.
85
+
86
+
}, function(err){
87
+
//Initialization error
88
+
console.error(err);
89
+
});
90
+
```
91
+
92
+
#### Start, stop and release the processor
93
+
94
+
As soon as the ready event fired we can start processing with `processor.start()`, check `onaudiostart` (defined in processor options) and wait for data in `bufferCallback` (defined in module).
95
+
96
+
After we're done we stop with `processor.stop()` and look out for `onaudioend`.
97
+
98
+
If we don't want to restart later we can close the processor and clean up resources with `processor.release()`.
99
+
100
+
### Resample input and record raw 16Bit PCM mono audio (WAV)
101
+
102
+
A very common use-case for this library is to resample microphone input and encode it as 16Bit PCM mono data (which is basically the default WAV file format).
103
+
To make this happen we will replace the buffer module from earlier with a resampler and wave encoder module.
104
+
NOTE: Some browsers are actually able to natively resampling for us ^^. The resampler module will simply skip transformation in this case but it might be preferable to prevent native resampling to retain full control over the quality and speed.
105
+
`SepiaFW.webAudio.isNativeStreamResamplingSupported` will be 'true' when the lib is imported because we can't test for the feature but set to 'false' after the first failed attempt of native resampling!
106
+
107
+
We create the resampler first and we use the "switch" version (not the "worker", this is preferred if 'AudioWorklet' is supported):
108
+
109
+
```javascript
110
+
SepiaFW.webAudio.tryNativeStreamResampling=false; //global option (remain in control of resampling)
111
+
112
+
var myModules = [];
113
+
var targetSampleRate =16000; //this is the sample-rate we want
114
+
var bufferSize =512; //size of samples generated
115
+
116
+
functionresamplerCallback(data){
117
+
//data will include e.g.: data.samples and data.rms (volume)
118
+
}
119
+
var resampler = {
120
+
name:'speex-resample-switch',
121
+
settings: {
122
+
onmessage: resamplerCallback,
123
+
sendToModules: [], //[moduleIndex] - filled below with index of wave-encoder module
0 commit comments