summaryrefslogtreecommitdiff
path: root/platform/javascript
diff options
context:
space:
mode:
Diffstat (limited to 'platform/javascript')
-rw-r--r--platform/javascript/SCsub5
-rw-r--r--platform/javascript/audio_server_javascript.cpp805
-rw-r--r--platform/javascript/audio_server_javascript.h198
-rw-r--r--platform/javascript/detect.py4
-rw-r--r--platform/javascript/export/export.cpp36
-rw-r--r--platform/javascript/javascript_main.cpp57
-rw-r--r--platform/javascript/os_javascript.cpp53
-rw-r--r--platform/javascript/os_javascript.h14
8 files changed, 1141 insertions, 31 deletions
diff --git a/platform/javascript/SCsub b/platform/javascript/SCsub
index c1ba0c2283..cd96cf4f31 100644
--- a/platform/javascript/SCsub
+++ b/platform/javascript/SCsub
@@ -3,7 +3,8 @@ Import('env')
javascript_files = [
"os_javascript.cpp",
"audio_driver_javascript.cpp",
- "javascript_main.cpp"
+ "javascript_main.cpp",
+ "audio_server_javascript.cpp"
]
#obj = env.SharedObject('godot_javascript.cpp')
@@ -16,6 +17,8 @@ javascript_objects=[]
for x in javascript_files:
javascript_objects.append( env_javascript.Object( x ) )
+env.Append(LINKFLAGS=["-s","EXPORTED_FUNCTIONS=\"['_main','_audio_server_mix_function','_main_after_fs_sync']\""])
+
prog = None
#env_javascript.SharedLibrary("#platform/javascript/libgodot_javascript.so",[javascript_objects])
diff --git a/platform/javascript/audio_server_javascript.cpp b/platform/javascript/audio_server_javascript.cpp
new file mode 100644
index 0000000000..a2c6740eaf
--- /dev/null
+++ b/platform/javascript/audio_server_javascript.cpp
@@ -0,0 +1,805 @@
+#include "audio_server_javascript.h"
+
+#include "emscripten.h"
+
+AudioMixer *AudioServerJavascript::get_mixer() {
+
+ return NULL;
+}
+
+void AudioServerJavascript::audio_mixer_chunk_callback(int p_frames){
+
+
+}
+
+
+RID AudioServerJavascript::sample_create(SampleFormat p_format, bool p_stereo, int p_length) {
+
+ 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;
+
+ return sample_owner.make_rid(sample);
+
+}
+
+void AudioServerJavascript::sample_set_description(RID p_sample, const String& p_description){
+
+
+}
+String AudioServerJavascript::sample_get_description(RID p_sample, const String& p_description) const{
+
+ return String();
+}
+
+AudioServerJavascript::SampleFormat AudioServerJavascript::sample_get_format(RID p_sample) const{
+
+ return SAMPLE_FORMAT_PCM8;
+}
+bool AudioServerJavascript::sample_is_stereo(RID p_sample) const{
+
+ const Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND_V(!sample,false);
+ return sample->stereo;
+
+}
+int AudioServerJavascript::sample_get_length(RID p_sample) const{
+ const Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND_V(!sample,0);
+ return sample->length;
+}
+const void* AudioServerJavascript::sample_get_data_ptr(RID p_sample) const{
+
+ return NULL;
+}
+
+void AudioServerJavascript::sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer){
+
+ Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND(!sample);
+ int chans = sample->stereo?2:1;
+
+ Vector<float> buffer;
+ 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;
+ }
+ } 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;
+ }
+ } else {
+ ERR_EXPLAIN("Unsupported for now");
+ ERR_FAIL();
+ }
+
+ sample->tmp_data=buffer;
+
+
+
+}
+const DVector<uint8_t> AudioServerJavascript::sample_get_data(RID p_sample) const{
+
+
+ return DVector<uint8_t>();
+}
+
+void AudioServerJavascript::sample_set_mix_rate(RID p_sample,int p_rate){
+ Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND(!sample);
+ sample->mix_rate=p_rate;
+
+}
+
+int AudioServerJavascript::sample_get_mix_rate(RID p_sample) const{
+ const Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND_V(!sample,0);
+ return sample->mix_rate;
+}
+
+
+void AudioServerJavascript::sample_set_loop_format(RID p_sample,SampleLoopFormat p_format){
+
+ Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND(!sample);
+ sample->loop_format=p_format;
+
+}
+
+AudioServerJavascript::SampleLoopFormat AudioServerJavascript::sample_get_loop_format(RID p_sample) const {
+
+ const Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND_V(!sample,SAMPLE_LOOP_NONE);
+ return sample->loop_format;
+}
+
+void AudioServerJavascript::sample_set_loop_begin(RID p_sample,int p_pos){
+
+ Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND(!sample);
+ sample->loop_begin=p_pos;
+
+}
+int AudioServerJavascript::sample_get_loop_begin(RID p_sample) const{
+
+ const Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND_V(!sample,0);
+ return sample->loop_begin;
+}
+
+void AudioServerJavascript::sample_set_loop_end(RID p_sample,int p_pos){
+
+ Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND(!sample);
+ sample->loop_end=p_pos;
+
+}
+int AudioServerJavascript::sample_get_loop_end(RID p_sample) const{
+
+ const Sample *sample = sample_owner.get(p_sample);
+ ERR_FAIL_COND_V(!sample,0);
+ return sample->loop_end;
+}
+
+
+/* VOICE API */
+
+RID AudioServerJavascript::voice_create(){
+
+ Voice *voice = memnew( Voice );
+
+ 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;
+
+ EM_ASM_( {
+ _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);
+
+ voice_base++;
+
+ return voice_owner.make_rid( voice );
+}
+
+void AudioServerJavascript::voice_play(RID p_voice, RID p_sample){
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND(!voice);
+ Sample *sample=sample_owner.get(p_sample);
+ ERR_FAIL_COND(!sample);
+
+ // due to how webaudio works, sample cration is deferred until used
+ // sorry! WebAudio absolutely sucks
+
+
+ if (sample->index==-1) {
+ //create sample if not created
+ ERR_FAIL_COND(sample->tmp_data.size()==0);
+ sample->index=sample_base;
+ EM_ASM_( {
+ _as_samples[$0]=_as_audioctx.createBuffer($1,$2,$3);
+ },sample_base,sample->stereo?2:1,sample->length,sample->mix_rate);
+
+ sample_base++;
+ int chans = sample->stereo?2:1;
+
+
+ for(int i=0;i<chans;i++) {
+
+
+ EM_ASM_({
+ _as_edited_buffer=_as_samples[$0].getChannelData($1);
+ },sample->index,i);
+
+
+ for(int j=0;j<sample->length;j++) {
+
+ EM_ASM_({
+ _as_edited_buffer[$0]=$1;
+ },j,sample->tmp_data[j*chans+i]);
+ }
+ }
+
+ sample->tmp_data.clear();
+ }
+
+
+ voice->sample_mix_rate=sample->mix_rate;
+ if (voice->mix_rate==-1) {
+ voice->mix_rate=voice->sample_mix_rate;
+ }
+
+ 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);
+
+ 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);
+
+ voice->active=true;
+}
+
+void AudioServerJavascript::voice_set_volume(RID p_voice, float p_volume){
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND(!voice);
+
+ voice->volume=p_volume;
+
+ if (voice->active) {
+ EM_ASM_( {
+
+ _as_voice_gain[$0].gain.value=$1;
+
+ },voice->index,voice->volume*fx_volume_scale);
+ }
+
+}
+void AudioServerJavascript::voice_set_pan(RID p_voice, float p_pan, float p_depth,float height){
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND(!voice);
+
+ voice->pan=p_pan;
+ voice->pan_depth=p_depth;
+ voice->pan_height=height;
+
+ if (voice->active) {
+ EM_ASM_( {
+
+ _as_voice_pan[$0].pan.value=$1;
+
+ },voice->index,voice->pan);
+ }
+}
+void AudioServerJavascript::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain){
+
+}
+void AudioServerJavascript::voice_set_chorus(RID p_voice, float p_chorus ){
+
+}
+void AudioServerJavascript::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb){
+
+}
+void AudioServerJavascript::voice_set_mix_rate(RID p_voice, int p_mix_rate){
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND(!voice);
+
+ voice->mix_rate=p_mix_rate;
+
+ if (voice->active) {
+
+ 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);
+ EM_ASM_( {
+
+ _as_voices[$0].detune.value=$1;
+
+ },voice->index,detune);
+ }
+}
+void AudioServerJavascript::voice_set_positional(RID p_voice, bool p_positional){
+
+}
+
+float AudioServerJavascript::voice_get_volume(RID p_voice) const{
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND_V(!voice,0);
+
+ return voice->volume;
+}
+float AudioServerJavascript::voice_get_pan(RID p_voice) const{
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND_V(!voice,0);
+
+ return voice->pan;
+}
+float AudioServerJavascript::voice_get_pan_depth(RID p_voice) const{
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND_V(!voice,0);
+
+ return voice->pan_depth;
+}
+float AudioServerJavascript::voice_get_pan_height(RID p_voice) const{
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND_V(!voice,0);
+
+ return voice->pan_height;
+}
+AudioServerJavascript::FilterType AudioServerJavascript::voice_get_filter_type(RID p_voice) const{
+
+ return FILTER_NONE;
+}
+float AudioServerJavascript::voice_get_filter_cutoff(RID p_voice) const{
+
+ return 0;
+}
+float AudioServerJavascript::voice_get_filter_resonance(RID p_voice) const{
+
+ return 0;
+}
+float AudioServerJavascript::voice_get_chorus(RID p_voice) const{
+
+ return 0;
+}
+AudioServerJavascript::ReverbRoomType AudioServerJavascript::voice_get_reverb_type(RID p_voice) const{
+
+ return REVERB_SMALL;
+}
+float AudioServerJavascript::voice_get_reverb(RID p_voice) const{
+
+ return 0;
+}
+
+int AudioServerJavascript::voice_get_mix_rate(RID p_voice) const{
+
+ return 44100;
+}
+
+bool AudioServerJavascript::voice_is_positional(RID p_voice) const{
+
+ return false;
+}
+
+void AudioServerJavascript::voice_stop(RID p_voice){
+
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND(!voice);
+
+ if (voice->active) {
+
+ 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);
+
+ voice->active=false;
+ }
+
+
+}
+bool AudioServerJavascript::voice_is_active(RID p_voice) const{
+ Voice* voice=voice_owner.get(p_voice);
+ ERR_FAIL_COND_V(!voice,false);
+
+ return voice->active;
+}
+
+/* STREAM API */
+
+RID AudioServerJavascript::audio_stream_create(AudioStream *p_stream) {
+
+
+ Stream *s = memnew(Stream);
+ s->audio_stream=p_stream;
+ s->event_stream=NULL;
+ s->active=false;
+ s->E=NULL;
+ s->volume_scale=1.0;
+ 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);
+ s->audio_stream=NULL;
+ s->event_stream=p_stream;
+ s->active=false;
+ s->E=NULL;
+ s->volume_scale=1.0;
+ //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);
+
+ if (s->active==p_active)
+ return;
+
+ s->active=p_active;
+ if (p_active)
+ s->E=active_audio_streams.push_back(s);
+ else {
+ active_audio_streams.erase(s->E);
+ s->E=NULL;
+ }
+}
+
+bool AudioServerJavascript::stream_is_active(RID p_stream) const {
+
+ Stream *s = stream_owner.get(p_stream);
+ ERR_FAIL_COND_V(!s,false);
+ 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);
+ s->volume_scale=p_scale;
+
+}
+
+float AudioServerJavascript::stream_set_volume_scale(RID p_stream) const {
+
+ Stream *s = stream_owner.get(p_stream);
+ ERR_FAIL_COND_V(!s,0);
+ return s->volume_scale;
+
+}
+
+
+/* Audio Physics API */
+
+void AudioServerJavascript::free(RID p_id){
+
+ if (voice_owner.owns(p_id)) {
+ Voice* voice=voice_owner.get(p_id);
+ ERR_FAIL_COND(!voice);
+
+ if (voice->active) {
+ EM_ASM_( {
+ if (_as_voices[$0]!==null) {
+ _as_voices[$0].stop();
+ _as_voices[$0].disconnect(_as_voice_gain[$0]);
+ }
+ },voice->index);
+ }
+
+ EM_ASM_( {
+ 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];
+
+ },voice->index);
+
+ 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);
+
+ EM_ASM_( {
+ delete _as_samples[$0];
+
+ },sample->index);
+
+ sample_owner.free(p_id);
+ memdelete(sample);
+
+ } else if (stream_owner.owns(p_id)) {
+
+
+ Stream *s=stream_owner.get(p_id);
+
+ if (s->active) {
+ stream_set_active(p_id,false);
+ }
+
+ memdelete(s);
+ stream_owner.free(p_id);
+ }
+}
+
+extern "C" {
+
+
+void audio_server_mix_function(int p_frames) {
+
+ //print_line("MIXI! "+itos(p_frames));
+ static_cast<AudioServerJavascript*>(AudioServerJavascript::get_singleton())->mix_to_js(p_frames);
+}
+
+}
+
+void AudioServerJavascript::mix_to_js(int p_frames) {
+
+
+ //process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
+ int todo=p_frames;
+ int offset=0;
+
+ while(todo) {
+
+ int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
+ driver_process_chunk(tomix);
+
+
+ EM_ASM_({
+
+ var data = HEAPF32.subarray($0/4, $0/4 + $2*2);
+
+ 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
+ outputData[sample+$1] = data[sample*2+channel];
+ }
+ }
+
+ },internal_buffer,offset,tomix);
+
+ todo-=tomix;
+ offset+=tomix;
+ }
+}
+
+void AudioServerJavascript::init(){
+
+ //EM_ASM(
+// console.log('server is '+audio_server);
+// );
+
+
+ //int latency = GLOBAL_DEF("javascript/audio_latency",16384);
+
+ 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
+
+ stream_volume=0.3;
+
+ int buffer_latency=16384;
+
+ 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);
+ }
+ },buffer_latency);
+
+
+}
+
+void AudioServerJavascript::finish(){
+
+}
+void AudioServerJavascript::update(){
+
+ for(List<Stream*>::Element *E=active_audio_streams.front();E;) { //stream might be removed durnig this callback
+
+ List<Stream*>::Element *N=E->next();
+
+ if (E->get()->audio_stream)
+ E->get()->audio_stream->update();
+
+ E=N;
+ }
+}
+
+/* MISC config */
+
+void AudioServerJavascript::lock(){
+
+}
+void AudioServerJavascript::unlock(){
+
+}
+int AudioServerJavascript::get_default_channel_count() const{
+
+ return 1;
+}
+int AudioServerJavascript::get_default_mix_rate() const{
+
+ return 44100;
+}
+
+void AudioServerJavascript::set_stream_global_volume_scale(float p_volume){
+
+ stream_volume_scale=p_volume;
+}
+void AudioServerJavascript::set_fx_global_volume_scale(float p_volume){
+
+ fx_volume_scale=p_volume;
+}
+void AudioServerJavascript::set_event_voice_global_volume_scale(float p_volume){
+
+}
+
+float AudioServerJavascript::get_stream_global_volume_scale() const{
+ return 1;
+}
+float AudioServerJavascript::get_fx_global_volume_scale() const{
+
+ return 1;
+}
+float AudioServerJavascript::get_event_voice_global_volume_scale() const{
+
+ return 1;
+}
+
+uint32_t AudioServerJavascript::read_output_peak() const{
+
+ return 0;
+}
+
+AudioServerJavascript *AudioServerJavascript::singleton=NULL;
+
+AudioServer *AudioServerJavascript::get_singleton() {
+ return singleton;
+}
+
+double AudioServerJavascript::get_mix_time() const{
+
+ return 0;
+}
+double AudioServerJavascript::get_output_delay() const {
+
+ return 0;
+}
+
+
+void AudioServerJavascript::driver_process_chunk(int p_frames) {
+
+
+
+ int samples=p_frames*internal_buffer_channels;
+
+ for(int i=0;i<samples;i++) {
+ internal_buffer[i]=0;
+ }
+
+
+ for(List<Stream*>::Element *E=active_audio_streams.front();E;E=E->next()) {
+
+ ERR_CONTINUE(!E->get()->active); // bug?
+
+
+ AudioStream *as=E->get()->audio_stream;
+ if (!as)
+ continue;
+
+ int channels=as->get_channel_count();
+ if (channels==0)
+ continue; // does not want mix
+ if (!as->mix(stream_buffer,p_frames))
+ continue; //nothing was mixed!!
+
+ int32_t stream_vol_scale=(stream_volume*stream_volume_scale*E->get()->volume_scale)*(1<<STREAM_SCALE_BITS);
+
+#define STRSCALE(m_val) ((((m_val>>STREAM_SCALE_BITS)*stream_vol_scale)>>8)/8388608.0)
+ switch(channels) {
+ case 1: {
+
+ for(int i=0;i<p_frames;i++) {
+
+ internal_buffer[(i<<1)+0]+=STRSCALE(stream_buffer[i]);
+ internal_buffer[(i<<1)+1]+=STRSCALE(stream_buffer[i]);
+ }
+ } break;
+ case 2: {
+
+ for(int i=0;i<p_frames*2;i++) {
+
+ internal_buffer[i]+=STRSCALE(stream_buffer[i]);
+ }
+ } break;
+ case 4: {
+
+ for(int i=0;i<p_frames;i++) {
+
+ 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);
+ }
+ } 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() {
+
+ singleton=this;
+ sample_base=1;
+ voice_base=1;
+ EM_ASM(
+ _as_samples={};
+ _as_voices={};
+ _as_voice_pan={};
+ _as_voice_gain={};
+
+ _as_audioctx = new (window.AudioContext || window.webkitAudioContext)();
+
+ audio_server_mix_function = Module.cwrap('audio_server_mix_function', 'void', ['number']);
+ );
+
+ webaudio_mix_rate = EM_ASM_INT_V(
+ return _as_audioctx.sampleRate;
+ );
+ print_line("WEBAUDIO MIX RATE: "+itos(webaudio_mix_rate));
+ event_voice_scale=1.0;
+ fx_volume_scale=1.0;
+ stream_volume_scale=1.0;
+
+}
diff --git a/platform/javascript/audio_server_javascript.h b/platform/javascript/audio_server_javascript.h
new file mode 100644
index 0000000000..450237d324
--- /dev/null
+++ b/platform/javascript/audio_server_javascript.h
@@ -0,0 +1,198 @@
+#ifndef AUDIO_SERVER_JAVASCRIPT_H
+#define AUDIO_SERVER_JAVASCRIPT_H
+
+
+#include "servers/audio_server.h"
+
+class AudioServerJavascript : public AudioServer {
+
+ OBJ_TYPE(AudioServerJavascript,AudioServer);
+
+ enum {
+ INTERNAL_BUFFER_SIZE=4096,
+ STREAM_SCALE_BITS=12
+
+ };
+
+ AudioMixer *get_mixer();
+ void audio_mixer_chunk_callback(int p_frames);
+
+ struct Sample {
+ SampleFormat format;
+ SampleLoopFormat loop_format;
+ int loop_begin;
+ int loop_end;
+ int length;
+ int index;
+ int mix_rate;
+ bool stereo;
+
+ Vector<float> tmp_data;
+ };
+
+ mutable RID_Owner<Sample> sample_owner;
+ int sample_base;
+
+ struct Voice {
+ int index;
+ float volume;
+ float pan;
+ float pan_depth;
+ float pan_height;
+
+ float chorus;
+ ReverbRoomType reverb_type;
+ float reverb;
+
+ int mix_rate;
+ int sample_mix_rate;
+ bool positional;
+
+ bool active;
+
+ };
+
+ mutable RID_Owner<Voice> voice_owner;
+
+ int voice_base;
+
+ struct Stream {
+ bool active;
+ List<Stream*>::Element *E;
+ AudioStream *audio_stream;
+ EventStream *event_stream;
+ float volume_scale;
+ };
+
+ List<Stream*> active_audio_streams;
+
+ //List<Stream*> event_streams;
+
+ float * internal_buffer;
+ int internal_buffer_channels;
+ int32_t * stream_buffer;
+
+ mutable RID_Owner<Stream> stream_owner;
+
+ float stream_volume;
+ float stream_volume_scale;
+
+ float event_voice_scale;
+ float fx_volume_scale;
+
+
+ void driver_process_chunk(int p_frames);
+
+ int webaudio_mix_rate;
+
+
+ static AudioServerJavascript *singleton;
+public:
+
+ void mix_to_js(int p_frames);
+ /* SAMPLE API */
+
+ virtual RID sample_create(SampleFormat p_format, bool p_stereo, int p_length);
+
+ virtual void sample_set_description(RID p_sample, const String& p_description);
+ virtual String sample_get_description(RID p_sample, const String& p_description) const;
+
+ virtual SampleFormat sample_get_format(RID p_sample) const;
+ virtual bool sample_is_stereo(RID p_sample) const;
+ virtual int sample_get_length(RID p_sample) const;
+ virtual const void* sample_get_data_ptr(RID p_sample) const;
+
+
+ virtual void sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer);
+ virtual const DVector<uint8_t> sample_get_data(RID p_sample) const;
+
+ virtual void sample_set_mix_rate(RID p_sample,int p_rate);
+ virtual int sample_get_mix_rate(RID p_sample) const;
+
+ virtual void sample_set_loop_format(RID p_sample,SampleLoopFormat p_format);
+ virtual SampleLoopFormat sample_get_loop_format(RID p_sample) const;
+
+ virtual void sample_set_loop_begin(RID p_sample,int p_pos);
+ virtual int sample_get_loop_begin(RID p_sample) const;
+
+ virtual void sample_set_loop_end(RID p_sample,int p_pos);
+ virtual int sample_get_loop_end(RID p_sample) const;
+
+
+ /* VOICE API */
+
+ virtual RID voice_create();
+
+ virtual void voice_play(RID p_voice, RID p_sample);
+
+ virtual void voice_set_volume(RID p_voice, float p_volume);
+ virtual void voice_set_pan(RID p_voice, float p_pan, float p_depth=0,float height=0); //pan and depth go from -1 to 1
+ virtual void voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance, float p_gain=0);
+ virtual void voice_set_chorus(RID p_voice, float p_chorus );
+ virtual void voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb);
+ virtual void voice_set_mix_rate(RID p_voice, int p_mix_rate);
+ virtual void voice_set_positional(RID p_voice, bool p_positional);
+
+ virtual float voice_get_volume(RID p_voice) const;
+ virtual float voice_get_pan(RID p_voice) const; //pan and depth go from -1 to 1
+ virtual float voice_get_pan_depth(RID p_voice) const; //pan and depth go from -1 to 1
+ virtual float voice_get_pan_height(RID p_voice) const; //pan and depth go from -1 to 1
+ virtual FilterType voice_get_filter_type(RID p_voice) const;
+ virtual float voice_get_filter_cutoff(RID p_voice) const;
+ virtual float voice_get_filter_resonance(RID p_voice) const;
+ virtual float voice_get_chorus(RID p_voice) const;
+ virtual ReverbRoomType voice_get_reverb_type(RID p_voice) const;
+ virtual float voice_get_reverb(RID p_voice) const;
+
+ virtual int voice_get_mix_rate(RID p_voice) const;
+ virtual bool voice_is_positional(RID p_voice) const;
+
+ virtual void voice_stop(RID p_voice);
+ virtual bool voice_is_active(RID p_voice) const;
+
+ /* STREAM API */
+
+ virtual RID audio_stream_create(AudioStream *p_stream);
+ virtual RID event_stream_create(EventStream *p_stream);
+
+ virtual void stream_set_active(RID p_stream, bool p_active);
+ virtual bool stream_is_active(RID p_stream) const;
+
+ virtual void stream_set_volume_scale(RID p_stream, float p_scale);
+ virtual float stream_set_volume_scale(RID p_stream) const;
+
+ /* Audio Physics API */
+
+ virtual void free(RID p_id);
+
+ virtual void init();
+ virtual void finish();
+ virtual void update();
+
+ /* MISC config */
+
+ virtual void lock();
+ virtual void unlock();
+ virtual int get_default_channel_count() const;
+ virtual int get_default_mix_rate() const;
+
+ virtual void set_stream_global_volume_scale(float p_volume);
+ virtual void set_fx_global_volume_scale(float p_volume);
+ virtual void set_event_voice_global_volume_scale(float p_volume);
+
+ virtual float get_stream_global_volume_scale() const;
+ virtual float get_fx_global_volume_scale() const;
+ virtual float get_event_voice_global_volume_scale() const;
+
+ virtual uint32_t read_output_peak() const;
+
+ static AudioServer *get_singleton();
+
+ virtual double get_mix_time() const; //useful for video -> audio sync
+ virtual double get_output_delay() const;
+
+
+ AudioServerJavascript();
+};
+
+#endif // AUDIO_SERVER_JAVASCRIPT_H
diff --git a/platform/javascript/detect.py b/platform/javascript/detect.py
index 947a637fb9..ec21bf6ee4 100644
--- a/platform/javascript/detect.py
+++ b/platform/javascript/detect.py
@@ -30,7 +30,6 @@ def get_flags():
('theora', 'no'),
('tools', 'no'),
('nedmalloc', 'no'),
- ('vorbis', 'no'),
('musepack', 'no'),
('squirrel', 'no'),
('squish', 'no'),
@@ -77,6 +76,9 @@ def configure(env):
#env.Append(CCFLAGS=['-D_DEBUG', '-Wall', '-g4', '-DDEBUG_ENABLED'])
env.Append(CPPFLAGS=['-DDEBUG_MEMORY_ALLOC'])
+ if(env["opus"]=="yes"):
+ env.opus_fixed_point="yes"
+
env.Append(CPPFLAGS=["-fno-exceptions",'-DNO_SAFE_CAST','-fno-rtti'])
env.Append(CPPFLAGS=['-DJAVASCRIPT_ENABLED', '-DUNIX_ENABLED', '-DNO_FCNTL','-DMPC_FIXED_POINT','-DTYPED_METHOD_BIND','-DNO_THREADS'])
env.Append(CPPFLAGS=['-DGLES2_ENABLED'])
diff --git a/platform/javascript/export/export.cpp b/platform/javascript/export/export.cpp
index b262684a59..9e2595f4a1 100644
--- a/platform/javascript/export/export.cpp
+++ b/platform/javascript/export/export.cpp
@@ -77,11 +77,11 @@ public:
virtual int get_device_count() const { return show_run?1:0; };
virtual String get_device_name(int p_device) const { return "Run in Browser"; }
virtual String get_device_info(int p_device) const { return "Run exported HTML in the system's default browser."; }
- virtual Error run(int p_device,bool p_dumb=false,bool p_remote_debug=false);
+ virtual Error run(int p_device,int p_flags=0);
virtual bool requieres_password(bool p_debug) const { return false; }
virtual String get_binary_extension() const { return "html"; }
- virtual Error export_project(const String& p_path,bool p_debug,bool p_dumb=false,bool p_remote_debug=false);
+ virtual Error export_project(const String& p_path,bool p_debug,int p_flags=0);
virtual bool can_export(String *r_error=NULL) const;
@@ -144,12 +144,16 @@ static void _fix_html(Vector<uint8_t>& html,const String& name,int max_memory) {
str.parse_utf8((const char*)html.ptr(),html.size());
Vector<String> lines=str.split("\n");
for(int i=0;i<lines.size();i++) {
- if (lines[i].find("godot.js")!=-1) {
- strnew+="<script type=\"text/javascript\" src=\""+name+"_filesystem.js\"></script>\n";
- strnew+="<script async type=\"text/javascript\" src=\""+name+".js\"></script>\n";
- } else if (lines[i].find("var Module")!=-1) {
- strnew+=lines[i];
- strnew+="TOTAL_MEMORY:"+itos(max_memory*1024*1024)+",";
+
+ if (lines[i].find("$GODOTTMEM")!=-1) {
+
+ strnew+=lines[i].replace("$GODOTTMEM",itos(max_memory*1024*1024))+"\n";
+ } else if (lines[i].find("$GODOTFS")!=-1) {
+ strnew+=lines[i].replace("$GODOTFS",name+"fs.js")+"\n";
+ } else if (lines[i].find("$GODOTMEM")!=-1) {
+ strnew+=lines[i].replace("$GODOTMEM",name+".mem")+"\n";
+ } else if (lines[i].find("$GODOTJS")!=-1) {
+ strnew+=lines[i].replace("$GODOTJS",name+".js")+"\n";
} else {
strnew+=lines[i]+"\n";
}
@@ -194,7 +198,7 @@ struct JSExportData {
-Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool p_debug, bool p_dumb, bool p_remote_debug) {
+Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool p_debug, int p_flags) {
String src_template;
@@ -267,10 +271,10 @@ Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool
_fix_html(data,p_path.get_file().basename(),1<<(max_memory+5));
file=p_path.get_file();
}
- if (file=="filesystem.js") {
+ if (file=="godotfs.js") {
_fix_files(data,len);
- file=p_path.get_file().basename()+"_filesystem.js";
+ file=p_path.get_file().basename()+"fs.js";
}
if (file=="godot.js") {
@@ -278,6 +282,12 @@ Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool
file=p_path.get_file().basename()+".js";
}
+ if (file=="godot.mem") {
+
+ //_fix_godot(data);
+ file=p_path.get_file().basename()+".mem";
+ }
+
String dst = p_path.get_base_dir().plus_file(file);
FileAccess *f=FileAccess::open(dst,FileAccess::WRITE);
if (!f) {
@@ -299,10 +309,10 @@ Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool
}
-Error EditorExportPlatformJavaScript::run(int p_device, bool p_dumb, bool p_remote_debug) {
+Error EditorExportPlatformJavaScript::run(int p_device, int p_flags) {
String path = EditorSettings::get_singleton()->get_settings_path()+"/tmp/tmp_export.html";
- Error err = export_project(path,true,"");
+ Error err = export_project(path,true,p_flags);
if (err)
return err;
diff --git a/platform/javascript/javascript_main.cpp b/platform/javascript/javascript_main.cpp
index 5be6c5b647..fb87dc848e 100644
--- a/platform/javascript/javascript_main.cpp
+++ b/platform/javascript/javascript_main.cpp
@@ -31,6 +31,9 @@
#include "main/main.h"
#include "io/resource_loader.h"
#include "os/keyboard.h"
+#include "emscripten.h"
+#include <string.h>
+
OS_JavaScript *os=NULL;
static void _gfx_init(void *ud,bool gl2,int w, int h,bool fs) {
@@ -196,15 +199,39 @@ static void _gfx_idle() {
glutPostRedisplay();
}
+int start_step=0;
+
static void _godot_draw(void) {
- os->main_loop_iterate();
+ if (start_step==1) {
+ start_step=2;
+ Main::start();
+ os->main_loop_begin();
+ }
+
+ if (start_step==2) {
+ os->main_loop_iterate();
+ }
+
glutSwapBuffers();
}
+
+
+extern "C" {
+
+void main_after_fs_sync(int value) {
+
+ start_step=1;
+ printf("FS SYNCHED!\n");
+}
+
+}
+
int main(int argc, char *argv[]) {
- /* Initialize the window */
+
+ /* Initialize the window */
printf("let it go!\n");
glutInit(&argc, argv);
os = new OS_JavaScript(_gfx_init,NULL,NULL,NULL,NULL);
@@ -218,7 +245,7 @@ int main(int argc, char *argv[]) {
#endif
ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility
- Main::start();
+
glutSpecialUpFunc(_glut_skey_up);
glutSpecialFunc(_glut_skey_down);
@@ -236,10 +263,32 @@ int main(int argc, char *argv[]) {
// glutReshapeFunc(gears_reshape);
glutDisplayFunc(_godot_draw);
//glutSpecialFunc(gears_special);
- os->main_loop_begin();
+
+
+
+ //mount persistent filesystem
+ EM_ASM(
+ FS.mkdir('/userfs');
+ FS.mount(IDBFS, {}, '/userfs');
+
+
+
+ // sync from persisted state into memory and then
+ // run the 'test' function
+ FS.syncfs(true, function (err) {
+ assert(!err);
+ console.log("done syncinc!");
+ _after_sync_cb = Module.cwrap('main_after_fs_sync', 'void',['number']);
+ _after_sync_cb(0);
+
+ });
+
+ );
glutMainLoop();
+
+
return 0;
}
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp
index a422f77b4b..ae97bf990b 100644
--- a/platform/javascript/os_javascript.cpp
+++ b/platform/javascript/os_javascript.cpp
@@ -37,6 +37,7 @@
#include "main/main.h"
#include "core/globals.h"
+#include "emscripten.h"
int OS_JavaScript::get_video_driver_count() const {
@@ -104,21 +105,21 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
visual_server->init();
visual_server->cursor_set_visible(false, 0);
- AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
+ /*AudioDriverManagerSW::get_driver(p_audio_driver)->set_singleton();
if (AudioDriverManagerSW::get_driver(p_audio_driver)->init()!=OK) {
ERR_PRINT("Initializing audio failed.");
- }
+ }*/
print_line("Init SM");
- sample_manager = memnew( SampleManagerMallocSW );
- audio_server = memnew( AudioServerSW(sample_manager) );
+ //sample_manager = memnew( SampleManagerMallocSW );
+ audio_server = memnew( AudioServerJavascript );
print_line("Init Mixer");
- audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
+ //audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
audio_server->init();
print_line("Init SoundServer");
@@ -270,6 +271,32 @@ bool OS_JavaScript::main_loop_iterate() {
if (!main_loop)
return false;
+
+ if (time_to_save_sync>=0) {
+ int64_t newtime = get_ticks_msec();
+ int64_t elapsed = newtime - last_sync_time;
+ last_sync_time=newtime;
+
+ time_to_save_sync-=elapsed;
+
+ print_line("elapsed "+itos(elapsed)+" tts "+itos(time_to_save_sync));
+
+ if (time_to_save_sync<0) {
+ //time to sync, for real
+ // run 'success'
+ print_line("DOING SYNCH!");
+ EM_ASM(
+ FS.syncfs(function (err) {
+ assert(!err);
+ console.log("Synched!");
+ //ccall('success', 'v');
+ });
+ );
+ }
+
+
+ }
+
return Main::iteration();
}
@@ -562,14 +589,21 @@ String OS_JavaScript::get_locale() const {
String OS_JavaScript::get_data_dir() const {
- if (get_data_dir_func)
- return get_data_dir_func();
- return "/";
+ //if (get_data_dir_func)
+ // return get_data_dir_func();
+ return "/userfs";
//return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
};
+void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) {
+ print_line("close "+p_file+" flags "+itos(p_flags));
+ if (p_file.begins_with("/userfs") && p_flags&FileAccess::WRITE) {
+ static_cast<OS_JavaScript*>(get_singleton())->last_sync_time=OS::get_singleton()->get_ticks_msec();
+ static_cast<OS_JavaScript*>(get_singleton())->time_to_save_sync=5000; //five seconds since last save
+ }
+}
OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func) {
@@ -589,6 +623,9 @@ OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, Ope
open_uri_func=p_open_uri_func;
get_data_dir_func=p_get_data_dir_func;
get_locale_func=p_get_locale_func;
+ FileAccessUnix::close_notification_func=_close_notification_funcs;
+
+ time_to_save_sync=-1;
}
diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h
index 81bb474401..1e925fb8df 100644
--- a/platform/javascript/os_javascript.h
+++ b/platform/javascript/os_javascript.h
@@ -38,8 +38,9 @@
#include "servers/audio/audio_server_sw.h"
#include "servers/physics_2d/physics_2d_server_sw.h"
#include "servers/visual/rasterizer.h"
-
+#include "audio_server_javascript.h"
#include "audio_driver_javascript.h"
+#include "main/input_default.h"
typedef void (*GFXInitFunc)(void *ud,bool gl2,int w, int h, bool fs);
typedef int (*OpenURIFunc)(const String&);
@@ -65,10 +66,13 @@ private:
bool use_gl2;
+ int64_t time_to_save_sync;
+ int64_t last_sync_time;
+
Rasterizer *rasterizer;
VisualServer *visual_server;
- AudioServerSW *audio_server;
- SampleManagerMallocSW *sample_manager;
+ AudioServerJavascript *audio_server;
+ //SampleManagerMallocSW *sample_manager;
SpatialSoundServerSW *spatial_sound_server;
SpatialSound2DServerSW *spatial_sound_2d_server;
PhysicsServer *physics_server;
@@ -84,6 +88,8 @@ private:
GetDataDirFunc get_data_dir_func;
GetLocaleFunc get_locale_func;
+ static void _close_notification_funcs(const String& p_file,int p_flags);
+
public:
// functions used by main to initialize/deintialize the OS
@@ -106,7 +112,7 @@ public:
typedef int64_t ProcessID;
- static OS* get_singleton();
+ //static OS* get_singleton();
virtual void vprint(const char* p_format, va_list p_list, bool p_stderr=false);
virtual void print(const char *p_format, ... );