Browse Source

Multi-channel support for input files

The getsamples function is now able to only get samples from only one channel
tags/v1.8.0
Xerbo 4 years ago
parent
commit
b9bf1e883e
1 changed files with 14 additions and 6 deletions
  1. +14
    -6
      main.c

+ 14
- 6
main.c View File

@@ -58,6 +58,8 @@ extern char TempPalette[256*3];
int zenith = 0; int zenith = 0;
// Audio file // Audio file
static SNDFILE *audioFile; static SNDFILE *audioFile;
// Number of channels in audio file
int channels = 1;


// Function predeclarations // Function predeclarations
static int initsnd(char *filename); static int initsnd(char *filename);
@@ -286,18 +288,24 @@ static int initsnd(char *filename) {
} }
printf("Input sample rate: %d\n", infwav.samplerate); printf("Input sample rate: %d\n", infwav.samplerate);


// TODO: accept stereo audio
if (infwav.channels != 1) {
fprintf(stderr, "Too many channels in input file: %d\n", infwav.channels);
return 0;
}
channels = infwav.channels;


return 1; return 1;
} }


// Read samples from the wave file // Read samples from the wave file
int getsample(float *sample, int nb) { int getsample(float *sample, int nb) {
return sf_read_float(audioFile, sample, nb);
if(channels == 1){
return sf_read_float(audioFile, sample, nb);
}else{
/* Multi channel audio is encoded such as:
* Ch1,Ch2,Ch1,Ch2,Ch1,Ch2
*/
float buf[nb * channels]; // Something like BLKIN*2 could also be used
int samples = sf_read_float(audioFile, buf, nb * channels);
for(int i = 0; i < nb; i++) sample[i] = buf[i * channels];
return samples / channels;
}
} }


static void usage(void) { static void usage(void) {


Loading…
Cancel
Save