2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* audio_server_javascript.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2015-09-10 03:10:54 +00:00
|
|
|
#include "audio_server_javascript.h"
|
|
|
|
|
|
|
|
#include "emscripten.h"
|
|
|
|
|
|
|
|
AudioMixer *AudioServerJavascript::get_mixer() {
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::audio_mixer_chunk_callback(int p_frames) {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RID AudioServerJavascript::sample_create(SampleFormat p_format, bool p_stereo, int p_length) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Sample *sample = memnew(Sample);
|
|
|
|
sample->format = p_format;
|
|
|
|
sample->stereo = p_stereo;
|
|
|
|
sample->length = p_length;
|
|
|
|
sample->loop_begin = 0;
|
|
|
|
sample->loop_end = p_length;
|
|
|
|
sample->loop_format = SAMPLE_LOOP_NONE;
|
|
|
|
sample->mix_rate = 44100;
|
|
|
|
sample->index = -1;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return sample_owner.make_rid(sample);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::sample_set_description(RID p_sample, const String &p_description) {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
String AudioServerJavascript::sample_get_description(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioServerJavascript::SampleFormat AudioServerJavascript::sample_get_format(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return SAMPLE_FORMAT_PCM8;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool AudioServerJavascript::sample_is_stereo(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
const Sample *sample = sample_owner.get(p_sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!sample, false);
|
2015-09-10 03:10:54 +00:00
|
|
|
return sample->stereo;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioServerJavascript::sample_get_length(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
const Sample *sample = sample_owner.get(p_sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!sample, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
return sample->length;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
const void *AudioServerJavascript::sample_get_data_ptr(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::sample_set_data(RID p_sample, const DVector<uint8_t> &p_buffer) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
Sample *sample = sample_owner.get(p_sample);
|
|
|
|
ERR_FAIL_COND(!sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
int chans = sample->stereo ? 2 : 1;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
Vector<float> buffer;
|
2017-03-18 23:36:26 +00:00
|
|
|
buffer.resize(sample->length * chans);
|
|
|
|
DVector<uint8_t>::Read r = p_buffer.read();
|
|
|
|
if (sample->format == SAMPLE_FORMAT_PCM8) {
|
|
|
|
const int8_t *ptr = (const int8_t *)r.ptr();
|
|
|
|
for (int i = 0; i < sample->length * chans; i++) {
|
|
|
|
buffer[i] = ptr[i] / 128.0;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (sample->format == SAMPLE_FORMAT_PCM16) {
|
|
|
|
const int16_t *ptr = (const int16_t *)r.ptr();
|
|
|
|
for (int i = 0; i < sample->length * chans; i++) {
|
|
|
|
buffer[i] = ptr[i] / 32768.0;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ERR_EXPLAIN("Unsupported for now");
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
sample->tmp_data = buffer;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<uint8_t> AudioServerJavascript::sample_get_data(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return DVector<uint8_t>();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::sample_set_mix_rate(RID p_sample, int p_rate) {
|
2015-09-10 03:10:54 +00:00
|
|
|
Sample *sample = sample_owner.get(p_sample);
|
|
|
|
ERR_FAIL_COND(!sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
sample->mix_rate = p_rate;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioServerJavascript::sample_get_mix_rate(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
const Sample *sample = sample_owner.get(p_sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!sample, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
return sample->mix_rate;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::sample_set_loop_format(RID p_sample, SampleLoopFormat p_format) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
Sample *sample = sample_owner.get(p_sample);
|
|
|
|
ERR_FAIL_COND(!sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
sample->loop_format = p_format;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioServerJavascript::SampleLoopFormat AudioServerJavascript::sample_get_loop_format(RID p_sample) const {
|
|
|
|
|
|
|
|
const Sample *sample = sample_owner.get(p_sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!sample, SAMPLE_LOOP_NONE);
|
2015-09-10 03:10:54 +00:00
|
|
|
return sample->loop_format;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::sample_set_loop_begin(RID p_sample, int p_pos) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
Sample *sample = sample_owner.get(p_sample);
|
|
|
|
ERR_FAIL_COND(!sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
sample->loop_begin = p_pos;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioServerJavascript::sample_get_loop_begin(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
const Sample *sample = sample_owner.get(p_sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!sample, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
return sample->loop_begin;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::sample_set_loop_end(RID p_sample, int p_pos) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
Sample *sample = sample_owner.get(p_sample);
|
|
|
|
ERR_FAIL_COND(!sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
sample->loop_end = p_pos;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioServerJavascript::sample_get_loop_end(RID p_sample) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
const Sample *sample = sample_owner.get(p_sample);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!sample, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
return sample->loop_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VOICE API */
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
RID AudioServerJavascript::voice_create() {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = memnew(Voice);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
voice->index = voice_base;
|
|
|
|
voice->volume = 1.0;
|
|
|
|
voice->pan = 0.0;
|
|
|
|
voice->pan_depth = .0;
|
|
|
|
voice->pan_height = 0.0;
|
|
|
|
voice->chorus = 0;
|
|
|
|
voice->reverb_type = REVERB_SMALL;
|
|
|
|
voice->reverb = 0;
|
|
|
|
voice->mix_rate = -1;
|
|
|
|
voice->positional = false;
|
|
|
|
voice->active = false;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-10 03:10:54 +00:00
|
|
|
EM_ASM_( {
|
2017-01-15 19:00:04 +00:00
|
|
|
_as_voices[$0] = null;
|
|
|
|
_as_voice_gain[$0] = _as_audioctx.createGain();
|
|
|
|
_as_voice_pan[$0] = _as_audioctx.createStereoPanner();
|
|
|
|
_as_voice_gain[$0].connect(_as_voice_pan[$0]);
|
|
|
|
_as_voice_pan[$0].connect(_as_audioctx.destination);
|
|
|
|
}, voice_base);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
voice_base++;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return voice_owner.make_rid(voice);
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_play(RID p_voice, RID p_sample) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
2015-09-10 03:10:54 +00:00
|
|
|
ERR_FAIL_COND(!voice);
|
2017-03-18 23:36:26 +00:00
|
|
|
Sample *sample = sample_owner.get(p_sample);
|
2015-09-10 03:10:54 +00:00
|
|
|
ERR_FAIL_COND(!sample);
|
|
|
|
|
|
|
|
// due to how webaudio works, sample cration is deferred until used
|
|
|
|
// sorry! WebAudio absolutely sucks
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (sample->index == -1) {
|
2015-09-10 03:10:54 +00:00
|
|
|
//create sample if not created
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(sample->tmp_data.size() == 0);
|
|
|
|
sample->index = sample_base;
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
_as_samples[$0] = _as_audioctx.createBuffer($1, $2, $3);
|
|
|
|
}, sample_base, sample->stereo ? 2 : 1, sample->length, sample->mix_rate);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
sample_base++;
|
2017-03-18 23:36:26 +00:00
|
|
|
int chans = sample->stereo ? 2 : 1;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < chans; i++) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-10 03:10:54 +00:00
|
|
|
EM_ASM_({
|
2017-01-15 19:00:04 +00:00
|
|
|
_as_edited_buffer = _as_samples[$0].getChannelData($1);
|
|
|
|
}, sample->index, i);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < sample->length; j++) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-10 03:10:54 +00:00
|
|
|
EM_ASM_({
|
2017-01-15 19:00:04 +00:00
|
|
|
_as_edited_buffer[$0] = $1;
|
|
|
|
}, j, sample->tmp_data[j * chans + i]);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sample->tmp_data.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
voice->sample_mix_rate = sample->mix_rate;
|
|
|
|
if (voice->mix_rate == -1) {
|
|
|
|
voice->mix_rate = voice->sample_mix_rate;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float freq_diff = Math::log(float(voice->mix_rate) / float(voice->sample_mix_rate)) / Math::log(2.0);
|
|
|
|
int detune = int(freq_diff * 1200.0);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
if (_as_voices[$0] !== null) {
|
|
|
|
_as_voices[$0].stop(); //stop and byebye
|
|
|
|
}
|
|
|
|
_as_voices[$0] = _as_audioctx.createBufferSource();
|
|
|
|
_as_voices[$0].connect(_as_voice_gain[$0]);
|
|
|
|
_as_voices[$0].buffer = _as_samples[$1];
|
|
|
|
_as_voices[$0].loopStart.value = $1;
|
|
|
|
_as_voices[$0].loopEnd.value = $2;
|
|
|
|
_as_voices[$0].loop.value = $3;
|
|
|
|
_as_voices[$0].detune.value = $6;
|
|
|
|
_as_voice_pan[$0].pan.value = $4;
|
|
|
|
_as_voice_gain[$0].gain.value = $5;
|
|
|
|
_as_voices[$0].start();
|
|
|
|
_as_voices[$0].onended = function() {
|
|
|
|
_as_voices[$0].disconnect(_as_voice_gain[$0]);
|
|
|
|
_as_voices[$0] = null;
|
|
|
|
}
|
|
|
|
}, voice->index, sample->index, sample->mix_rate * sample->loop_begin, sample->mix_rate * sample->loop_end, sample->loop_format != SAMPLE_LOOP_NONE, voice->pan, voice->volume * fx_volume_scale, detune);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
voice->active = true;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
2015-09-10 03:10:54 +00:00
|
|
|
ERR_FAIL_COND(!voice);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
voice->volume = p_volume;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
if (voice->active) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
_as_voice_gain[$0].gain.value = $1;
|
|
|
|
}, voice->index, voice->volume * fx_volume_scale);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_depth, float height) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
2015-09-10 03:10:54 +00:00
|
|
|
ERR_FAIL_COND(!voice);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
voice->pan = p_pan;
|
|
|
|
voice->pan_depth = p_depth;
|
|
|
|
voice->pan_height = height;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
if (voice->active) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
_as_voice_pan[$0].pan.value = $1;
|
|
|
|
}, voice->index, voice->pan);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain) {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_set_chorus(RID p_voice, float p_chorus) {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb) {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
2015-09-10 03:10:54 +00:00
|
|
|
ERR_FAIL_COND(!voice);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
voice->mix_rate = p_mix_rate;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
if (voice->active) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float freq_diff = Math::log(float(voice->mix_rate) / float(voice->sample_mix_rate)) / Math::log(2.0);
|
|
|
|
int detune = int(freq_diff * 1200.0);
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
_as_voices[$0].detune.value = $1;
|
|
|
|
}, voice->index, detune);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_set_positional(RID p_voice, bool p_positional) {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_volume(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
|
|
|
ERR_FAIL_COND_V(!voice, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return voice->volume;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_pan(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
|
|
|
ERR_FAIL_COND_V(!voice, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return voice->pan;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_pan_depth(RID p_voice) const {
|
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
|
|
|
ERR_FAIL_COND_V(!voice, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return voice->pan_depth;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_pan_height(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
|
|
|
ERR_FAIL_COND_V(!voice, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return voice->pan_height;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioServerJavascript::FilterType AudioServerJavascript::voice_get_filter_type(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return FILTER_NONE;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_filter_cutoff(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_filter_resonance(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_chorus(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioServerJavascript::ReverbRoomType AudioServerJavascript::voice_get_reverb_type(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return REVERB_SMALL;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::voice_get_reverb(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioServerJavascript::voice_get_mix_rate(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 44100;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool AudioServerJavascript::voice_is_positional(RID p_voice) const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::voice_stop(RID p_voice) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
2015-09-10 03:10:54 +00:00
|
|
|
ERR_FAIL_COND(!voice);
|
|
|
|
|
|
|
|
if (voice->active) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
if (_as_voices[$0] !== null) {
|
|
|
|
_as_voices[$0].stop();
|
|
|
|
_as_voices[$0].disconnect(_as_voice_gain[$0]);
|
|
|
|
_as_voices[$0] = null;
|
|
|
|
}
|
|
|
|
}, voice->index);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
voice->active = false;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
bool AudioServerJavascript::voice_is_active(RID p_voice) const {
|
|
|
|
Voice *voice = voice_owner.get(p_voice);
|
|
|
|
ERR_FAIL_COND_V(!voice, false);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return voice->active;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STREAM API */
|
|
|
|
|
|
|
|
RID AudioServerJavascript::audio_stream_create(AudioStream *p_stream) {
|
|
|
|
|
|
|
|
Stream *s = memnew(Stream);
|
2017-03-18 23:36:26 +00:00
|
|
|
s->audio_stream = p_stream;
|
|
|
|
s->event_stream = NULL;
|
|
|
|
s->active = false;
|
|
|
|
s->E = NULL;
|
|
|
|
s->volume_scale = 1.0;
|
2015-09-10 03:10:54 +00:00
|
|
|
p_stream->set_mix_rate(webaudio_mix_rate);
|
|
|
|
|
|
|
|
return stream_owner.make_rid(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
RID AudioServerJavascript::event_stream_create(EventStream *p_stream) {
|
|
|
|
|
|
|
|
Stream *s = memnew(Stream);
|
2017-03-18 23:36:26 +00:00
|
|
|
s->audio_stream = NULL;
|
|
|
|
s->event_stream = p_stream;
|
|
|
|
s->active = false;
|
|
|
|
s->E = NULL;
|
|
|
|
s->volume_scale = 1.0;
|
2015-09-10 03:10:54 +00:00
|
|
|
//p_stream->set_mix_rate(AudioDriverJavascript::get_singleton()->get_mix_rate());
|
|
|
|
|
|
|
|
return stream_owner.make_rid(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioServerJavascript::stream_set_active(RID p_stream, bool p_active) {
|
|
|
|
|
|
|
|
Stream *s = stream_owner.get(p_stream);
|
|
|
|
ERR_FAIL_COND(!s);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (s->active == p_active)
|
2015-09-10 03:10:54 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
s->active = p_active;
|
2015-09-10 03:10:54 +00:00
|
|
|
if (p_active)
|
2017-03-18 23:36:26 +00:00
|
|
|
s->E = active_audio_streams.push_back(s);
|
2015-09-10 03:10:54 +00:00
|
|
|
else {
|
|
|
|
active_audio_streams.erase(s->E);
|
2017-03-18 23:36:26 +00:00
|
|
|
s->E = NULL;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioServerJavascript::stream_is_active(RID p_stream) const {
|
|
|
|
|
|
|
|
Stream *s = stream_owner.get(p_stream);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!s, false);
|
2015-09-10 03:10:54 +00:00
|
|
|
return s->active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioServerJavascript::stream_set_volume_scale(RID p_stream, float p_scale) {
|
|
|
|
|
|
|
|
Stream *s = stream_owner.get(p_stream);
|
|
|
|
ERR_FAIL_COND(!s);
|
2017-03-18 23:36:26 +00:00
|
|
|
s->volume_scale = p_scale;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float AudioServerJavascript::stream_set_volume_scale(RID p_stream) const {
|
|
|
|
|
|
|
|
Stream *s = stream_owner.get(p_stream);
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!s, 0);
|
2015-09-10 03:10:54 +00:00
|
|
|
return s->volume_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Audio Physics API */
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::free(RID p_id) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
if (voice_owner.owns(p_id)) {
|
2017-03-18 23:36:26 +00:00
|
|
|
Voice *voice = voice_owner.get(p_id);
|
2015-09-10 03:10:54 +00:00
|
|
|
ERR_FAIL_COND(!voice);
|
|
|
|
|
|
|
|
if (voice->active) {
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
|
|
|
if (_as_voices[$0] !== null) {
|
2015-09-10 03:10:54 +00:00
|
|
|
_as_voices[$0].stop();
|
2017-01-15 19:00:04 +00:00
|
|
|
_as_voices[$0].disconnect(_as_voice_gain[$0]);
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-01-15 19:00:04 +00:00
|
|
|
}, voice->index);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
2015-09-10 03:10:54 +00:00
|
|
|
delete _as_voices[$0];
|
|
|
|
_as_voice_gain[$0].disconnect(_as_voice_pan[$0]);
|
|
|
|
delete _as_voice_gain[$0];
|
|
|
|
_as_voice_pan[$0].disconnect(_as_audioctx.destination);
|
|
|
|
delete _as_voice_pan[$0];
|
2017-01-15 19:00:04 +00:00
|
|
|
}, voice->index);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
voice_owner.free(p_id);
|
|
|
|
memdelete(voice);
|
|
|
|
|
|
|
|
} else if (sample_owner.owns(p_id)) {
|
|
|
|
|
|
|
|
Sample *sample = sample_owner.get(p_id);
|
|
|
|
ERR_FAIL_COND(!sample);
|
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
|
|
|
EM_ASM_({
|
2015-09-10 03:10:54 +00:00
|
|
|
delete _as_samples[$0];
|
2017-01-15 19:00:04 +00:00
|
|
|
}, sample->index);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
sample_owner.free(p_id);
|
|
|
|
memdelete(sample);
|
|
|
|
|
|
|
|
} else if (stream_owner.owns(p_id)) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Stream *s = stream_owner.get(p_id);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
if (s->active) {
|
2017-03-18 23:36:26 +00:00
|
|
|
stream_set_active(p_id, false);
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(s);
|
|
|
|
stream_owner.free(p_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
void audio_server_mix_function(int p_frames) {
|
|
|
|
|
|
|
|
//print_line("MIXI! "+itos(p_frames));
|
2017-03-18 23:36:26 +00:00
|
|
|
static_cast<AudioServerJavascript *>(AudioServerJavascript::get_singleton())->mix_to_js(p_frames);
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioServerJavascript::mix_to_js(int p_frames) {
|
|
|
|
|
|
|
|
//process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
|
2017-03-18 23:36:26 +00:00
|
|
|
int todo = p_frames;
|
|
|
|
int offset = 0;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (todo) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int tomix = MIN(todo, INTERNAL_BUFFER_SIZE);
|
2015-09-10 03:10:54 +00:00
|
|
|
driver_process_chunk(tomix);
|
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-10 03:10:54 +00:00
|
|
|
EM_ASM_({
|
2017-01-15 19:00:04 +00:00
|
|
|
var data = HEAPF32.subarray($0 / 4, $0 / 4 + $2 * 2);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
for (var channel = 0; channel < _as_output_buffer.numberOfChannels; channel++) {
|
|
|
|
var outputData = _as_output_buffer.getChannelData(channel);
|
|
|
|
// Loop through samples
|
|
|
|
for (var sample = 0; sample < $2; sample++) {
|
|
|
|
// make output equal to the same as the input
|
2017-01-15 19:00:04 +00:00
|
|
|
outputData[sample + $1] = data[sample * 2 + channel];
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 19:00:04 +00:00
|
|
|
}, internal_buffer, offset, tomix);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
todo -= tomix;
|
|
|
|
offset += tomix;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::init() {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/*
|
|
|
|
// clang-format off
|
|
|
|
EM_ASM(
|
|
|
|
console.log('server is ' + audio_server);
|
|
|
|
);
|
|
|
|
// clang-format on
|
|
|
|
*/
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
//int latency = GLOBAL_DEF("javascript/audio_latency",16384);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
internal_buffer_channels = 2;
|
|
|
|
internal_buffer = memnew_arr(float, INTERNAL_BUFFER_SIZE *internal_buffer_channels);
|
|
|
|
stream_buffer = memnew_arr(int32_t, INTERNAL_BUFFER_SIZE * 4); //max 4 channels
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
stream_volume = 0.3;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int buffer_latency = 16384;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-10 03:10:54 +00:00
|
|
|
EM_ASM_( {
|
|
|
|
_as_script_node = _as_audioctx.createScriptProcessor($0, 0, 2);
|
|
|
|
_as_script_node.connect(_as_audioctx.destination);
|
|
|
|
console.log(_as_script_node.bufferSize);
|
|
|
|
|
|
|
|
_as_script_node.onaudioprocess = function(audioProcessingEvent) {
|
|
|
|
// The output buffer contains the samples that will be modified and played
|
|
|
|
_as_output_buffer = audioProcessingEvent.outputBuffer;
|
|
|
|
audio_server_mix_function(_as_output_buffer.getChannelData(0).length);
|
|
|
|
}
|
2017-01-15 19:00:04 +00:00
|
|
|
}, buffer_latency);
|
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::finish() {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::update() {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Stream *>::Element *E = active_audio_streams.front(); E;) { //stream might be removed durnig this callback
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
List<Stream *>::Element *N = E->next();
|
2015-09-10 23:30:46 +00:00
|
|
|
|
|
|
|
if (E->get()->audio_stream)
|
2015-09-10 03:10:54 +00:00
|
|
|
E->get()->audio_stream->update();
|
2015-09-10 23:30:46 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
E = N;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MISC config */
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::lock() {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::unlock() {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioServerJavascript::get_default_channel_count() const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
int AudioServerJavascript::get_default_mix_rate() const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 44100;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::set_stream_global_volume_scale(float p_volume) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
stream_volume_scale = p_volume;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::set_fx_global_volume_scale(float p_volume) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
fx_volume_scale = p_volume;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void AudioServerJavascript::set_event_voice_global_volume_scale(float p_volume) {
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::get_stream_global_volume_scale() const {
|
2015-09-10 03:10:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::get_fx_global_volume_scale() const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
float AudioServerJavascript::get_event_voice_global_volume_scale() const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t AudioServerJavascript::read_output_peak() const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioServerJavascript *AudioServerJavascript::singleton = NULL;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
AudioServer *AudioServerJavascript::get_singleton() {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double AudioServerJavascript::get_mix_time() const {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
double AudioServerJavascript::get_output_delay() const {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioServerJavascript::driver_process_chunk(int p_frames) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int samples = p_frames * internal_buffer_channels;
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < samples; i++) {
|
|
|
|
internal_buffer[i] = 0;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<Stream *>::Element *E = active_audio_streams.front(); E; E = E->next()) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
ERR_CONTINUE(!E->get()->active); // bug?
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AudioStream *as = E->get()->audio_stream;
|
2015-09-10 03:10:54 +00:00
|
|
|
if (!as)
|
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int channels = as->get_channel_count();
|
|
|
|
if (channels == 0)
|
2015-09-10 03:10:54 +00:00
|
|
|
continue; // does not want mix
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!as->mix(stream_buffer, p_frames))
|
2015-09-10 03:10:54 +00:00
|
|
|
continue; //nothing was mixed!!
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int32_t stream_vol_scale = (stream_volume * stream_volume_scale * E->get()->volume_scale) * (1 << STREAM_SCALE_BITS);
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#define STRSCALE(m_val) ((((m_val >> STREAM_SCALE_BITS) * stream_vol_scale) >> 8) / 8388608.0)
|
|
|
|
switch (channels) {
|
2015-09-10 03:10:54 +00:00
|
|
|
case 1: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_frames; i++) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
internal_buffer[(i << 1) + 0] += STRSCALE(stream_buffer[i]);
|
|
|
|
internal_buffer[(i << 1) + 1] += STRSCALE(stream_buffer[i]);
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 2: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_frames * 2; i++) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
internal_buffer[i] += STRSCALE(stream_buffer[i]);
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case 4: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_frames; i++) {
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
internal_buffer[(i << 2) + 0] += STRSCALE((stream_buffer[(i << 2) + 0] + stream_buffer[(i << 2) + 2]) >> 1);
|
|
|
|
internal_buffer[(i << 2) + 1] += STRSCALE((stream_buffer[(i << 2) + 1] + stream_buffer[(i << 2) + 3]) >> 1);
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef STRSCALE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*void AudioServerSW::driver_process(int p_frames,int32_t *p_buffer) {
|
|
|
|
|
|
|
|
|
|
|
|
_output_delay=p_frames/double(AudioDriverSW::get_singleton()->get_mix_rate());
|
|
|
|
//process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
|
|
|
|
int todo=p_frames;
|
|
|
|
while(todo) {
|
|
|
|
|
|
|
|
int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
|
|
|
|
driver_process_chunk(tomix,p_buffer);
|
|
|
|
p_buffer+=tomix;
|
|
|
|
todo-=tomix;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
AudioServerJavascript::AudioServerJavascript() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
singleton = this;
|
|
|
|
sample_base = 1;
|
|
|
|
voice_base = 1;
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-10 03:10:54 +00:00
|
|
|
EM_ASM(
|
2017-01-15 19:00:04 +00:00
|
|
|
_as_samples = {};
|
|
|
|
_as_voices = {};
|
|
|
|
_as_voice_pan = {};
|
|
|
|
_as_voice_gain = {};
|
2015-09-10 03:10:54 +00:00
|
|
|
|
|
|
|
_as_audioctx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
|
|
2018-03-18 15:40:20 +00:00
|
|
|
audio_server_mix_function = cwrap('audio_server_mix_function', null, ['number']);
|
2015-09-10 03:10:54 +00:00
|
|
|
);
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format on */
|
2015-09-10 03:10:54 +00:00
|
|
|
|
2017-01-15 19:00:04 +00:00
|
|
|
/* clang-format off */
|
2015-09-10 03:10:54 +00:00
|
|
|
webaudio_mix_rate = EM_ASM_INT_V(
|
2017-01-15 19:00:04 +00:00
|
|
|
return _as_audioctx.sampleRate;
|
|
|
|
);
|
|
|
|
/* clang-format on */
|
2017-03-18 23:36:26 +00:00
|
|
|
print_line("WEBAUDIO MIX RATE: " + itos(webaudio_mix_rate));
|
|
|
|
event_voice_scale = 1.0;
|
|
|
|
fx_volume_scale = 1.0;
|
|
|
|
stream_volume_scale = 1.0;
|
2015-09-10 03:10:54 +00:00
|
|
|
}
|