audio-utils
Advanced tools
Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "audio-utils", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Audio utilities like fundamental frequency detection, populate buffer with sinusoidal curves", | ||
@@ -5,0 +5,0 @@ "main": "src/audio_utils.js", |
@@ -41,11 +41,215 @@ | ||
// --- | ||
var play_detect_frequency = function(audio_obj, spec) { | ||
// --- iterate across to identify dominate frequency --- // | ||
var default_min_error_thresold = 0.1; | ||
var default_minimum_size_subsection = 6; | ||
var defafult_max_samples_per_subsection = 30; | ||
var default_subsection_mode = "decimation"; // keep dividing buffer size by incrementing counter | ||
var default_subsection_mode = "continuous"; // continuously slide left/right center point closer to start | ||
var spec = spec || { | ||
min_error_thresold : default_min_error_thresold, | ||
minimum_size_subsection : default_minimum_size_subsection, | ||
max_samples_per_subsection : defafult_max_samples_per_subsection, | ||
subsection_mode : default_subsection_mode | ||
}; | ||
var min_error_thresold = spec.min_error_thresold || default_min_error_thresold; | ||
var minimum_size_subsection = spec.minimum_size_subsection || default_minimum_size_subsection; | ||
var max_samples_per_subsection = spec.max_samples_per_subsection || defafult_max_samples_per_subsection; | ||
console.log("min_error_thresold ", min_error_thresold); | ||
spec.all_low_error_sample_sizes = []; | ||
var SIZE_BUFFER_SOURCE = audio_obj.buffer.length; | ||
spec.SIZE_BUFFER_SOURCE = SIZE_BUFFER_SOURCE; | ||
console.log("play_detect_frequency SIZE_BUFFER_SOURCE ", SIZE_BUFFER_SOURCE); | ||
console.log("play_detect_frequency SIZE_BUFFER_SOURCE ", SIZE_BUFFER_SOURCE); | ||
console.log("play_detect_frequency SIZE_BUFFER_SOURCE ", SIZE_BUFFER_SOURCE); | ||
// shared_utils.show_object(audio_obj, | ||
// "DFF audio_obj DFF", "total", 10); | ||
var curr_interval = 2; // take entire input buffer and divide by this looking for similarities between such subsections | ||
var size_subsection; | ||
var count_subsection = 0; | ||
var size_chunks; | ||
var curr_chunk; | ||
var curr_start; | ||
var curr_end; | ||
var curr_sample_left; | ||
var curr_sample_right; | ||
// var curr_sample_space; | ||
var curr_left; | ||
// var min_left; | ||
// var max_left; | ||
var curr_right; | ||
// var min_right; | ||
// var max_right; | ||
var prev_size_subsection = 0; | ||
// var max_samples_per_subsection = 30; | ||
var size_increment; | ||
var reconstituted_size_subsection; | ||
// var max_size_subsample_to_do_increment_fixup = 30; | ||
var max_size_subsample_to_do_increment_fixup = max_samples_per_subsection; | ||
// --- | ||
var aggregate_total; | ||
var aggregate_diff; | ||
var subsection_total; | ||
var subsection_diff; | ||
var count_num_iterations; | ||
do { | ||
size_subsection = ~~(SIZE_BUFFER_SOURCE / curr_interval); | ||
if (size_subsection == prev_size_subsection) { | ||
curr_interval++; | ||
continue; | ||
} | ||
if (size_subsection < max_size_subsample_to_do_increment_fixup) { | ||
size_increment = 1; | ||
reconstituted_size_subsection = size_subsection; | ||
} else { | ||
size_increment = ~~(size_subsection / max_samples_per_subsection); | ||
reconstituted_size_subsection = size_increment * max_samples_per_subsection; | ||
}; | ||
// stens TODO - we may want to compare more than ONE pair ... make it a parm to compare X cycles | ||
// console.log("size_subsection ", size_subsection, " curr_interval ", curr_interval, | ||
// " size_increment ", size_increment, reconstituted_size_subsection); | ||
console.log("subsection size ", size_subsection, " recon ", reconstituted_size_subsection, | ||
" step size ", size_increment); | ||
// min_left = 0; | ||
// max_left = size_subsection; | ||
// min_right = size_subsection; | ||
// max_right = size_subsection * 2; | ||
// console.log("min_left ", min_left, " max_left ", max_left, " min_right ", min_right, " max_right ", max_right); | ||
subsection_total = 0; | ||
subsection_diff = 0; | ||
count_num_iterations = 0; | ||
for (curr_left = 0; curr_left < reconstituted_size_subsection; curr_left += size_increment) { | ||
curr_right = curr_left + size_subsection; | ||
curr_sample_left = audio_obj.buffer[curr_left]; | ||
curr_sample_right = audio_obj.buffer[curr_right]; | ||
subsection_total += curr_sample_right; | ||
subsection_diff += Math.abs(curr_sample_left - curr_sample_right); | ||
count_num_iterations++; | ||
// console.log("User %s has %d points", userName, userPoints); | ||
// console.log(reconstituted_size_subsection, curr_left, curr_sample_left, curr_right, curr_sample_right); | ||
// console.log("aaa %d %d %f %d %f", reconstituted_size_subsection, curr_left, curr_sample_left, curr_right, curr_sample_right); | ||
// process.stdout.write('aaa %d %d %f %d %f\n', reconstituted_size_subsection, curr_left, curr_sample_left, curr_right, curr_sample_right); | ||
console.log("" + shared_utils.toFixed(reconstituted_size_subsection, 5), | ||
// curr_left, curr_sample_left.toFixed(5), | ||
// curr_right, curr_sample_right.toFixed(5), " vs mine ", | ||
shared_utils.toFixed(curr_left, 5), shared_utils.toFixed(curr_sample_left, 5), | ||
shared_utils.toFixed(curr_right, 5), shared_utils.toFixed(curr_sample_right, 5) | ||
); | ||
}; | ||
// console.log("" + size_subsection, samples_per_cycle, count_num_iterations, | ||
// " subsection_diff ", subsection_diff/count_num_iterations); | ||
aggregate_diff = subsection_diff/count_num_iterations; | ||
if (aggregate_diff < min_error_thresold) { | ||
var this_goodie = {}; | ||
this_goodie.aggregate_diff = aggregate_diff; | ||
this_goodie.size_subsection = size_subsection; | ||
spec.all_low_error_sample_sizes.push(this_goodie); | ||
} | ||
console.log("" + shared_utils.toFixed(size_subsection, 5), | ||
// shared_utils.toFixed(samples_per_cycle, 5), | ||
shared_utils.toFixed(count_num_iterations, 5), | ||
" subsection_diff ", | ||
// shared_utils.toFixed(subsection_diff/count_num_iterations, 5) | ||
shared_utils.toFixed(aggregate_diff, 5) | ||
); | ||
// --- | ||
prev_size_subsection = size_subsection; | ||
curr_interval++; | ||
} while (size_subsection > minimum_size_subsection); | ||
}; // detect_fundamental_frequency | ||
that.play_detect_frequency = play_detect_frequency; | ||
// --- | ||
// var detect_fundamental_frequency = function(audio_obj, SIZE_BUFFER_SOURCE, samples_per_cycle) { | ||
var detect_fundamental_frequency = function(audio_obj) { | ||
var detect_fundamental_frequency = function(audio_obj, spec) { | ||
// --- iterate across to identify dominate frequency --- // | ||
var default_min_error_thresold = 0.1; | ||
var spec = spec || { | ||
min_error_thresold : default_min_error_thresold | ||
}; | ||
var min_error_thresold = spec.min_error_thresold || default_min_error_thresold; | ||
console.log("min_error_thresold ", min_error_thresold); | ||
spec.all_low_error_sample_sizes = []; | ||
var SIZE_BUFFER_SOURCE = audio_obj.buffer.length; | ||
spec.SIZE_BUFFER_SOURCE = SIZE_BUFFER_SOURCE; | ||
console.log("detect_fundamental_frequency SIZE_BUFFER_SOURCE ", SIZE_BUFFER_SOURCE); | ||
@@ -174,3 +378,14 @@ console.log("detect_fundamental_frequency SIZE_BUFFER_SOURCE ", SIZE_BUFFER_SOURCE); | ||
aggregate_diff = subsection_diff/count_num_iterations; | ||
if (aggregate_diff < min_error_thresold) { | ||
var this_goodie = {}; | ||
this_goodie.aggregate_diff = aggregate_diff; | ||
this_goodie.size_subsection = size_subsection; | ||
spec.all_low_error_sample_sizes.push(this_goodie); | ||
} | ||
console.log("" + shared_utils.toFixed(size_subsection, 5), | ||
@@ -180,7 +395,6 @@ // shared_utils.toFixed(samples_per_cycle, 5), | ||
" subsection_diff ", | ||
shared_utils.toFixed(subsection_diff/count_num_iterations, 5) | ||
// shared_utils.toFixed(subsection_diff/count_num_iterations, 5) | ||
shared_utils.toFixed(aggregate_diff, 5) | ||
); | ||
// --- | ||
@@ -197,6 +411,4 @@ | ||
// ------------------------------------------------------------------------ // | ||
var pop_audio_buffer = function (size_buff, given_samples_per_cycle) { | ||
@@ -216,5 +428,11 @@ | ||
var samples_per_cycle = (typeof given_samples_per_cycle != "undefined") ? | ||
given_samples_per_cycle : 64; | ||
// var samples_per_cycle = (typeof given_samples_per_cycle != "undefined") ? | ||
// given_samples_per_cycle : 64; | ||
var samples_per_cycle = given_samples_per_cycle || 64; | ||
console.log("samples_per_cycle ", samples_per_cycle); | ||
console.log("given_samples_per_cycle ", given_samples_per_cycle); | ||
if (samples_per_cycle > size_buff) { | ||
@@ -221,0 +439,0 @@ |
@@ -28,3 +28,2 @@ #!/usr/bin/env node | ||
var shared_utils; | ||
@@ -77,70 +76,64 @@ | ||
// ---------- generates sin tone ------------- // | ||
// ---------- generates nice listenable sin tone ------------- // | ||
var output_dir = resolvePath("~/Dropbox/Documents/data/audio/"); | ||
SIZE_BUFFER_SOURCE = 256; | ||
// SIZE_BUFFER_SOURCE = 16384; | ||
var output_format = ".wav"; | ||
var samples_per_cycle = 64; | ||
console.log(" output_dir ", output_dir); | ||
// ---------------- | ||
var SIZE_BUFFER_SOURCE = 256; | ||
// SIZE_BUFFER_SOURCE = 16384; | ||
var output_dir = resolvePath("~/Dropbox/Documents/data/audio/"); | ||
var samples_per_cycle = 64; | ||
var output_format = ".wav"; | ||
var source_obj = {}; | ||
console.log(" output_dir ", output_dir); | ||
var source_obj = audio_utils.pop_audio_buffer(SIZE_BUFFER_SOURCE, samples_per_cycle); | ||
// ---------------- | ||
var max_index = 3; | ||
// var max_index = SIZE_BUFFER_SOURCE; | ||
var source_obj = {}; | ||
for (var index = 0; index < max_index; index++) { | ||
var source_obj = audio_utils.pop_audio_buffer(SIZE_BUFFER_SOURCE, samples_per_cycle); | ||
console.log(index, " pop_audio_buffer ", source_obj.buffer[index]); | ||
} | ||
var max_index = 3; | ||
// var max_index = SIZE_BUFFER_SOURCE; | ||
// ---------- write to output file ------------- // | ||
for (var index = 0; index < max_index; index++) { | ||
var source_wave = "audio_util_test_file"; | ||
console.log(index, " pop_audio_buffer ", source_obj.buffer[index]); | ||
} | ||
var source_wave_filename = path.join(output_dir, source_wave + output_format); | ||
// ---------- write to output file ------------- // | ||
console.log("source_wave_filename ", source_wave_filename); | ||
var source_wave = "audio_util_test_file"; | ||
shared_utils.write_32_bit_float_buffer_to_16_bit_wav_file(source_obj, source_wave_filename); | ||
var source_wave_filename = path.join(output_dir, source_wave + output_format); | ||
console.log("source_wave_filename ", source_wave_filename); | ||
console.log("source_wave_filename ", source_wave_filename); | ||
// ------------ read wav file -------------------- // | ||
shared_utils.write_32_bit_float_buffer_to_16_bit_wav_file(source_obj, source_wave_filename); | ||
console.log("\n\nread wav file\n\n"); | ||
console.log("source_wave_filename ", source_wave_filename); | ||
var wav_file_input_obj = {}; // create stub object to which we attach .buffer | ||
// ------------ read wav file -------------------- // | ||
var property_buffer_raw_input_file = "buffer_raw_input_file"; | ||
var property_buffer_input_file = "buffer_input_file"; | ||
console.log("\n\nread wav file\n\n"); | ||
wav_file_input_obj.filename = source_wave_filename; | ||
var wav_file_input_obj = {}; // create stub object to which we attach .buffer | ||
wav_file_input_obj[property_buffer_raw_input_file] = new Buffer(0); | ||
console.log("abouttttt to read wav_file_input_obj.filename ", wav_file_input_obj.filename); | ||
var property_buffer_raw_input_file = "buffer_raw_input_file"; | ||
var property_buffer_input_file = "buffer_input_file"; | ||
var spec = {}; | ||
wav_file_input_obj.filename = source_wave_filename; | ||
shared_utils.read_16_bit_wav_file_into_32_bit_float_buffer( | ||
wav_file_input_obj, | ||
wav_file_input_obj.filename, | ||
spec, | ||
cb_read_file_done); | ||
wav_file_input_obj[property_buffer_raw_input_file] = new Buffer(0); | ||
console.log("abouttttt to read wav_file_input_obj.filename ", wav_file_input_obj.filename); | ||
var spec = {}; | ||
shared_utils.read_16_bit_wav_file_into_32_bit_float_buffer( | ||
wav_file_input_obj, | ||
wav_file_input_obj.filename, | ||
spec, | ||
cb_read_file_done); | ||
}; // synth_curve_write_to_wav_file_then_read_back | ||
@@ -147,0 +140,0 @@ |
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
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
33170
8
505