dm20 / Music-Synthesis

A piano simulator.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Note generation

machineelearner opened this issue · comments

Hey, how did you generate notes? I get that a tone is a sine wave, but how exactly did you do this and why is sampling rate important?

Also, any interest in applying this toward artificially intelligent music generation?

also, what are your thoughts on doing this in Python? I don't know Matlab or have a license, but it looks very similar to Python

Since I've received this question before, here is my own implementation. Hope this helps! calling soundsc on a note you make at key 44 for 1 second, as I've done below, should show you the idea!

You'll want to define this function separately and call it from some other file like

pretty_tone = note(44,1)
soundsc(pretty_tone)

function tone = note(keynum,dur)
    sampling_rate = 8192;             % sampling rate (see note at bottom)
    t = 0 : 1/sampling_rate : dur;   % time vector for note duration 
    
    if keynum == -1                        % special key for a pause
        tone = zeros(size(t));           
        return;                                   
    end
    
    % now here at this line you need to choose some equation relating key number to frequency
    % this is totally up to you, so if you'd like to see what I did, try f = 440*(2^((keynum - 49)/12));
    f = some _expression % <--- UP TO YOU TO DEFINE!!
    tone = sin(2*pi*t*f);        % the audio tone, which may be passed to soundsc()
    % tone = Envelope(tone);       % wrap the raw tone in the ADSR function (optional)
end

The sampling rate will take 8000 steps in reaching a one second duration, meaning that the resolution of the sampled data is pretty high. The spectrum of audible frequencies that the human ear can pick up goes to around 8 kHz by the way, so the sounds we are concerned with are only going to change that fast and need to be sampled at that small of a timestep.

As for translating to Python, I've totally considered it. I was thinking of posting something to that effect soon.

Thanks so much! The notes are beautiful, especially the scale. Did you make the songs?

If you don't mind me asking, what is "trap"?

Post the python implementation if you do it please!

Trap is a musical genre born of hip hop and EDM.

I'll be sure to post! Likewise.