websockets-streaming-audio
Advanced tools
Comparing version 0.5.1 to 0.6.1
npm start > src_mod/server_log.js 2>&1 & | ||
20140817 | ||
@@ -4,0 +6,0 @@ |
@@ -37,3 +37,3 @@ { | ||
"subdomain": "websockets-streaming-audio", | ||
"version": "0.5.1" | ||
"version": "0.6.1" | ||
} |
@@ -26,8 +26,4 @@ websockets-streaming-audio | ||
``` | ||
Change directory into the project folder websockets-streaming-audio | ||
Change directory into the project folder: | ||
```bash | ||
cd websockets-streaming-audio | ||
``` | ||
Then install the dependent modules: | ||
@@ -34,0 +30,0 @@ |
@@ -5,333 +5,72 @@ | ||
var audio_context; | ||
var client_memory; | ||
/* | ||
2^2 4 | ||
2^3 8 | ||
2^4 16 | ||
2^5 32 | ||
2^6 64 | ||
2^7 128 | ||
2^8 256 | ||
2^9 512 | ||
2^10 1024 | ||
2^11 2048 | ||
2^12 4096 | ||
2^13 8192 | ||
2^14 16384 | ||
2^15 32768 | ||
2^16 65536 | ||
2^17 131072 | ||
2^18 262144 | ||
2^19 524288 | ||
2^20 1048576 | ||
2^21 2097152 | ||
2^22 4194304 | ||
2^23 8388608 | ||
2^24 16777216 | ||
2^25 33554432 | ||
2^26 67108864 | ||
2^27 134217728 | ||
2^28 268435456 | ||
2^29 536870912 | ||
2^30 1073741824 | ||
2^31 2147483648 | ||
2^32 4294967296 | ||
*/ | ||
// BUFF_SIZE_AUDIO_RENDERER must be in units of sample frames, i.e., one of: | ||
// 256, 512, 1024, 2048, 4096, 8192, or 16384 | ||
// var BUFF_SIZE_AUDIO_RENDERER = 2048; | ||
// var BUFF_SIZE_AUDIO_RENDERER = 4096; | ||
// var BUFF_SIZE_AUDIO_RENDERER = 8192; | ||
var BUFF_SIZE_AUDIO_RENDERER = 16384; | ||
var count_num_buffers_received_from_server = 0; | ||
var audio_obj_from_server = {}; | ||
var flag_audio_rendering = false; | ||
var terminate_current_run = false; | ||
var we_retrieved_last_chunk_from_server = false; | ||
var curr_web_audio_obj; | ||
var curr_circular_queue_memory_buffer_obj; | ||
var request_number = 0; | ||
var msgs_to_server = {}; | ||
var timer_Id; // for setIterval clearing once complete | ||
// ---------------------------------------- // | ||
var BUFFER_SIZE_STREAM_QUEUE; // stens TODO - wrap into a circular queue | ||
var curr_index_stream_buffer = 0; | ||
var server_side_audio_obj; | ||
function entry_point(given_request) { | ||
var streaming_audio_obj = { | ||
switch (given_request) { | ||
index_stream : 0 | ||
}; | ||
case "init_audio_context" : { | ||
var final_index; // suppled by server side ... only populated when server senses source media has been exhausted | ||
var streaming_status_ready = "streaming_status_ready"; | ||
var streaming_status_preloading = "streaming_status_preloading"; | ||
var streaming_status_active = "streaming_status_active"; | ||
var streaming_status_done = "streaming_status_done"; | ||
// curr_web_audio_obj = client_web_audio(); | ||
curr_web_audio_obj = Object.create(client_web_audio()); | ||
var flag_streaming_status = streaming_status_ready; // when server side signals stream is done this becomes false | ||
curr_web_audio_obj.init_audio_context(); | ||
// ------------------------ // | ||
break; | ||
} | ||
function init_audio_context() { | ||
case "stream_audio" : { | ||
if (typeof audio_context !== "undefined") { | ||
console.log("stream audio from server to client browser"); | ||
// console.log("audio_context already defined"); | ||
return; | ||
} | ||
// if (flag_streaming_status !== streaming_status_ready) { | ||
// if (! curr_web_audio_obj.is_streaming_status_ready()) { | ||
if (curr_web_audio_obj && (! curr_web_audio_obj.is_streaming_status_ready()) { | ||
try { | ||
console.log("flag_streaming_status ", curr_web_audio_obj.get_streaming_status()); | ||
window.AudioContext = window.AudioContext || | ||
window.webkitAudioContext || | ||
window.mozAudioContext || | ||
window.oAudioContext || | ||
window.msAudioContext; | ||
console.error("NOTICE - currently is middle of previous streaming request ... try later"); | ||
return; | ||
} | ||
audio_context = new AudioContext(); | ||
// if (request_number === 0) { | ||
console.log("cool audio context established ... audio_context "); | ||
request_number = new Date().getTime(); | ||
} catch (e) { | ||
// } else { | ||
alert("Web Audio API is not supported by this browser\n ... http://caniuse.com/#feat=audio-api"); | ||
} | ||
// console.log("We already have a request ... request_number ", request_number); | ||
// return; | ||
// } | ||
// --- | ||
// --- | ||
gain_node = audio_context.createGain(); // Declare gain node | ||
gain_node.connect(audio_context.destination); // Connect gain node to speakers | ||
}; | ||
curr_circular_queue_memory_buffer_obj = null; | ||
// ----------------- // | ||
curr_circular_queue_memory_buffer_obj = Object.create(circular_queue()); | ||
function cb_stream_is_complete(given_max_index) { | ||
console.log("curr_circular_queue_memory_buffer_obj ", curr_circular_queue_memory_buffer_obj); | ||
final_index = given_max_index; | ||
// --- | ||
flag_streaming_status = streaming_status_done; | ||
curr_web_audio_obj = null; | ||
// we_retrieved_last_chunk_from_server = true; | ||
curr_web_audio_obj = Object.create(client_web_audio()); | ||
client_memory_mgr.set_terminal_index(final_index); | ||
console.log("curr_web_audio_obj ", curr_web_audio_obj); | ||
console.log("cb_stream_is_complete final_index ", final_index, " flag_streaming_status ", flag_streaming_status); | ||
}; | ||
curr_web_audio_obj.init_audio_context(); | ||
// --- | ||
curr_web_audio_obj.set_circular_queue(curr_circular_queue_memory_buffer_obj); | ||
var gain_node; | ||
var streaming_node; | ||
var allow_synth = false; | ||
var in_middle_of_playback = false; | ||
function set_false_in_middle_of_playback() { | ||
in_middle_of_playback = false; | ||
console.log('just set false to in_middle_of_playback'); | ||
} | ||
function stop_audio(given_node) { | ||
given_node.disconnect(gain_node); | ||
given_node.onaudioprocess = null; | ||
given_node = null; | ||
console.log('stop_audio ... just called disconnect'); | ||
console.log('stop_audio ... just called disconnect'); | ||
console.log('stop_audio ... just called disconnect'); | ||
flag_streaming_status = streaming_status_ready; // get ready for next time | ||
// flag_audio_rendering = false; | ||
terminate_current_run = true; | ||
set_false_in_middle_of_playback(); | ||
client_socket_comms.socket_client(msgs_to_server.mode_stop_streaming); | ||
clearInterval(timer_Id); | ||
}; | ||
// ------------------------ | ||
function setup_onaudioprocess_callback_stream(given_node, circular_queue_obj, cb_get_memory_chunk, done_callback) { | ||
// var curr_index_synth_buffer = 0; // keep playing until this reaches size of synth buffer | ||
var internal_audio_buffer_obj = {}; | ||
given_node.onaudioprocess = (function() { | ||
return function(event) { | ||
// if (terminate_current_run) { return;}; | ||
console.log("Middleburg top of rendering callback "); | ||
internal_audio_buffer_obj.buffer = event.outputBuffer.getChannelData(0);// stens TODO - setup 2 channels | ||
console.log("SIZE internal_audio_buffer_obj.buffer ", internal_audio_buffer_obj.buffer.length); | ||
console.log("about to call is_consumption_possible"); | ||
if ((! we_retrieved_last_chunk_from_server) && circular_queue_obj.is_consumption_possible()) { | ||
console.log("YES is_consumption_possible"); | ||
cb_get_memory_chunk(internal_audio_buffer_obj); // retrieve buffer data from circular queue | ||
console.log("AAAAA size_available_to_consume ", internal_audio_buffer_obj.size_available_to_consume); | ||
console.log("AAAAA count_total_size_consumed ", internal_audio_buffer_obj.count_total_size_consumed); | ||
} else { | ||
console.log("playback is complete ... stopping now"); | ||
stop_audio(given_node); | ||
}; | ||
// --- | ||
if (flag_streaming_status === streaming_status_done) { | ||
console.log("request_number ", request_number); | ||
we_retrieved_last_chunk_from_server = true; // stop when we reach above on next iteration of this cb | ||
} else if (client_memory_mgr.is_production_possible()) { | ||
// OK circular queue consumed all of previous dollup so go ahead and get another buffer chunk from server | ||
console.log("OK circular queue is NOT full so get another chunk"); | ||
client_socket_comms.socket_client(msgs_to_server.mode_stream_audio_to_client); | ||
} else { | ||
console.log("... production is NOT possible so just go with the flow"); | ||
}; | ||
// shared_utils.show_object(internal_audio_buffer_obj, | ||
// "internal_audio_buffer_obj", "total", 10); | ||
}; | ||
}()); | ||
}; // setup_onaudioprocess_callback_stream | ||
// function request_server_send_another_chunk() { | ||
// if (client_memory_mgr.is_production_possible()) { | ||
// // OK circular queue consumed all of previous dollup so go ahead and get another buffer chunk from server | ||
// console.log("OK circular queue is NOT full so get another chunk"); | ||
// console.log("OK circular queue is NOT full so get another chunk"); | ||
// console.log("OK circular queue is NOT full so get another chunk"); | ||
// client_socket_comms.socket_client(msgs_to_server.mode_stream_audio_to_client); | ||
// }; | ||
// }; | ||
// function setup_setInterval() { | ||
// timer_Id = setInterval(request_server_send_another_chunk, 100); | ||
// }; | ||
// --------------------------------------------------------------------------- // | ||
function cb_receive_buffer_from_server_to_web_audio_player(audio_obj_from_server) { | ||
if (terminate_current_run) { return;}; | ||
client_memory_mgr.pop_stream_buffer(audio_obj_from_server); // save data from server into circular queue | ||
console.log("size_available_to_produce ", audio_obj_from_server.size_available_to_produce, " out of ", | ||
audio_obj_from_server.buffer.length); | ||
console.log("AAAAAAA count_total_size_buffered ", audio_obj_from_server.count_total_size_buffered); | ||
console.log("audio_obj_from_server buffer size ", audio_obj_from_server.buffer.length, | ||
" transaction_size ", audio_obj_from_server.transaction_size); | ||
if ((!flag_audio_rendering) && client_memory_mgr.did_buffer_get_filled()) { | ||
console.log("BUFF_SIZE_AUDIO_RENDERER ", BUFF_SIZE_AUDIO_RENDERER); | ||
console.log("BUFF_SIZE_AUDIO_RENDERER ", BUFF_SIZE_AUDIO_RENDERER); | ||
console.log("BUFF_SIZE_AUDIO_RENDERER ", BUFF_SIZE_AUDIO_RENDERER); | ||
console.log("BUFF_SIZE_AUDIO_RENDERER ", BUFF_SIZE_AUDIO_RENDERER); | ||
console.log("BUFF_SIZE_AUDIO_RENDERER ", BUFF_SIZE_AUDIO_RENDERER); | ||
streaming_node = audio_context.createScriptProcessor(BUFF_SIZE_AUDIO_RENDERER, 1, 1); | ||
// setup_onaudioprocess_callback_stream(streaming_node, streaming_audio_obj.buffer, | ||
// streaming_audio_obj.buffer.length, set_false_in_middle_of_playback); | ||
setup_onaudioprocess_callback_stream(streaming_node, client_memory_mgr, client_memory_mgr.get_memory_chunk, set_false_in_middle_of_playback); | ||
streaming_node.connect(gain_node); | ||
flag_streaming_status = streaming_status_active; | ||
flag_audio_rendering = true; | ||
}; | ||
if (audio_obj_from_server.transaction_size === audio_obj_from_server.buffer.length && | ||
client_memory_mgr.is_production_possible()) { | ||
// OK circular queue consumed all of previous dollup so go ahead and get another buffer chunk from server | ||
console.log("OK circular queue is NOT full so get another chunk"); | ||
console.log("OK circular queue is NOT full so get another chunk"); | ||
console.log("OK circular queue is NOT full so get another chunk"); | ||
client_socket_comms.socket_client(msgs_to_server.mode_stream_audio_to_client); | ||
} else { | ||
console.log("NOTICE - currently production is NOT possible"); | ||
}; | ||
}; // cb_receive_buffer_from_server_to_web_audio_player | ||
// ---------------------------------------- // | ||
function entry_point(given_request) { | ||
switch (given_request) { | ||
case "init_audio_context" : { | ||
init_audio_context(); | ||
break; | ||
} | ||
case "stream_audio" : { | ||
console.log("stream audio from server to client browser"); | ||
if (flag_streaming_status !== streaming_status_ready) { | ||
console.log("flag_streaming_status ", flag_streaming_status); | ||
console.error("NOTICE - currently is middle of previous streaming request ... try later"); | ||
return; | ||
} | ||
// --- | ||
@@ -366,10 +105,10 @@ | ||
streaming_audio_obj = { | ||
// streaming_audio_obj = { | ||
index_stream : 0 | ||
}; | ||
// index_stream : 0 | ||
// }; | ||
curr_index_synth_buffer = 0; | ||
// curr_index_synth_buffer = 0; | ||
flag_audio_rendering = false; | ||
// flag_audio_rendering = false; | ||
@@ -382,7 +121,11 @@ var media_file; | ||
// media_file = "sine_wave_262144_64.wav"; | ||
// media_file = "sine_wave_8388608_64.wav"; | ||
// media_file = "Lee_Smolin_Physics_Envy_and_Economic_Theory-cWn86ESze6M_mono_1st_few_seconds.wav"; | ||
media_file = "sine_wave_8388608_64.wav"; | ||
// media_file = "Justice_Genesis_first_third_sec_tight.wav"; | ||
media_file = "Justice_Genesis_mono_trim_16bit_y6iHYTjEyKU.wav"; | ||
var callback = cb_receive_buffer_from_server_to_web_audio_player; | ||
// media_file = "Justice_Genesis_mono_trim_16bit_y6iHYTjEyKU.wav"; | ||
var callback = curr_web_audio_obj.cb_receive_buffer_from_server_to_web_audio_player; | ||
// --- | ||
@@ -394,2 +137,3 @@ | ||
stop_streaming_msg.requested_action = "stop_streaming"; | ||
stop_streaming_msg.request_number = request_number; | ||
@@ -408,2 +152,4 @@ msgs_to_server.mode_stop_streaming = stop_streaming_msg; | ||
stream_audio_msg.request_number = request_number; | ||
stream_audio_msg.transmit_chunksize = 16384; | ||
@@ -439,5 +185,7 @@ // stream_audio_msg.transmit_chunksize = 65536; | ||
client_memory_mgr.allocate_streaming_buffer(size_memory_buffer); | ||
// client_memory_mgr.allocate_streaming_buffer(size_memory_buffer); | ||
curr_web_audio_obj.allocate_streaming_buffer(size_memory_buffer); | ||
console.log("get_size_memory_buffer ", client_memory_mgr.get_size_memory_buffer()); | ||
// console.log("get_size_memory_buffer ", client_memory_mgr.get_size_memory_buffer()); | ||
console.log("get_size_memory_buffer ", curr_web_audio_obj.get_size_memory_buffer()); | ||
@@ -447,5 +195,7 @@ | ||
curr_web_audio_obj.set_msgs_to_server(msgs_to_server); | ||
// --- | ||
client_socket_comms.set_stream_is_complete_cb(cb_stream_is_complete); | ||
client_socket_comms.set_stream_is_complete_cb(curr_web_audio_obj.cb_stream_is_complete); | ||
@@ -463,4 +213,10 @@ console.log("stream_audio_msg ", msgs_to_server.mode_stream_audio_to_client); | ||
stop_audio(streaming_node); | ||
curr_circular_queue_memory_buffer_obj.deallocate_queue(); | ||
curr_circular_queue_memory_buffer_obj = null; | ||
curr_web_audio_obj.stop_audio(); | ||
// stop_audio(streaming_node); | ||
curr_web_audio_obj = null; // may need to help this more explicitly | ||
break; | ||
@@ -476,6 +232,4 @@ } | ||
} | ||
}; | ||
// ----------------------------------------------------------------------- // | ||
@@ -488,3 +242,2 @@ | ||
// render_buffer: render_buffer, | ||
// cb_stream_is_complete : cb_stream_is_complete | ||
}; | ||
@@ -491,0 +244,0 @@ |
@@ -58,2 +58,8 @@ var server_streaming_audio = function() { | ||
var request_number = 0; | ||
var previous_request_number = 0; | ||
var request_new = "request_new"; | ||
var request_ongoing = "request_ongoing"; | ||
// --- | ||
@@ -70,2 +76,20 @@ | ||
var init_fresh_request = function() { | ||
// streaming_buffer_obj.curr_state = stream_status_complete; | ||
streaming_buffer_obj.curr_state = stream_status_prior; // prepare for next request to stream | ||
// --- | ||
temp_stream_chunk_buffer = null; | ||
streaming_buffer_obj = null; | ||
streaming_buffer_obj = { | ||
curr_state : stream_status_prior, | ||
index_stream : 0 | ||
}; | ||
}; | ||
var streaming_is_done = function(given_max_index, curr_ws) { | ||
@@ -77,4 +101,2 @@ | ||
streaming_buffer_obj.curr_state = stream_status_complete; | ||
var streaming_is_done_msg = { | ||
@@ -86,3 +108,2 @@ | ||
// bbb | ||
console.log("SEND -------- json DONE --------"); | ||
@@ -96,9 +117,5 @@ console.log("SEND ---------- streaming_is_done_msg ", streaming_is_done_msg); | ||
// streaming_buffer_obj = null; | ||
init_fresh_request(); | ||
// streaming_buffer_obj = { | ||
// curr_state : stream_status_prior, | ||
// index_stream : 0 | ||
// }; | ||
previous_request_number = request_number; | ||
} | ||
@@ -109,4 +126,7 @@ | ||
// var curr_index = streaming_buffer_obj.index_stream; | ||
// var max_index = streaming_buffer_obj.max_index; | ||
var curr_index = streaming_buffer_obj.index_stream; | ||
var max_index = streaming_buffer_obj.max_index; | ||
var max_index = streaming_buffer_obj.max_index; | ||
@@ -293,2 +313,27 @@ console.log("PREEEEE ", curr_index + " out of " + max_index); | ||
// --- | ||
var putative_request_number = received_json.request_number; // request_number | ||
console.log(" request_number ", request_number); | ||
console.log(" received_json.request_number ", received_json.request_number); | ||
if (received_json.request_number === request_number) { | ||
console.log("OK seeing same request_number ", request_number); | ||
request_status = request_ongoing; | ||
} else { | ||
console.log("OK seeing new request_number ", putative_request_number); | ||
request_number = putative_request_number; | ||
request_status = request_new; | ||
console.log("new request_number ", request_number); | ||
} | ||
// --- | ||
console.log("requested_action ", requested_action); | ||
@@ -300,7 +345,26 @@ | ||
console.log("RECEIVED ---------- stream_audio_to_client"); | ||
console.log("RECEIVED ---------- stream_audio_to_client"); | ||
console.log("RECEIVED ---------- stream_audio_to_client"); | ||
console.log("RECEIVED ---------- stream_audio_to_client request_status ", request_status); | ||
console.log("RECEIVED ---------- stream_audio_to_client request_status ", request_status); | ||
console.log("RECEIVED ---------- stream_audio_to_client request_status ", request_status); | ||
if (request_status === request_ongoing) { | ||
if (request_number === previous_request_number) { | ||
console.log("cool we have already stopped this stream request so ignoring stream request"); | ||
break; | ||
}; | ||
} else { | ||
console.log("request_status === request_new ... so call init_fresh_request"); | ||
console.log("request_status === request_new ... so call init_fresh_request"); | ||
console.log("request_status === request_new ... so call init_fresh_request"); | ||
init_fresh_request(); | ||
}; | ||
read_file_pop_buffer_stream_back_to_client(received_json, curr_ws); | ||
break; | ||
@@ -311,7 +375,16 @@ }; | ||
console.log("RECEIVED ---------- stop_streaming"); | ||
console.log("RECEIVED ---------- stop_streaming"); | ||
console.log("RECEIVED ---------- stop_streaming"); | ||
if (request_status === request_ongoing && | ||
streaming_buffer_obj.curr_state === stream_status_prior) { | ||
stop_streaming(received_json, curr_ws); | ||
console.log("cool we have already stopped this stream request so ignoring stop request"); | ||
} else { | ||
console.log("RECEIVED ---------- stop_streaming"); | ||
console.log("RECEIVED ---------- stop_streaming"); | ||
console.log("RECEIVED ---------- stop_streaming"); | ||
stop_streaming(received_json, curr_ws); | ||
} | ||
break; | ||
@@ -318,0 +391,0 @@ }; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90161918
50
6013
58
16