Last active
January 6, 2017 15:27
-
-
Save alexanderwallin/ff504be6875b1cb90b4e898444a06f89 to your computer and use it in GitHub Desktop.
Emscripten audio noise using pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
float get_random_float(const float min, const float max) { | |
return min + (max - min) * ((float) rand() / (float) RAND_MAX); | |
} | |
void add_noise(const void *inBuffer, unsigned int inBufferLength, const void *outBuffer) { | |
int i = 0; | |
float* inData = (float*) inBuffer; | |
float* outData = (float*) outBuffer; | |
printf("in test method: size of inBuffer buffer = %d\n", inBufferLength); | |
for (i = 0; i < inBufferLength; i++) { | |
float noise = get_random_float(-0.1f, 0.1f); | |
outData[i] = inData[i] + noise; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>emcc-audio-add-noise</title> | |
</head> | |
<body> | |
<script src="dsp.c.js"></script> | |
<script src="app.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addNoise = Module.cwrap('add_noise', 'number', ['number', 'number']) | |
const ctx = new AudioContext() | |
const osc = ctx.createOscillator() | |
osc.frequency.value = 440 | |
const noise = ctx.createScriptProcessor(512, 2, 2) | |
noise.onaudioprocess = (evt) => { | |
// const timeStart = Date.now() | |
const { inputBuffer, outputBuffer } = evt | |
for (let c = 0; c < outputBuffer.numberOfChannels; c++) { | |
const channelInput = inputBuffer.getChannelData(c) | |
const channelOutput = outputBuffer.getChannelData(c) | |
const numSamples = channelInput.length | |
const inPtr = Module._malloc(numSamples * Float32Array.BYTES_PER_ELEMENT) | |
const inBuf = new Float32Array(Module.HEAPF32.buffer, inPtr, numSamples) | |
const outPtr = Module._malloc(numSamples * Float32Array.BYTES_PER_ELEMENT) | |
const outBuf = new Float32Array(Module.HEAPF32.buffer, outPtr, numSamples) | |
for (let is = 0; is < channelInput.length; is++) { | |
inBuf[is] = channelInput[is] | |
} | |
addNoise(inPtr, inBuf.length, outPtr) | |
for (let os = 0; os < channelOutput.length; os++) { | |
channelOutput[os] = outBuf[os] | |
} | |
Module._free(inPtr) | |
Module._free(outPtr) | |
} | |
// console.log(Date.now() - timeStart) | |
} | |
const vol = ctx.createGain() | |
vol.gain.value = 0.1 | |
osc.connect(noise) | |
noise.connect(vol) | |
vol.connect(ctx.destination) | |
osc.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
emcc -o dsp.c.js -s EXPORTED_FUNCTIONS="['_add_noise']" -s ASSERTIONS=1 dsp.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment