Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 30th, 2010  |  syntax: C  |  size: 1.54 KB  |  views: 894  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <alsa/asoundlib.h>
  4.  
  5. #include <math.h>
  6.  
  7. #include "main.h"
  8.  
  9. double fclip(double value, double start, double end) {
  10.   return 0.5 * ( fabs(value-start) + (start+end) - fabs(value-end) );
  11. }
  12.  
  13. void play(struct user_data_s* data) {
  14.  
  15.   static char* device = "default";
  16.  
  17.   /* snd_output_t* output = NULL; */
  18.  
  19.   int err;
  20.   snd_pcm_t* handle;
  21.   snd_pcm_sframes_t frames;
  22.  
  23.   short buffer[data->audio_info_save->frames];
  24.   for(unsigned long int i = 0; i < data->audio_info_save->frames; i++ ) {
  25.     buffer[i] = (short)fclip( 32767.0 * data->mono_channel[i], -32767, +32768 );
  26.   }
  27.  
  28.   if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  29.     printf("Playback open error: %s\n", snd_strerror(err));
  30.     exit(1);
  31.   }
  32.  
  33.   if ((err = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16,
  34.                                 SND_PCM_ACCESS_RW_INTERLEAVED, 1,
  35.                                 data->audio_info_save->samplerate,
  36.                                 1, 500000)) < 0) {
  37.     printf("Playback open error: %s\n", snd_strerror(err));
  38.     exit(1);
  39.   }
  40.  
  41.   for (int i = 0; i < 1; i++) {
  42.     frames = snd_pcm_writei(handle, buffer, data->audio_info_save->frames);
  43.  
  44.     if (frames < 0) {
  45.       frames = snd_pcm_recover(handle, frames, 0);
  46.     }
  47.     if (frames < 0) {
  48.       printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
  49.       break;
  50.     }
  51.     if (frames > 0 && frames < (long)data->audio_info_save->frames) {
  52.       printf("Short write (expected %li, wrote %li)\n",
  53.              (long)data->audio_info_save->frames, frames);
  54.     }
  55.   }
  56.  
  57.   snd_pcm_close(handle);
  58.  
  59. }
clone this paste RAW Paste Data