Re: C program for playing sound

Sleep <[email protected]>
Newsgroups gmane.linux.alsa.user
Message-ID <[email protected]>
I've used snd_pcm_prepare() because underrun (-EPIPE) was occurring 
because I had put the wrong value for frames, but despite adjusting the 
value of the frames, I am still getting the scratchy effect, here is the 
current code I have, now I am really stuck. Also yeah, "scratchy" would 
be a better description of the effect I suppose.

I might post a recording of the result later, meanwhile here is the code 
for now, I have removed snd_pcm_prepare() from it.

#include <stdlib.h>
#include <stdint.h>
#include <alsa/asoundlib.h>

#define STEREO 2
#define BITS 16 / 8
#define FRAMEBUFFERSIZE 512 // in samples
#define PERIODS 2
#define SAMPLERATE 11025

int main(void)
{
     snd_pcm_t *handle;
     uint32_t channels = STEREO;
     uint32_t sample_size = BITS; // 16 bit
     uint32_t frame_size = sample_size * channels;
     snd_pcm_uframes_t frames = FRAMEBUFFERSIZE / frame_size;
     snd_pcm_uframes_t frames_per_period = frames / PERIODS;
     int32_t dir = 0; // No idea what this does.
     snd_pcm_hw_params_t *params;
     int16_t framebuffer[FRAMEBUFFERSIZE * STEREO * BITS] = {0};
     FILE *wav;
     int32_t size;

     snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
     snd_pcm_hw_params_alloca(&params);
     snd_pcm_hw_params_any(handle, params);
     snd_pcm_hw_params_set_buffer_size(handle, params, frames);
     snd_pcm_hw_params_set_period_size(handle, params, 
frames_per_period, dir);
     snd_pcm_hw_params_set_rate(handle, params, SAMPLERATE, dir);
     snd_pcm_hw_params_set_access(handle, params, 
SND_PCM_ACCESS_RW_INTERLEAVED);
     snd_pcm_hw_params_set_channels(handle, params, STEREO);
     snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
     snd_pcm_hw_params(handle, params);

     // Insert samples to framebuffer.
     wav = fopen("pcm1611s.wav", "r");
     fseek(wav, 0L, SEEK_END);
     size = ftell(wav);
     fseek(wav, 0L, SEEK_SET);
     size /= sizeof(int16_t);
     while (size > 0)
     {
         fread(framebuffer, sizeof(int16_t), FRAMEBUFFERSIZE * STEREO * 
BITS, wav);
         snd_pcm_writei(handle, framebuffer, frames);
         size -= FRAMEBUFFERSIZE * BITS * STEREO;
     }
     snd_pcm_drain(handle);
     snd_pcm_close(handle);
     exit(EXIT_SUCCESS);
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.