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

Thread: Teensy-Based Sampler: "dynamic" Memory Allocation?

  1. #1
    Senior Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    171

    Teensy-Based Sampler: "dynamic" Memory Allocation?

    I have a Teensy with some extra RAM soldered on. I can play a modest amount of uncompressed WAV samples I load from SD.

    This is a multitimbral sampler and I want to be able to switch samples quickly: I want to have Voice One load a sample, play it, and then if I switch to another "kit" I want it load another sample at runtime.

    I can define blocks of memory to load my samples from SD.

    EXTMEM char MEM_BLOCK_1[MAX_EXTMEM];
    EXTMEM char MEM_BLOCK_2[MAX_EXTMEM];
    EXTMEM char MEM_BLOCK_3[MAX_EXTMEM]; etc

    But, this is not efficient because some samples are short, and some are long. I tend to waste memory by giving each "sample voice" in my multitimbral sample and equal amount of fixed memory.

    I think I'm missing something. It looks like some folks use Flash memory to hold samples, and thereby eliminate the need for a "Teensy Memory Manager" that can malloc and free EXTMEM without fragmentation. But I'm not quite seeing the connection.

    So I have two questions.
    1) Can I quickly load, and then swap a sample from flash memory? Can I do it a thousand times without a hitch?
    2) Just curious, are there "memory management libraries" you can use in a Teensy without embracing a complete RTOS paradigm?
    ( I would expect I could pass a big block of EXTMEM into a library and it would handle allocation/deallocation. )

    I know these are multiple divergent questions, but I would appreciate any ideas.

    Thanks,

    Ben

  2. #2
    Senior Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    171
    ...What I'm implying is I want it to act like a sampler that can pull in samples from SD at runtime without hard-coding any samples as constants or as resources that get loaded in on setup().

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,015
    Dynamic allocation is with sm_alloc() given external QSPI RAM

    Some info here: forum.pjrc.com/threads/63695-malloc-free-for-EXTMEM-and-DTCM?highlight=sm_malloc

    It has some smarts about maintaining the memory pool integrity ... that thread even had a graphic display IIRC across some test allocations.

  4. #4
    Senior Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    171
    Awesome, thank you. I'll give it a try.

  5. #5
    Senior Member
    Join Date
    Sep 2020
    Location
    Massachusetts
    Posts
    171
    Good heavens!
    It works.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,015
    Quote Originally Posted by blakeAlbion View Post
    Good heavens!
    It works.
    Awesome indeed!

  7. #7
    Member
    Join Date
    Jan 2020
    Location
    Finland
    Posts
    33
    Hi blakeAlbion!

    Could you share code for your solution, I have been battling the exact same problem with sample playing from SD, but memory allocation is still a bit too much for my skills at the moment.
    It would be super-helpful if I could get a working example code of how you got it working.

    At the moment, I have been using WaveplayerEx library for playing straight from the SD card, but that has still some limitations regarding polyphony. What kind of polyphony are you achieving with your code?

    Best,
    Miro

Posting Permissions

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