creating audio file based on frequencies

Elnatan vazana

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.

Scott Stensland

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 ;-)))

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Creating a directory name based on a file name

分類Dev

Pandas- How to save frequencies of different values in different columns line by line in a csv file (including 0 frequencies)

分類Dev

SpeechKit transcribing audio file

分類Dev

Creating a new file inside awk but in different directory based on input field value

分類Dev

scala : creating directory and file

分類Dev

xauth not creating .Xauthority file

分類Dev

Creating new *.reg file

分類Dev

Creating a file in php

分類Dev

Is there a shortcut for creating a new file?

分類Dev

Creating projects based on jupyter notebooks

分類Dev

Play local audio file with AVAudioPlayer

分類Dev

Convert an audio file into an array of samples

分類Dev

Loop audio file to a given length

分類Dev

How to download audio file in Android

分類Dev

How can I make an array of frequencies accurately depict a decoded mp3 file?

分類Dev

Javascript - When creating new audio objects, when are they downloaded?

分類Dev

Combine Multiple Audio Files into a single higher-quality audio File

分類Dev

Creating pdf file in Rebol/Red

分類Dev

Programmatically Creating a File Directory with Python?

分類Dev

Creating a log file for a VSCode extension

分類Dev

Creating Navigation from text file

分類Dev

Creating a BAT file to execute query

分類Dev

Creating dictionary from XML file

分類Dev

Creating a .desktop file for a new application

分類Dev

Creating Multiple host file in Ansible

分類Dev

Creating stream based on asynchronously loaded DOM in RxJS

分類Dev

Creating a new column based on the values of other columns

分類Dev

Creating separate columns based on string value in python

分類Dev

Creating groups based on running totals against a value

Related 関連記事

  1. 1

    Creating a directory name based on a file name

  2. 2

    Pandas- How to save frequencies of different values in different columns line by line in a csv file (including 0 frequencies)

  3. 3

    SpeechKit transcribing audio file

  4. 4

    Creating a new file inside awk but in different directory based on input field value

  5. 5

    scala : creating directory and file

  6. 6

    xauth not creating .Xauthority file

  7. 7

    Creating new *.reg file

  8. 8

    Creating a file in php

  9. 9

    Is there a shortcut for creating a new file?

  10. 10

    Creating projects based on jupyter notebooks

  11. 11

    Play local audio file with AVAudioPlayer

  12. 12

    Convert an audio file into an array of samples

  13. 13

    Loop audio file to a given length

  14. 14

    How to download audio file in Android

  15. 15

    How can I make an array of frequencies accurately depict a decoded mp3 file?

  16. 16

    Javascript - When creating new audio objects, when are they downloaded?

  17. 17

    Combine Multiple Audio Files into a single higher-quality audio File

  18. 18

    Creating pdf file in Rebol/Red

  19. 19

    Programmatically Creating a File Directory with Python?

  20. 20

    Creating a log file for a VSCode extension

  21. 21

    Creating Navigation from text file

  22. 22

    Creating a BAT file to execute query

  23. 23

    Creating dictionary from XML file

  24. 24

    Creating a .desktop file for a new application

  25. 25

    Creating Multiple host file in Ansible

  26. 26

    Creating stream based on asynchronously loaded DOM in RxJS

  27. 27

    Creating a new column based on the values of other columns

  28. 28

    Creating separate columns based on string value in python

  29. 29

    Creating groups based on running totals against a value

ホットタグ

アーカイブ