Cabbage Logo
Back to Cabbage Site

Pitch shifting audio Files

Hi @rorywalsh ,thanks again for your help with the last issue. again any help would be fantastic, I am trying to pitch shift the audio files by a random amount within say plus or minus two semi tones on each button press. I know its possible to pitch shift the audio file by adjusting kpitch after diskin2. I am just wondering is it possible to do randomize the pitch this way or am I going about this the wrong way.

instr 11

a1, a2 diskin2 "OneShot1.wav", .9

kVol chnget "volumeSlider4"
kToggle chnget "OneShotLPF"
kReverbSize chnget "reverbMix"
kReverbCutoff chnget "reverbCutoff"
kReverbMix chnget "reverbMix"


kCutoff = 1000
if kToggle == 1 then
    a1 butlp a1, kCutoff
    a2 butlp a2, kCutoff
endif

aLReverb, aRReverb reverbsc a1, a2, kReverbSize, kReverbCutoff

aOutL = a1 * (1 - kReverbMix) + aLReverb * kReverbMix
aOutR = a2 * (1 - kReverbMix) + aRReverb * kReverbMix

outs aOutL * kVol, aOutR * kVol

endin

My gut instinct is that you’d be better off reading in the file once, storing it in a table (doing so would store it in RAM), and reading back the table at your randomly assigned rates.

Using the diskin2 approach you can just do something like this:

a1, a2 diskin2 "OneShot1.wav", 1+randi:k(1, .1)

that will cause tiny detuning to take place over time. You might want to look at the jspline or jitter opcodes for a more organic type of random number generation.

Thanks again for the help, I’ll have to figure out how to activate the change in the playback instrument. I’m having a similar issue to the one I had yesterday in that it is picking a random number and playing back the sound file but its only picking the random number on loading the program rather than each time the sound file is played

instr 111
kButton11, kButton11Trig cabbageGetValue “playButton11”

if (kButton11Trig == 1) then
    event "i", 11, 0, -1
endif

kStopButton4, kStopButtonTrig cabbageGetValue "stopButton4"

if (kStopButtonTrig == 1) then
    turnoff2 11, 0, 0
endif

endin

Hi @rorywalsh, if you have the time I can’t figure out for the life of me how to pitch shift the audio files any time they are activated by different amounts within a set range. I’m trying for something like what FMOD can do where I could load in say the sound of a gunshot and it will shift the pitch slightly every time its triggered. I have tried a couple of versions of randomizing the kpitch number in the diskin2 opcode but I cant seem to get it working.

This is my code at the minute

instr OneShot
SFileOS sprintf "Oneshot%d.wav", p4
a1, a2 diskin2 SFileOS, 1, 0, 0

kVolOS chnget "volumeSliderOS"
kToggle chnget "OneShotLPF"
kReverbSize chnget "reverbMix"
kReverbMix chnget "reverbMix"

kCutoff = 1000
if kToggle == 1 then
    a1 butlp a1, kCutoff
    a2 butlp a2, kCutoff
endif

denorm a1, a2

aLReverb, aRReverb reverbsc a1, a2, kReverbSize, 12000

aOutL = a1 * (1 - kReverbMix) + aLReverb * kReverbMix
aOutR = a2 * (1 - kReverbMix) + aRReverb * kReverbMix

outs aOutL * kVolOS, aOutR * kVolOS

endin

instr OneShotPlay
    SPlayOS sprintf "OSplay%d", p4
    kButtonOS, kButtonTrigOS cabbageGetValue SPlayOS

if (kButtonTrigOS == 1 && kButtonOS == 1) then
    event "i", "OneShot", 0, -1, p4
elseif (kButtonTrigOS == 1 && kButtonOS == 0) then
    turnoff2 9+(p4*.1), 0, 0
endif

kStopButtonOS, kStopButtonTrigOS cabbageGetValue "stopButtonOS"
if (kStopButtonTrigOS == 1) then
    turnoff2 9.1, 0, 0
    turnoff2 9.2, 0, 0
    turnoff2 9.3, 0, 0
    turnoff2 9.4, 0, 0
    turnoff2 9.5, 0, 0
    turnoff2 9.6, 0, 0
    turnoff2 9.7, 0, 0
    turnoff2 9.8, 0, 0
    turnoff2 9.9, 0, 0
    turnoff2 9.10, 0, 0
    turnoff2 9.11, 0, 0
    turnoff2 9.12, 0, 0
    turnoff2 9.13, 0, 0
    turnoff2 9.14, 0, 0
    turnoff2 9.15, 0, 0
    turnoff2 9.16, 0, 0
endif

endin

If you want it to play at a different pitch each time it is triggered, use an i-rate random number generator.

seed 0 ;// make sure it's a unique random number
iPitch random 0.95, 1.05
a1, a2 diskin2 SFileOS, iPitch, 0, 0

@rorywalsh, three lines of code… I’ve have tried literally a dozen methods including editing the sound files so that there were three differently pitched .wav files for each sound and tryin to get them to pick round robin style and none worked :joy: :joy: Thank you so much