Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/ofxEmscripten/libs/html5audio/include/html5audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C"{
extern void html5audio_sound_set_pan(int sound, double pan);
extern void html5audio_sound_free(int sound);

extern void html5audio_stream_create(int bufferSize, int inputChannels, int outputChannels, float * inbuffer, float * outbuffer, html5audio_stream_callback callback, void * userData);
extern void html5audio_stream_create(int audioWorkletNode, int numInputChannels);
extern void html5audio_stream_free();
extern bool html5audio_sound_is_loaded(int sound);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var mediaElement = null;

function getAudioDevices(){
var audioInDevices = "";
var audioOutDevices = "";
if(!navigator.mediaDevices.enumerateDevices){
console.log("enumerateDevices() not supported.");
}else{
navigator.mediaDevices.getUserMedia({audio: true, video: false}).then(s => {
navigator.mediaDevices.enumerateDevices().then((devices) => {
devices.forEach((device) => {
if(device.kind == "audioinput"){
audioInDevices = audioInDevices.concat(",", `${device.deviceId}`,",", `${device.label}`);
}
if(device.kind == "audiooutput"){
audioOutDevices = audioOutDevices.concat(",", `${device.groupId}`,",", `${device.label}`);
}
});
Module.loadAudioInDevices(audioInDevices);
Module.loadAudioOutDevices(audioOutDevices);
})
.catch((err) => {
console.error(`${err.name}: ${err.message}`);
});
});
}
}

function selectAudioIn(file){
if (navigator.getUserMedia){
navigator.getUserMedia(
{ audio: { deviceId: { exact: file } } },
function (audioIn){
if(mediaElement){
mediaElement.disconnect();
}
mediaElement = AUDIO.context.createMediaStreamSource(audioIn);
mediaElement.connect(AUDIO.stream);
},
function (error){
console.log("error creating audio in",error);
});
}
}

function selectAudioOut(file){
//AUDIO.context.setSinkId(file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ var LibraryHTML5Audio = {
}
},

html5audio_context_create__deps: ['$emscriptenRegisterAudioObject'],
html5audio_context_create: function () {
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext({});
var context = new AudioContext({ sampleRate: 44100 });
var id = emscriptenRegisterAudioObject(context);

// Fix issue with chrome autoplay policy
document.addEventListener('mousedown', function cb(event) {
Expand All @@ -44,7 +46,7 @@ var LibraryHTML5Audio = {
fft.maxDecibels = 0;
fft.minDecibels = -100;
AUDIO.fft = fft;
return 0;
return id;
} catch (e) {
console.log('Web Audio API is not supported in this browser', e);
return -1;
Expand All @@ -62,7 +64,11 @@ var LibraryHTML5Audio = {
html5audio_context_spectrum: function (bands, spectrum) {
AUDIO.fft.fftSize = bands * 2;
var spectrumArray = Module.HEAPF32.subarray(spectrum >> 2, (spectrum >> 2) + bands);
AUDIO.fft.getFloatFrequencyData(spectrumArray);
var spectrumArrayCopy = new Float32Array (spectrumArray);
AUDIO.fft.getFloatFrequencyData(spectrumArrayCopy);
for (let i = 0; i < spectrumArrayCopy.length; i++) {
spectrumArray[i] = spectrumArrayCopy[i];
}
},

html5audio_context_samplerate: function () {
Expand Down Expand Up @@ -201,48 +207,21 @@ var LibraryHTML5Audio = {
}
},

html5audio_stream_create: function(bufferSize, inputChannels, outputChannels, inbuffer, outbuffer, callback, userData) {
var stream = AUDIO.context.createScriptProcessor(bufferSize, inputChannels, outputChannels);
var inbufferArray = Module.HEAPF32.subarray(inbuffer >> 2, (inbuffer >> 2) + bufferSize * inputChannels);
var outbufferArray = Module.HEAPF32.subarray(outbuffer >> 2, (outbuffer >> 2) + bufferSize * outputChannels);

stream.onaudioprocess = function(event) {
var i, j, c;
if (inputChannels > 0) {
for (c = 0; c < inputChannels; ++c) {
var inChannel = event.inputBuffer.getChannelData(c);
for (i = 0, j = c; i < bufferSize; ++i, j += inputChannels) {
inbufferArray[j] = inChannel[i];
}
}
}

{{{ makeDynCall('viiii', 'callback') }}}(bufferSize, inputChannels, outputChannels, userData);

if (outputChannels > 0) {
for (c = 0; c < outputChannels; ++c) {
var outChannel = event.outputBuffer.getChannelData(c);
for (i = 0, j = c; i < bufferSize; ++i, j += outputChannels) {
outChannel[i] = outbufferArray[j];
}
}
}
};

if (inputChannels > 0) {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(function (audioIn) {
var mediaElement = AUDIO.context.createMediaStreamSource(audioIn);
mediaElement.connect(stream);
AUDIO.mediaElement = mediaElement;
})
.catch(function (error) {
console.log("Error creating audio in", error);
});
}

stream.connect(AUDIO.fft);
},
html5audio_stream_create__deps: ['$emscriptenGetAudioObject'],
html5audio_stream_create: function(audioWorkletNode, numInputChannels){
var audioWorkletNode = emscriptenGetAudioObject(audioWorkletNode);
if(numInputChannels > 0){
navigator.mediaDevices.getUserMedia({ audio: true })
.then(function (audioIn) {
var mediaElement = AUDIO.context.createMediaStreamSource(audioIn);
mediaElement.connect(audioWorkletNode);
})
.catch(function (error) {
console.log("Error creating audio in", error);
});
}
audioWorkletNode.connect(AUDIO.fft);
},

html5audio_stream_free: function () {

Expand Down
Loading
Loading