Writing musical notes to a wav file

后端 未结 3 873
失恋的感觉
失恋的感觉 2020-12-02 05:13

I am interested in how to take musical notes (e.g A, B, C#, etc) or chords (multiple notes at the same time) and write them to a wav file.

From what I understand, ea

3条回答
  •  失恋的感觉
    2020-12-02 05:52

    Nobody has yet mentioned the Karplus Strong plucked string algorithm.

    Karplus–Strong string synthesis It's an extremely simple method for generating a realistic plucked string sound. I have written polyphonic musical instruments / realtime MIDI players using this.

    You do it like this:

    First, what frequency do you want to simulate? Let's say concert pitch A = 440Hz

    Supposing your sample-rate is 44.1kHz, that is 44100 / 440 = 100.25 samples per wavelength.

    Let's round that to the nearest integer: 100, and create a circular buffer length 100.

    So it will hold one standing wave of frequency ~440Hz (note it is not exact, there are ways around this).

    Fill it with random static between -1 and +1, and:

    DECAY = 0.99
    while( n < 99999 )
        outbuf[n++] = buf[k]
    
        newVal = DECAY  *  ( buf[k] + buf_prev ) / 2
    
        buf_prev = buf[k]
        buf[k] = newVal
    
        k = (k+1) % 100
    

    It is an amazing algorithm because it is so simple and generates a super sound.

    The best way to understand what's going on is to realise that random static in the time domain is white noise; random static in the frequency domain. You can imagine it as the composite of many waves of different (random) frequency.

    Frequencies close to 440Hz (or 2*440Hz, 3*440Hz etc) will create constructive interference with themselves, as they pass around the ring again and again. So they will be preserved. Other frequencies will destructively interfere with themselves.

    Additionally, the averaging acts as a lowpass filter -- imagine your sequence is +1 -1 +1 -1 +1 -1, if you're averaging pairs then each average comes out as 0. but if you have slower wave like 0 0.2 0.3 0.33 0.3 0.2 ... then averaging still results in a wave. The longer the wave the more its energy is preserved -- i.e. the averaging causes less damping.

    So averaging can be thought of as very simple lowpass filter.

    There are complications of course, having to choose an integer buffer length forces a quantisation of possible frequencies, which becomes noticeable towards the top of the piano. Everything is surmountable but it gets hard!

    Links:

    Delicious Max/MSP Tutorial 1: Karplus-Strong

    The Karplus-Strong Algorithm

    JOS as far as I can see is the world's leading authority on synthetic tone generation, all roads lead back to his website. But be warned, it gets tricky very fast and requires University level maths.

提交回复
热议问题