Cabbage Logo
Back to Cabbage Site

Set sample note to a reference midi number

Hi. I’m totally new to Cabbage but enjoying the challenge so far. Both the Cabbage Audio program and forum seem like a nice creative environment.

I have a number of individual instrument samples all with a single sample file at Middle C (midi 60). Using the comboBox example I can see how to load each individual instrument wav file but so far haven’t managed to fix the reference note to midi = 60 and then scale/pitch the other notes. Currently it plays the same note for all keys on the midi keyboard whereas I would like to use the single instrument wav file to be the source for the keyboard range.

Ultimately I would also like to make it future proof for when I have a sample from a different note so I would also include a slider to adjust the reference MIDI note (along the lines of several of the file player examples).

I’ve looked through most of the example files but haven’t managed to find how this would be achieved. Is there an example file that shows how this could be done (that isn’t too complicated with other functions or sound sources)?

I have some comment lines in my .csd file which I’ve tried in various ways without success:
;icps cpsmidi p4 = 60 ; read in midi note data as cycles per second
;iamp ampmidi 1 ; read in midi velocity (as a value within the range 0 - 1)
;iMidiRef chnget “MidiRef” ; MIDI unison reference note
;inote = icps/cpsmidinn(iMidiRef)
;ispeed = icps/cpsmidinn(iMidiRef) ; derive playback speed from note played in relation to a reference note (MIDI note 60 / middle C)
;iFrqRatio = icps/cpsmidinn(iMidiRef) ; derive playback speed from note played in relation to a reference note (MIDI note 60 / middle C)

comboSample w MIDI ref.csd (2.1 KB)

Any help in getting me started on my Cabbage journey would be greatly appreciated.

Hi @wez. You’ll need to re-pitch your samples according to the current MIDI note. Here is a simple example that takes a note at C60 and transposes it according to the incoming MIDI note:

giSample ftgen 1, 0, 0, 1, "C60.wav", 0, 4, 0

instr 1
    iLenInSamples = ftlen(1)
    iSampleNoteNum = 60

    iPlaybackRate = pow(2, (p4-iSampleNoteNum)/12)

    aPhasor phasor (sr/iLenInSamples)*iPlaybackRate
    aTab tab aPhasor*iLenInSamples, 1

    out aTab, aTab
endin

Take it for a spin, it might need some tweaking, let me know if you have any questions. Welcome to the forum :+1:

Thanks Rory for the quick response.

I’m trying to preserve the stereo aspect of the sample so made a second table using a similar approach to the tutorial ‘Reading speed of a GEN table’ for the right channel but all I get is a continuous sound gradually building up with feedback. (Even in single channel ‘mono’ mode I get the same continuous sound.)

I initiated i1 and f1 in the CsScore section but maybe these settings aren’t correct and causing the continuous tone. The MIDI keyboard is ineffectual in changing the sound although I see output changes in the code monitoring window.

testSynth.csd (1005 Bytes)

What’s happening is your starting instrument 1 to play when Csound starts. It is not passed a p4 value so you get this low rumble. Because this is a MIDI driven instrument, you don’t need to manually start instrument 1. It is triggered via MIDI. The other issue was with this line of code:

-n -d -+rtmidi=NULL -M0 --midi-key-cps=4 --midi-velocity-amp=5

It tells Csound to take the incoming MIDI note, and pass its frequency to p4. You don’t want the frequency you want the MIDI key. So you need to change that to:

-n -d -+rtmidi=NULL -M0 --midi-key=4 --midi-velocity-amp=5

Here is the updated version.
testSynth.csd (987 Bytes)

Note that you will still have to deal with loops and sustain points unless it’s a percussive sample or a piano. Csound has a special looping oscillator that can handle loop points. It might be worth looking into.
https://csound.com/docs/manual/loscil.html

Thanks!

My next steps will be integrating file lookup (comboBox), loscil and my first effect.