Pitch control of sample playback: current state of things?

blakeAlbion

Well-known member
Hi Forum,

I have been searching through the threads on the subject of pitched playback of samples.
Probably my limited brain power, but I have not found a definitive answer.

So I am going to try to be specific here and state my requirements.

Using short samples like those you conveniently can obtain from wav2sketch, I would like to play it back at some arbitrary pitch.
Example: a short sample of a cowbell "played" either faster or slower than the rate it was recorded at.

That seems like a very ordinary requirement to me for an audio library.

From what I have read, there's no direct way to do this using the Teensy Audio Library. I could take a tiny snippet of my sample and put it into the waveform player. That's not what I want. I want a pitch parameter on the AudioPlayMemory interface. That is exactly what I want. I want changing pitch to be as easy and fast as changing volume. This is a basic requirement to map MIDI to samples by pitch and velocity.

I realize that's like asking to add BSP rendering to a 3D library just as an afterthought. But hey, it's also a fundamental building block.

Am I missing something? Sorry if I did.

(this is just a sign that I'm really excited about this library and this platform)

Ben
 
I suspect this is because there's no simple easy way to do this for an arbitrary rate-conversion - the technically
ideal way requires sinc-pulse summation (band-limited pulses) which is very to extremely compute-intensive
depending on the frequency parameters.

There are special cases when the frequency ratio is a simple ratio allowing resampling by decimation and filtering
and if you are prepared to have some aliasing interpolation can be done the cheap linear method for an arbirary
ratio, whether this is acceptable quality would be dependent on the spectral components of the sample itself, and
the frequency ratio.

A fairly common approach in sampling synths is to resample for each octave so the bandwidth is controlled and
such that only cheap linear-interpolation is needed.

Basically there are many approaches, each with issues, so its not obvious exactly what the library should be providing.

Its a much more complex area than I think you were expecting...
 
I understand it's complex, but it's a deal-breaker for people who just want it to work the way they expect it to.
I can do this in the Chrome browser. I could do this in the DirectX SDK in 1998. I could do this in HyperCard in 1988.
I can't make a singing kitten sound track on my Teensy without it.
I guess I would prefer a quick-and-dirty resampling to a precise pitch adjustment with minimal artifacts. Being able to pitch it musically is nice, but non-chromatic is a good start.
Sometimes any is better than none.
 
Just to be more clear, what I’m trying to describe is simply a playback rate adjustment. Not a pitch-shifting effect you could, for example use with your electric guitar in a live performance.
I believe these are very different requirements and require different approaches.
 
Pitched or not, the Teensy 4 makes a wicked drum machine. Mixing drum synth sounds with some old favorite samples is awesome.
 
Okay! I went down the rabbit hole and got the granular effect working.
To answer my question, NO, you can't directly control playback rate.
But you can do some crazy weird things with pitch downstream. For my application, I think it has potential, but because it's an "effect" the granular model requires a big buffer and causes delay (confusion and delay).
Verdict: It's fun.
See the granular example in the library. Or watch the video.
 
Just to be more clear, what I’m trying to describe is simply a playback rate adjustment. Not a pitch-shifting effect you could, for example use with your electric guitar in a live performance.
I believe these are very different requirements and require different approaches.

Audio ADCs and DACs typically may not support dynamically changing sample rates, requiring quartz-stable
clocking to get good results - they often have clock-speed detection circuits to switch in and out digital filtering, and
change internal over-sampling factors etc etc. Internally high-resolution signal converters are pipelined sigma-delta
architecture, they are complex beasts to get the performance.

The drivers for them set up a particular set of I2S clocks and a regularly clocked DMA to feed data to/from the device,
having to change these clocks and DMA settings on the fly would be a challenge (and would be hard to do without
producing obvious artifacts, thinking about it).

If you have two or more different samples to playback simultaneously at arbitrary pitches you can't just use sample
playback rate changes...

Playing back at low sample rates will have strong aliasing artifacts, only fixable with analog brick-wall filtering after
the DAC - thus you really want to keep sampling rate above 40kSPS to prevent this.

Even a 6 octave range is a 64:1 ratio in sample rate, and 40kSPS to 2.56MSPS range is beyond most DACs.

As I said this is much more complex area than you might expect, and the ability of the ear to easily detect aliased tones
at -50dB or lower is very demanding.
 
Everything you mention is correct if the purpose of pitch shifting is a hi-fi effect and you’re trying convince your audience this sound is naturally played at this rate.
What if I don’t care? In the old days I had a ring buffer and I had code that could spoon the chunks of a sample into the circular buffer at whatever speed I wanted. It sounded robotic and crunchy, and LOVELY.
 
It sounded robotic and crunchy, and LOVELY.
:)
Well, it is a matter of definition of your goals!

