- #include <stdio.h>
- #include <stdlib.h>
- #include "alsa/asoundlib.h"
-
- int main(int argc, char *argv[])
- {
- int i;
- int ret;
- int buf[128];
- unsigned int val;
- int dir=0;
- char *buffer;
- int size;
- snd_pcm_uframes_t frames;
- snd_pcm_uframes_t periodsize;
- snd_pcm_t *playback_handle;
- snd_pcm_hw_params_t *hw_params;
- if (argc != 2) {
- printf("error: alsa_play_test [music name]\n");
- exit(1);
- }
- printf("play song %s by wolf\n", argv[1]);
- FILE *fp = fopen(argv[1], "rb");
- if(fp == NULL)
- return 0;
- fseek(fp, 100, SEEK_SET);
-
-
- ret = snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
- if (ret < 0) {
- perror("snd_pcm_open");
- exit(1);
- }
-
-
- ret = snd_pcm_hw_params_malloc(&hw_params);
- if (ret < 0) {
- perror("snd_pcm_hw_params_malloc");
- exit(1);
- }
-
- ret = snd_pcm_hw_params_any(playback_handle, hw_params);
- if (ret < 0) {
- perror("snd_pcm_hw_params_any");
- exit(1);
- }
-
- ret = snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
- if (ret < 0) {
- perror("snd_pcm_hw_params_set_access");
- exit(1);
- }
-
- ret = snd_pcm_hw_params_set_format(playback_handle, hw_params, SND_PCM_FORMAT_U8);
- if (ret < 0) {
- perror("snd_pcm_hw_params_set_format");
- exit(1);
- }
-
-
-
-
- val = 8000;
- ret = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &val, &dir);
- if (ret < 0) {
- perror("snd_pcm_hw_params_set_rate_near");
- exit(1);
- }
-
- ret = snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2);
- if (ret < 0) {
- perror("snd_pcm_hw_params_set_channels");
- exit(1);
- }
-
-
- frames = 32;
- periodsize = frames * 2;
- ret = snd_pcm_hw_params_set_buffer_size_near(playback_handle, hw_params, &periodsize);
- if (ret < 0)
- {
- printf("Unable to set buffer size %li : %s\n", frames * 2, snd_strerror(ret));
-
- }
- periodsize /= 2;
-
- ret = snd_pcm_hw_params_set_period_size_near(playback_handle, hw_params, &periodsize, 0);
- if (ret < 0)
- {
- printf("Unable to set period size %li : %s\n", periodsize, snd_strerror(ret));
- }
-
-
- ret = snd_pcm_hw_params(playback_handle, hw_params);
- if (ret < 0) {
- perror("snd_pcm_hw_params");
- exit(1);
- }
-
-
- snd_pcm_hw_params_get_period_size(hw_params, &frames, &dir);
-
- size = frames * 2;
- buffer = (char *) malloc(size);
- fprintf(stderr,
- "size = %d\n",
- size);
-
- while (1)
- {
- ret = fread(buffer, 1, size, fp);
- if(ret == 0)
- {
- fprintf(stderr, "end of file on input\n");
- break;
- }
- else if (ret != size)
- {
- }
-
- while(ret = snd_pcm_writei(playback_handle, buffer, frames)<0)
- {
- usleep(2000);
- if (ret == -EPIPE)
- {
-
- fprintf(stderr, "underrun occurred\n");
-
- snd_pcm_prepare(playback_handle);
- }
- else if (ret < 0)
- {
- fprintf(stderr,
- "error from writei: %s\n",
- snd_strerror(ret));
- }
- }
-
- }
-
- snd_pcm_close(playback_handle);
-
- return 0;
- }
posted @
2015-11-05 15:41
苍月代表我
阅读(
434)
评论()
编辑
收藏
举报