Elna Haim
Elna Haim

Reputation: 1233

creating audio file based on frequencies

I'm using node.js for a project im doing. The project is to convert words into numbers and then to take those numbers and create an audio output. The audio output should play the numbers as frequencies. for example, I have an array of numbers [913, 250,352] now I want to play those numbers as frequencies. I know I can play them in the browser with audio API or any other third package that allows me to do so. The thing is that I want to create some audio file, I tried to convert those numbers into notes and then save it as Midi file, I succeed but the problem is that the midi file takes the frequencies, convert them into the closest note (example: 913 will convert into 932.33HZ - which is note number 81),

    // add a track
    var array = gematriaArray
    var count = 0
    var track = midi.addTrack()
    var note
    
    for (var i = 0; i < array.length; i++) {
            note = array[i]

        track = track.addNote({

            //here im converting the freq -> midi note.
            midi: ftom(parseInt(note)),
            time: count,
            duration: 3
        })
        count++
    }

    // write the output
    fs.writeFileSync('./public/sounds/' + name + random + '.mid', new Buffer.from(midi.toArray()))

I searched the internet but I couldn't find anything that can help. I really want to have a file that the user can download with those numbers as frequencies, someone knows what can be done to get this result?

Thanks in advance for the helpers.

Upvotes: 1

Views: 564

Answers (1)

Scott Stensland
Scott Stensland

Reputation: 28285

this function will populate a buffer with floating point values which represent the height of the raw audio curve for the given frequency

var pop_audio_buffer_custom = function (number_of_samples, given_freq, samples_per_second) {

    var number_of_samples = Math.round(number_of_samples);

    var audio_obj = {};
    
    var source_buffer = new Float32Array(number_of_samples);

    audio_obj.buffer = source_buffer;

    var incr_theta = (2.0 * Math.PI * given_freq) / samples_per_second;
    var theta = 0.0;

    for (var curr_sample = 0; curr_sample < number_of_samples; curr_sample++) {

        audio_obj.buffer[curr_sample] = Math.sin(theta);

        console.log(audio_obj.buffer[curr_sample] , "theta ", theta);

        theta += incr_theta;
    }
    
    return audio_obj;

};       //      pop_audio_buffer_custom

var number_of_samples = 10000; // long enough to be audible
var given_freq = 300;
var samples_per_second = 44100;  // CD quality sample rate
var wav_output_filename = "/tmp/wav_output_filename.wav"

var synthesized_obj = {};

synthesized_obj.buffer = pop_audio_buffer_custom(number_of_samples, given_freq, samples_per_second);

the world of digital audio is non trivial ... the next step once you have an audio buffer is to translate the floating point representation into something which can be stored in bytes ( typically 16 bit integers dependent on your choice of bit depth ) ... then that 16 bit integer buffer needs to get written out as a WAV file

audio is a wave sometimes called a time series ... when you pound your fist onto the table the table wobbles up and down which pushes tiny air molecules in unison with that wobble ... this wobbling of air propagates across the room and reaches a microphone diaphragm or maybe your eardrum which in turn wobbles in resonance with this wave ... if you glued a pencil onto the diaphragm so it wobbled along with the diaphragm and you slowly slid a strip of paper along the lead tip of the pencil you would see a curve being written onto that paper strip ... this is the audio curve ... an audio sample is just the height of that curve at an instant of time ... if you repeatedly wrote down this curve height value X times per second at a constant rate you will have a list of data points of raw audio ( this is what above function creates ) ... so a given audio sample is simply the value of the audio curve height at a given instant in time ... since computers are not continuous instead are discrete they cannot handle the entire pencil drawn curve so only care about this list of instantaneously measured curve height values ... those are audio samples

above 32 bit floating point buffer can be fed into following function to return a 16 bit integer buffer

var convert_32_bit_float_into_signed_16_bit_int_lossy = function(input_32_bit_buffer) {

    // this method is LOSSY - intended as preliminary step when saving audio into WAV format files
    //                        output is a byte array where the 16 bit output format 
    //                        is spread across two bytes in little endian ordering

    var size_source_buffer = input_32_bit_buffer.length;

    var buffer_byte_array = new Int16Array(size_source_buffer * 2); // Int8Array 8-bit twos complement signed integer

    var value_16_bit_signed_int;
    var index_byte = 0;

    console.log("size_source_buffer", size_source_buffer);


    for (var index = 0; index < size_source_buffer; index++) {

        value_16_bit_signed_int = ~~((0 < input_32_bit_buffer[index]) ? input_32_bit_buffer[index] * 0x7FFF : 
                                                                        input_32_bit_buffer[index] * 0x8000);

        buffer_byte_array[index_byte] = value_16_bit_signed_int & 0xFF; // bitwise AND operation to pluck out only the least significant byte

        var byte_two_of_two = (value_16_bit_signed_int >> 8); //  bit shift down to access the most significant byte

        buffer_byte_array[index_byte + 1] = byte_two_of_two;

        index_byte += 2;
    };

    // ---

    return buffer_byte_array;
};

next step is to persist above 16 bit int buffer into a wav file ... I suggest you use one of the many nodejs libraries for that ( or even better write your own as its only two pages of code ;-)))

Upvotes: 1

Related Questions