Sound frequency?


#1

I am manually playing audio from a file with the following code…

                    // the sample buffer from the file is a different size to the pokitto
                    if(!fileReadBytes(&tempSound[0], vidhead.sampleSize))stillGoing=0;
                    point = soundbufindex;
                    for(int t=0; t<vidhead.sampleSize; t++){
                        soundbuf[point]=tempSound[t];
                        if(point++ > SBUFSIZE){point=0;}
                    }

Which works reasonably well when the sample frequency is 11025 with

#define PROJ_AUD_FREQ 11025

in mysettings.h

However, if I change the sample frequency to 22050 and define it as such, it plays half speed. Am I missing someting, or is the audio still playing at 11025 even after I changed it?


#2

Could you elaborate which sound interrupt you are using? And, please post a bit longer bit of code to allow me to understand what is happening. Where is this snippet of code used?


#4

It’s from my video player. I was experimenting with better sound quality as it seems to be reading the file fast enough at the current poor setting.

/** Mode 13 (110x88x256) by Spinal, video streaming concept */
#include "Pokitto.h"
#include "SDFileSystem.h"
#include "HWSound.h"
Pokitto::Core game;
int myDelay;
unsigned short pal[256]; // assign a 256 entry array to hold the palette
struct VIDEO_HEADER
{
  uint8_t   ID[3];
  uint8_t   format;
  uint8_t   width;
  uint8_t   height;
  uint16_t  sampleSize;
  uint32_t  frameCount;
  uint8_t   FPS;
  uint32_t  frameSize;
  uint32_t   frameOffset;
} vidhead;
void PFFS_Video() { // Petit Fat File System
    uint32_t myFile = fileOpen("movie.dat", FILE_MODE_READONLY);
if( !myFile ){
    fileReadBytes(&vidhead.ID[0], 3); // should be P,O,K
    fileReadBytes(&vidhead.format, 1); // should be 1
    fileReadBytes(&vidhead.width, 1); // should be 110
    fileReadBytes(&vidhead.height, 1); // should be 88

    vidhead.frameSize = vidhead.width * vidhead.height;
    vidhead.frameOffset = ((88-vidhead.height)/2)*110;

    uint8_t temp1[4];

    fileReadBytes(&temp1[0], 2); // should be ??
    vidhead.sampleSize = (temp1[1] << 8) + temp1[0];
    fileReadBytes(&temp1[0], 4); // should be ??
    vidhead.frameCount = (temp1[3] << 24)+(temp1[2] << 16)+(temp1[1] << 8)+temp1[0];

    fileReadBytes(&vidhead.FPS, 1); // should be 1

    myDelay = 1000/vidhead.FPS;

    unsigned char col[3];
    for(int temp=0; temp<256; temp++){
        fileReadBytes(&col[0], 3);
        pal[temp] = (col[0]>>3) | ((col[1] >> 2) << 5) | ((col[2] >> 3) << 11);
    }
    game.display.load565Palette(&pal[0]); // load a palette the same way as any other palette in any other screen mode
    int sCount=0;

    pokPlayStream();
    int point=0;
    uint8_t tempSound[vidhead.sampleSize];

    bool stillGoing=1;
    uint32_t tempTime  = game.getTime();

    while(stillGoing==1){

        uint32_t elapsed = game.getTime()-tempTime;
        if(elapsed > myDelay){
            tempTime = game.getTime() - (elapsed-myDelay);
            if(game.update()){

                // the sample buffer fromthe file is a different size to the pokitto
                if(!fileReadBytes(&tempSound[0], vidhead.sampleSize))stillGoing=0;
                point = soundbufindex;
                for(int t=0; t<vidhead.sampleSize; t++){
                    soundbuf[point]=tempSound[t];
                    if(point++ > SBUFSIZE){point=0;}
                }
                if(!fileReadBytes(&game.display.screenbuffer[vidhead.frameOffset], vidhead.frameSize))stillGoing=0;

                if(game.rightBtn()){
                    pokPauseStream();
                    for(int t=0; t<SBUFSIZE; t++){
                        soundbuf[point]=0;
                    }
                    point = soundbufindex;
                    fileSeekRelative((vidhead.frameSize+vidhead.sampleSize)*150);
                    pokPlayStream();
                }

            } // update
        } // timer
    } // stillgoing
    fileClose();


} // if myFile
}
void FATFS_video(){
    FILE *myFile = fopen("/sd/movie.dat", "rb");
if (myFile){

    // read header
    fread(&vidhead.ID[0], sizeof(char),3, myFile);
    fread(&vidhead.format, sizeof(char),1, myFile);
    fread(&vidhead.width, sizeof(char),1, myFile);
    fread(&vidhead.height, sizeof(char),1, myFile);

    vidhead.frameSize = vidhead.width * vidhead.height;
    vidhead.frameOffset = ((88-vidhead.height)/2)*110;

    uint8_t temp1[4];
    fread(&temp1[0], sizeof(char),2, myFile);
    vidhead.sampleSize = (temp1[1] << 8) + temp1[0];
    fread(&temp1[0], sizeof(char),4, myFile);
    vidhead.frameCount = (temp1[3] << 24)+(temp1[2] << 16)+(temp1[1] << 8)+temp1[0];
    fread(&vidhead.FPS, sizeof(char),1, myFile);

    myDelay = 1000/vidhead.FPS;

    unsigned char col[3];
    for(int temp=0; temp<256; temp++){
        fread(&col[0], sizeof(char),3, myFile);
        pal[temp] = (col[0]>>3) | ((col[1] >> 2) << 5) | ((col[2] >> 3) << 11);
    }
    game.display.load565Palette(&pal[0]); // load a palette the same way as any other palette in any other screen mode
    int sCount=0;

    pokPlayStream();
    int point=0;
    uint8_t tempSound[vidhead.sampleSize];
    bool stillGoing=1;
    uint32_t tempTime  = game.getTime();

    while(stillGoing==1){
        // delay, 15fps
        uint32_t elapsed = game.getTime()-tempTime;
        if(elapsed > myDelay){
            tempTime = game.getTime() - (elapsed-myDelay);
            if(game.update()){

                // the sample buffer fromthe file is a different size to the pokitto
                if(!fread(&tempSound[0], sizeof(char),vidhead.sampleSize, myFile))stillGoing=0;
                point = soundbufindex;
                for(int t=0; t<vidhead.sampleSize; t++){
                    soundbuf[point]=tempSound[t];
                    if(point++ > SBUFSIZE){point=0;}
                }
                if(!fread(&game.display.screenbuffer[vidhead.frameOffset], sizeof(char),vidhead.frameSize, myFile))stillGoing=0;

                if(game.rightBtn()){
                    pokPauseStream();
                    for(int t=0; t<SBUFSIZE; t++){
                        soundbuf[point]=0;
                    }
                    point = soundbufindex;
                    fseek(myFile, (vidhead.frameSize+vidhead.sampleSize)*150, SEEK_CUR);
                    pokPlayStream();
                }

            } // update
        } // timer
    } // stillgoing
    fclose(myFile);
} // myFile
}
int main(){
    game.begin();
    game.display.persistence=1;
    game.setFrameRate(999);
    int temp;
SDFileSystem sd(/*MOSI*/P0_9, /*MISO*/P0_8, /*SCK*/P0_6, /*CS*/P0_7, /*Mountpoint*/"sd");
pokInitSD(); // Call init always.

game.display.print("A for PFFS");
game.display.print("B for FATFS");

while (game.isRunning()) {

    if(game.update()){
        if(game.aBtn()){PFFS_Video();}
        if(game.bBtn()){FATFS_video();}
    }
}
return 1;
}

#5

Unrelated to the issue you’re having, but that’s a buffer overflow. It should be:

if(point >= SBUFSIZE){point=0;}
soundbuf[point++]=tempSound[t];

#6

Weirdly I seem to not have the issue anymore. Perhaps I’m reading the wrong file…


#7

Small remark, since setFrameRate accepts an uint8_t as input, your actual max frame rate is set to 231 instead of 999…


#8

(Isn’t there supposed to be a warning for providing a literal that doesn’t fit the container?)


#9

How 231? isn’t uint8_t 0-255?

[edit] - Ahh, overflow.


#10

It should do, but currently the PokittoLib produces a ton of warnings so I’m guessing warnings about user code might end up getting buried a bit.

(Either that or @spinal doesn’t read the warnings :P.)


#11

A bit of both.