0

I am trying to read a wave file and play it on one of the sound cards on the board. Below is the code. I am facing several problems with this code.

  1. I am trying to play files with various sample rates. With a file of Sampling rate of 8KHz, File doesn't play completely, even though the file dump shows entire content of the file.
  2. If I play a file of 11025 Hz, I hear only noise
  3. For stereo data I get garbled audio.

PS :

  1. I am setting the rate obtained from the wave header
  2. Period size is set to 10 ms. i.e for 16KHz - 160; 11025 - 110

    ret = snd_pcm_open(&handlePCMHD, "hw:0,0",
        SND_PCM_STREAM_PLAYBACK, 0);
    if (ret < 0) {
        snd_pcm_close(handlePCMHD);
        return -1;
    }
    
    fd = open(fileName, O_RDONLY);
    /* file dump */
    fd2 = open("test.raw", O_WRONLY | O_CREAT);
    if(fd < 0)
    {
        return -1;
    }
    
    /* Wave file header is 44 bytes */
    if(read(fd, buf, 44)){
        sampleRate = buf[12] | ((unsigned int)buf[13] << 16);
    
        channels = buf[11];
    }else{
        //read failed
    }
    
    set_pcmhd_hw_param(handlePCMHD, sampleRate);
    
    /* Buffer size = SampleRate * Number of Bytes per frame * No of Periods / For 10 ms */
    readBytes = (sampleRate * 2 * 2)/100;
    
    while((channels == 1) ? (x = read(fd, buf, readBytes)) : (x = read(fd, stereobuf, readBytes * 2)))
    {
        if(channels == 2){
            StereoToMono_samples(buf ,stereobuf, readBytes/2);
        }
        write(fd2, buf, readBytes);
        /* Number of frames = read bytes / 2 for 16 bit PCM */
        x = readBytes / 2;
        if ((ret = snd_pcm_writei(handlePCMHD, buf, (snd_pcm_uframes_t)x) == -EPIPE)) {
            snd_pcm_prepare(handlePCMHD);
        } else if (ret < 0) {
            //error handling
        } else{
            count++;
        }
    }
    close(fd);
    close(fd2);
    snd_pcm_close(handlePCMHD);
    
Rohit Walavalkar
  • 790
  • 9
  • 28

0 Answers0