How to play sounds?

Because it came from Arduino (ATMega 328)

That is a terrifyingly long function.
Let’s hope that can be replaced someday.

That explains the presence of byte, but not byte being mixed with uint8_t.
(Not that it would be the first of Arduino’s weird decisions.)

When I have chance I’ll try to make a PR to replace the bytes with uint8_ts.
(And perhaps introduce a version that accepts a data structure instead of 17 parameters.)

1 Like

mixing multiple samples is reasonably straight forward…

uint8_t mixSound()
    int temp = 0;
    signed int ss[4];
    for(int s=0; s<4; s++){
        // currentPos is theposition that the played sample is up to, here I adjust it for playing at different speeds.
        int currentPos = (snd[s].soundPoint*snd[s].speed)>>8;

        if( currentPos >= snd[s].currentSoundSize){
                snd[s].soundPoint = snd[s].repeat;

        ss[s] = snd[s].currentSound[currentPos] -128;
        ss[s] *= snd[s].volume;
        ss[s] = ss[s]>>8;
        ss[s] *= snd[s].playSample; // will be 1 or 0, if not playing, will silence

    temp = (ss[0] + ss[1] + ss[2] + ss[3])/4;
    return temp +128;

Simply put, take your current sample, minus 128 to make it signed, add in your other samples (-128 also), divide by how many samples are playing than add 128 to unsign it again.



PokittoSound::playSFX( const uint8_t *sfx, uint32_t length)


#define PROJ_HIRES 0
#define PROJ_AUD_FREQ 8000

And it doesn’t work, no sound is played, plus there is this weird constant silent buzz from the speaker.

@jpfli’s bytebeat code gives me errors

'buffers' was not declared in this scope
'currentBuffer' was not declared in this scope

And @spinal’s code doesn’t compile either:

../PokittoLib/POKITTO_HW/timer_11u6x.h:132: multiple definition of TIMER32_0_IRQHandler’`


Actually the original @spinal’s code does work after a few edits, thank you, I have something to work with now.

1 Like

I’ve added the final snippet into my original post.

Great that you found a solution.

I just tested it by creating new c++ project in FemtoIDE and copy-pasting both main.cpp and My_settings.h and it works. If streaming music is not enabled in My_settings.h, then I get the same error.

Maybe you tried my modified version of spinal’s code and forgot to also copy My_settings.h? You get that error message because Pokitto sound is not disabled as it should be for that version to work.

1 Like

That code higher up for manually enabling the dac results in a high pitch noise on my Pokitto. Is it the same for everyone else?

I remember there might have been some background hiss.

The code is taken from MiniLib. Comparing it to standard PokittoLib, I see that after
the actual DAC init in soundInit(uint8_t reinit)-function in POKITTO_HW/HWSound.cpp there is this:

// init amp
LPC_GPIO_PORT->DIR[1] |= (1 << 17);
LPC_GPIO_PORT->SET[1] = (1 << 17);

I can’t test it now but could that be the reason? You can try adding this at the end of enableDAC():

// DIR1 is already declared above
volatile unsigned int* SET1 = (unsigned int*)(0xa0002204);
*DIR1 |= 1 << 17;
*SET1 = 1 << 17;

Have to be careful swapping code around between MiniLib and PokittoLib.
A lot of the initialization that is scattered around mbed and PokittoLib has been merged together into MiniLib’s SystemInit.s.

Still whistles :frowning:

I’ll look into this more closely next week when I’m back home.

Re: whistling. Is this the mangled old pokitto or the new one?

1 Like

New one, didn’t try on the mangled one :sunglasses:
[edit] it’s only the above code, pokittolib setup works fine.

I don’t hear whistling or other strange noise. There is some hiss when sound is playing, at least when using the built in speaker, but nothing else.

Here is a recording from my Pokitto, recorded through line-in, and the test program I used: (104.8 KB)

I included the ‘init amp’ lines although they didn’t change anything. I also added one line to the enableTimer function to reset the timer. It has no effect on sound quality, but I noticed that without it, writing to eeprom (when changing sound volume) caused timer to not start (happened only when using emulator, not on hw). The test program has three slightly different methods to initialize timer, but they all sound the same.