You mention three different of your goals in this thread:

* pitch shifting
* playback rate adjustment
* crunchy and lovely audio effects

All of those are feasible with the Teensy (but they need different levels of care and complexity) :).

Our mileages may vary temporally and spatially and -of course- individually, thats perfectly OK.
 
Playback rate adjustment is the only material goal. The rest will arise from the bubbling cauldron.

I'm sorry if I sound daft. I understand there's a shortage of timebases in this architecture. I understand also that the Teensy Audio Library works with hunks of audio designed to fit in the hardware-defined pipeline.

But I am also aware of history, and it seems a shame that something I could do so easily 30 years ago with so much less CPU/FPU and so much less memory and such a lower clock speed... Cannot be done with this amazing slab. I doesn't make me love the Teensy any less, but it's ironic.
 
Granted, it took the 56000 DSP, and the AT&T chipset for the "Quadra AV Macs" to enable my Mac to do decent sound manipulation.
But I was doing bad sound manipulation long before that. I just want the right to be bad.
 
Vocoding normally just adds formants, which is a stack of tunable resonant filters, not a change in pitch of the signal itself.
 
Hi Ben, can you through up an example of the sketch and samples that you're using and I'll have a go. cheers, Paul
 
Vocoding is a kind of re-synthesis, but if done with enough resolution, it still gives the best results for a pitch shifter.

Here is a non-arduino library called soundtouch, that could be inserted in a project, but it is LGPL licensed.
http://www.surina.net/soundtouch/faq.html

I think the by far the best algorithm in both sound and quality, is the Elastique Pitch, which is commercial.
 
Sounds like about 30 minutes of work if you already have the kitten sample as an array of bytes or ints... You'll need to right a simple class derived from Audiostream. Maybe hack "synth_sine" class for starters...
 
oooohhhh!
AudioPlaySdRawResampled.cpp! (The tasty part is in the h file.)
The implementation in this case depends upon SDReader having an adjustable rate.
Spoonfuls of sample. Thanks!
 
It has helped me to read up on I2S, understand some of the inherent limitations, and see what other folks are doing. In comparison to Arduino Sound library, you see how mature TSL is.
 
No, for now I will give it a rest. What I want is a sampler that works like a sampler. This is like an iPod (a hardware codec really for playing back compressed music) with an effects rack (a dsp). I would rather have a good sampler with no effects.
 
Ah, but to me that is by definition not a "good" sampler because there is no control over playback rate. Who would buy a sampler that only played back your sample at the original speed? That's not a sampler, it's a dictaphone. The only way to adjust pitch in the Teensy is via an "effect" which means you must allocate a big buffer and your playback is now "going through an effect" which creates lag? Who wants a sampler that can only change playback rate if it must be delayed?
If your only tool is a DSP, every problem looks like an effect. But I don't want an effect.
 
I'm looking at this from 2 perspectives, from a features/requirements perspective, and from a hardware hacker perspective.

From a features/requirements perspective:
For sampler-as-musical-instrument, you would not choose an audio playback engine incapable of nimble and broad adjustable playback rate. You just wouldn't, because that last 20% of missing functionality costs more than the 80% you get from everything else.

From a hardware hacker perspective:
... A block diagram of a sampler would include adjustable playback rate somewhere outside of the effects chain. The sampler needs an architecture dedicated to the musical playback of samples. That's not easy to add as an afterthought. The Teensy itself can be programmed to do whatever we want, but the Audio Library is rightfully standing on the shoulders of audio playback hardware not intended for this purpose. If I decide I want to use the Mozzi library to play samples at a musical pitch, what I give up is the excellent codecs and sound quality of the Teensy Audio Library; I am back to using PWM to play my samples. Now granted, Teensy PWM is some of the best PWM, but dang.

I'm asking for the moon. I'm asking for a new audio board with new capabilities. Now that I understand what I'm asking for, I realize it's an unreasonable ask. That's okay. The Teensy Audio Library is amazing and fun. I'll leave it at that.
 
Back
Top