Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 25 of 25

Thread: Pitch control of sample playback: current state of things?

  1. #1
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50

    Pitch control of sample playback: current state of things?

    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

  2. #2
    Senior Member
    Join Date
    Jul 2020
    Posts
    406
    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...

  3. #3
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  4. #4
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  5. #5
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    Pitched or not, the Teensy 4 makes a wicked drum machine. Mixing drum synth sounds with some old favorite samples is awesome.

  6. #6
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  7. #7
    Senior Member
    Join Date
    Jul 2020
    Posts
    406
    Quote Originally Posted by blakeAlbion View Post
    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.

  8. #8
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  9. #9
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Location
    Central Europe
    Posts
    676
    Quote Originally Posted by blakeAlbion View Post
    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.

  10. #10
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  11. #11
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  12. #12
    Senior Member
    Join Date
    Jun 2018
    Posts
    134
    I would assume that "pitch shifting" is something that is done in the Vocoder project? How does this project relate to what you are asking for? How can it be adapted to do what you need?

    https://forum.pjrc.com/threads/42355...hlight=vocoder

  13. #13
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    And sorry if Iím sounding trollish. This is really interesting and Iím learning. Grateful for the responses.

  14. #14
    Senior Member
    Join Date
    Jul 2020
    Posts
    406
    Vocoding normally just adds formants, which is a stack of tunable resonant filters, not a change in pitch of the signal itself.

  15. #15
    Senior Member houtson's Avatar
    Join Date
    Aug 2015
    Location
    Scotland
    Posts
    132
    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

  16. #16
    Senior Member
    Join Date
    Jan 2020
    Location
    Toscana
    Posts
    130
    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.

  17. #17
    Member
    Join Date
    Nov 2012
    Location
    Portland
    Posts
    59
    Culling through what I can remember, for it is a common topic: for monophonic sample playing FrankB mentioned you could precisely change the overall Teensy speed. I don’t remember seeing any code for that, but it’s clever and expedient.

    Also, and less hacky may be this library, though I haven’t looked into it much less tested: https://github.com/newdigate/Audio/t...mples/Playback

  18. #18
    Junior Member
    Join Date
    Aug 2020
    Posts
    14
    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...

  19. #19
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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!

  20. #20
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  21. #21
    Member
    Join Date
    Nov 2012
    Location
    Portland
    Posts
    59
    Did the AudioPlaySdRawResampled library fit the bill? I'm about to start on a similar polyphonic journey.

    Reviewing the last "useful things not in the standard audio library" bit from my saved threads, you might also peruse the BleepLabs tape delay: https://forum.pjrc.com/threads/41299-My-audio-h-fork-Tape-delay-FM-waveform-input-granular-effect-and-more

  22. #22
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  23. #23
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Location
    Central Europe
    Posts
    676
    Quote Originally Posted by blakeAlbion View Post
    I would rather have a good sampler with no effects.
    Very easy: buy a Teensy 4.1 (SD card slot) with a Teensy audio board and use the LINE IN. The audio from the LINE IN is sampled by the codec and no effect is added.

  24. #24
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

  25. #25
    Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    50
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •