What would be the best practice of casting a void array to a typedef?
Here's how I do it now:
typedef struct {
int16_t left,right;
} SampleStereo16;
void *buffer[100000];
SampleStereo16* sample;
sample = (SampleStereo16*) buffer;
Everything seems to be working fine, but somehow I feel that there is a better way. I was also wondering if there is a way to have an array of samples and a buffer to share the same memory (now each of them uses its own memory).
Here is the complete code I'm working on now:
#include "fmod.h"
#include "fmod.hpp"
#include "fmod_errors.h"
#include "wincompat.h"
#include <stdio.h>
typedef struct {
int16_t left,right;
} SampleStereo16;
void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("\nFMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
if (result != FMOD_ERR_FILE_EOF)
exit(-1);
}
}
int main(int argc, char *argv[])
{
FMOD::System *system;
FMOD::Sound *sound;
FMOD_RESULT result;
unsigned int version;
int channels;
int bits;
unsigned int lenbytes;
void *buffer[1000000];
unsigned int *read;
unsigned int position;
unsigned int samplesread;
unsigned int samplesbuffer;
unsigned int cueposition;
SampleStereo16* sample;
result = FMOD::System_Create(&system);
ERRCHECK(result);
system->getVersion(&version);
result = system->getVersion(&version);
ERRCHECK(result);
if (version < FMOD_VERSION)
{
printf("Error! You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
getch();
return 0;
}
result = system->setOutput(FMOD_OUTPUTTYPE_ALSA);
ERRCHECK(result);
result = system->init(32, FMOD_INIT_NORMAL, 0);
ERRCHECK(result);
result = system->createStream("/home/dpk/Dropbox/Music/Will Smith - Miami.mp3", FMOD_SOFTWARE, 0, &sound);
result = sound->getFormat(0, 0, &channels, &bits);
ERRCHECK(result);
printf("channels : %d bits : %d \n", channels, bits);
if (channels!=2 and bits!=16)
{
printf("File must be stereo (2 channels) 16 bits \n");
exit(-1);
}
lenbytes = sizeof(buffer);
samplesbuffer = lenbytes / channels / ( bits / 8 );
position = 0;
cueposition = 0;
do
{
result = sound->seekData(position);
ERRCHECK(result);
printf("Reading block : %u ",position);
result = sound->readData(&buffer, lenbytes, read);
ERRCHECK(result);
samplesread = *read / channels / ( bits / 8 );
sample = (SampleStereo16*) buffer;
printf("number of PCM samples read : %u \n", samplesread);
for(unsigned int i=0; i<samplesread; i++)
{
if (cueposition==0 && ( abs(sample[i].left)>500 || abs(sample[i].right)>500 ) )
{
cueposition = position+i;
printf("cue point : %u \n", cueposition);
}
}
position += lenbytes / channels / ( bits / 8 );
} while(samplesread==samplesbuffer);
printf("\nExit\n");
result = sound->release();
ERRCHECK(result);
result = system->close();
ERRCHECK(result);
result = system->release();
ERRCHECK(result);
return 0;
}
I also get segmentation errors when the buffer increase is too large, but from what I have been able to find so far, it seems that this is a stack size limit.
Also feel free to comment on everything I do wrong, I recently started C ++ (last week), so I'm sure some things look bad in my code.