Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Trying to set some parameters for ALSA.
- *
- * Code from: http://equalarea.com/paul/alsa-audio.html
- *
- */
- #include <alsa/asoundlib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <math.h>
- #define PCM_DEVICE "default"
- int main(int argc, char **argv)
- {
- unsigned int pcm;
- snd_pcm_t *pcm_handle;
- snd_pcm_hw_params_t *params;
- char test[5];
- if(pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0) < 0) {
- printf("ERROR: Can't open \"%s\" PCM device. %s\n", PCM_DEVICE, snd_strerror(pcm));
- return 1;
- }
- snd_pcm_hw_params_alloca(¶ms);
- snd_pcm_hw_params_any(pcm_handle, params);
- if(pcm = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
- printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
- return 1;
- }
- if(pcm = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_FLOAT) < 0) {
- printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
- return 1;
- }
- int channels = 2;
- if(pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0) {
- printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
- return 1;
- }
- int rate = 44100;
- if(pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0) {
- printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
- return 1;
- }
- printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
- printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
- int ch;
- snd_pcm_hw_params_get_channels(params, &ch);
- if(ch > 1) {
- printf("Channels: %i, stereo\n", ch);
- }
- else if(ch == 1) {
- printf("Channels: %i, mono\n", ch);
- }
- snd_pcm_hw_params_get_rate(params, &ch, 0);
- printf("Rate: %d bps\n", ch);
- int dir;
- snd_pcm_uframes_t frames = 1024;
- printf("Attempting to set frames to %d\n", frames);
- snd_pcm_hw_params_set_period_size_near(pcm_handle, params, &frames, &dir);
- snd_pcm_hw_params_get_period_size(params, &frames, 0);
- printf("Frames is now: %zd\n", frames);
- snd_pcm_hw_params_get_period_time(params, &ch, NULL);
- int buffer_size = 4096;
- printf("Attempting to set the buffer size to: %d\n", buffer_size);
- snd_pcm_hw_params_set_buffer_size(pcm_handle, params, buffer_size);
- if(pcm = snd_pcm_hw_params(pcm_handle, params) < 0) {
- printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
- return 1;
- }
- snd_pcm_uframes_t temp;
- snd_pcm_hw_params_get_buffer_size(params, &temp);
- printf("Buffer size is now: %d\n", temp);
- snd_pcm_drain(pcm_handle);
- snd_pcm_close(pcm_handle);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement