/* close the audio device */
int ao_plugin_close(ao_device *device)
{
	ao_alsa_internal *internal;

	if (device) {
          if ((internal = (ao_alsa_internal *) device->internal)) {
            if (internal->pcm_handle) {

              /* this is a PulseAudio ALSA emulation bug workaround;
                 snd_pcm_drain always takes about 2 seconds, even if
                 there's nothing to drain.  Rather than wait for no
                 reason, determine the current playback depth, wait
                 that long, then kill the stream.  Remove this code
                 once Pulse gets fixed. */

              snd_pcm_sframes_t sframes;
              if(snd_pcm_delay (internal->pcm_handle, &sframes)){
                snd_pcm_drain(internal->pcm_handle);
              }else{
                double s = (double)(sframes - internal->static_delay)/internal->sample_rate;
                if(s>1){
                  /* something went wrong; fall back */
                  snd_pcm_drain(internal->pcm_handle);
                }else{
                  if(s>0){
                    struct timespec sleep,wake;
                    sleep.tv_sec = (int)s;
                    sleep.tv_nsec = (s-sleep.tv_sec)*1000000000;
                    while(nanosleep(&sleep,&wake)<0){
                      if(errno==EINTR)
                        sleep=wake;
                      else
                        break;
                    }
                  }
                }
              }
              snd_pcm_close(internal->pcm_handle);
              if(internal->local_config)
                snd_config_delete(internal->local_config);
              internal->local_config=NULL;
              internal->pcm_handle=NULL;
            }
          } else
            awarn("ao_plugin_close called with uninitialized ao_device->internal\n");
	} else
          awarn("ao_plugin_close called with uninitialized ao_device\n");

	return 1;
}
Beispiel #2
0
void Audio::run()
{
    if (!initialized)
    {
        init();
        initialized = true;
        qDebug() << "Audio components initialized.";
    }

    if (synth->masterPlaying && synth->isPlaying())
    {
        replaceBuffer(synth->genChunk());
        playBuffer();
//        resetBuffer();
    }

    if (state != snd_pcm_state(handle))
    {
        state = snd_pcm_state(handle);
        qDebug() << "State: " << state;
    }

    // Buffer underrun (not being written to fast enough)
    if (state == 4)
    {
        snd_pcm_drain(handle);
        prepareDevice();
    }
}
Beispiel #3
0
static void stop(void) {
    if (alsa_handle) {
        snd_pcm_drain(alsa_handle);
        snd_pcm_close(alsa_handle);
        alsa_handle = NULL;
    }
}
int
sa_stream_drain(sa_stream_t *s)
{
  if (s == NULL || s->output_unit == NULL) {
    return SA_ERROR_NO_INIT;
  }

  if (snd_pcm_state(s->output_unit) == SND_PCM_STATE_PREPARED) {
    size_t min_samples = 0;
    size_t min_bytes = 0;
    void *buf;

    if (sa_stream_get_min_write(s, &min_samples) < 0)
      return SA_ERROR_SYSTEM;
    min_bytes = snd_pcm_frames_to_bytes(s->output_unit, min_samples);    

    buf = malloc(min_bytes);
    if (!buf)
      return SA_ERROR_SYSTEM;
    memset(buf, 0, min_bytes);
    sa_stream_write(s, buf, min_bytes);
    free(buf);
  }

  if (snd_pcm_state(s->output_unit) != SND_PCM_STATE_RUNNING) {
    return SA_ERROR_INVALID;
  }
  snd_pcm_drain(s->output_unit);
  return SA_SUCCESS;
}
Beispiel #5
0
 static void alsaCleanup(Soloud *aSoloud)
 {
     if (0 == aSoloud->mBackendData)
     {
         return;
     }
     ALSAData *data = static_cast<ALSAData*>(aSoloud->mBackendData);
     data->audioProcessingDone = true;
     if (data->threadHandle)
     {
         Thread::wait(data->threadHandle);
         Thread::release(data->threadHandle);
     }
     snd_pcm_drain(data->alsaDeviceHandle);
     snd_pcm_close(data->alsaDeviceHandle);
     if (0 != data->sampleBuffer)
     {
         delete[] data->sampleBuffer;
     }
     if (0 != data->buffer)
     {
         delete[] data->buffer;
     }
     delete data;
     aSoloud->mBackendData = 0;
 }
Beispiel #6
0
static int alsa_can_read(snd_pcm_t *dev)
{
	snd_pcm_sframes_t avail;
	int err;

	alsa_resume(dev);
	avail = snd_pcm_avail_update(dev);
	/* A buggy driver does not return an error while being in Xrun */
	if (avail >= 0 && snd_pcm_state(dev) == SND_PCM_STATE_XRUN) avail=-EPIPE;
	if (avail < 0) {
		ms_error("snd_pcm_avail_update: %s", snd_strerror(avail));	// most probably -EPIPE
		/* overrun occured, snd_pcm_state() would return SND_PCM_STATE_XRUN
		 FIXME: handle other error conditions*/
		ms_error("*** alsa_can_read fixup, trying to recover");
		snd_pcm_drain(dev); /* Ignore possible error, at least -EAGAIN.*/
		err = snd_pcm_recover(dev, avail, 0);
		if (err){
			ms_error("snd_pcm_recover() failed with err %d: %s", err, snd_strerror(err));
			return -1;
		}
		err = snd_pcm_start(dev);
		if (err){
			ms_error("snd_pcm_start() failed with err %d: %s", err, snd_strerror(err));
			return -1;
		}
		ms_message("Recovery done");
	}
	return avail;
}
Beispiel #7
0
// 释放资源
void sound_info_free(struct SoundInfo *info) {
    
    snd_pcm_drain(info->handle);
    snd_pcm_close(info->handle);
    free(info->buffer);
    free(info);
}
Beispiel #8
0
 int main(int argc, char *argv[]) 
 { 
     char *filename; 
     char *devicename = "default"; 
     int fd; 
     WAVContainer_t wav; 
     SNDPCMContainer_t playback; 
      
     if (argc != 2) { 
         fprintf(stderr, "Usage: ./lplay <file name>/n"); 
         return -1; 
     } 
      
     memset(&playback, 0x0, sizeof(playback)); 
  
     filename = argv[1]; 
     fd = open(filename, O_RDONLY); 
     if (fd < 0) { 
         fprintf(stderr, "Error open [%s]/n", filename); 
         return -1; 
     } 
      
     if (WAV_ReadHeader(fd, &wav) < 0) { 
         fprintf(stderr, "Error WAV_Parse [%s]/n", filename); 
         goto Err; 
     } 
  
     if (snd_output_stdio_attach(&playback.log, stderr, 0) < 0) {
         fprintf(stderr, "Error snd_output_stdio_attach/n"); 
         goto Err; 
     } 
  
     if (snd_pcm_open(&playback.handle, devicename, SND_PCM_STREAM_PLAYBACK, 0) < 0) { 
         fprintf(stderr, "Error snd_pcm_open [ %s]/n", devicename); 
         goto Err; 
     } 
  
     if (SNDWAV_SetParams(&playback, &wav) < 0) { 
         fprintf(stderr, "Error set_snd_pcm_params/n"); 
         goto Err; 
     } 
     snd_pcm_dump(playback.handle, playback.log); 
  
     SNDWAV_Play(&playback, &wav, fd); 
  
     snd_pcm_drain(playback.handle); 
  
     close(fd); 
     free(playback.data_buf); 
     snd_output_close(playback.log); 
     snd_pcm_close(playback.handle); 
     return 0; 
  
 Err: 
     close(fd); 
     if (playback.data_buf) free(playback.data_buf); 
     if (playback.log) snd_output_close(playback.log); 
     if (playback.handle) snd_pcm_close(playback.handle); 
     return -1; 
 } 
Beispiel #9
0
void Mouth::close(){
    snd_pcm_hw_params_free(params);
    snd_pcm_drain(capture_handle);
    snd_pcm_close(capture_handle);
    snd_config_update_free_global();
    open=false;
}
Beispiel #10
0
bool SoundProcessor::Record(){
  SetUpRecorder();
  snd_pcm_sframes_t fwdframes=snd_pcm_forwardable(handle_m);
  printf("forwarding %d\n",fwdframes);
  snd_pcm_forward(handle_m,fwdframes);
  while (true) {
    rc_m = snd_pcm_readi(handle_m, buffer_m, frames);
    if (rc_m == -EPIPE) {
      /* EPIPE means overrun */
      fprintf(stderr, "overrun occurred\n");
      snd_pcm_prepare(handle_m);
    } else if (rc_m < 0) {
      fprintf(stderr,
              "error from read: %s\n",
              snd_strerror(rc_m));
    } else if (rc_m != (int)frames) {
      fprintf(stderr, "short read, read %d frames\n", rc_m);
    }
    rc_m = write(1, buffer_m, size);
    if (rc_m != size)
      fprintf(stderr,
              "short write: wrote %d bytes\n", rc_m);
  }

  snd_pcm_drain(handle_m);
  snd_pcm_close(handle_m);
  free(buffer_m);
  return true;
}
/*----------------------------------------------------------------------
|    AlsaOutput_Close
+---------------------------------------------------------------------*/
static BLT_Result
AlsaOutput_Close(AlsaOutput* self)
{
    ATX_LOG_FINER("closing output");

    switch (self->state) {
      case BLT_ALSA_OUTPUT_STATE_CLOSED:
        /* ignore */
        return BLT_SUCCESS;

      case BLT_ALSA_OUTPUT_STATE_PREPARED:
        /* wait for buffers to finish */
        ATX_LOG_FINER("snd_pcm_drain");
        snd_pcm_drain(self->device_handle);
        /* FALLTHROUGH */

      case BLT_ALSA_OUTPUT_STATE_OPEN:
      case BLT_ALSA_OUTPUT_STATE_CONFIGURED:
        /* close the device */
        ATX_LOG_FINER("snd_pcm_close");
        snd_pcm_close(self->device_handle);
        self->device_handle = NULL;
        break;
    }

    /* update the state */
    AlsaOutput_SetState(self, BLT_ALSA_OUTPUT_STATE_CLOSED);

    return BLT_SUCCESS;
}
Beispiel #12
0
void alsa_close(alsa_dev_t *alsa_dev)
{
    snd_pcm_drain(alsa_dev->snd_pcm);
    snd_pcm_close(alsa_dev->snd_pcm);

    free(alsa_dev);
}
Beispiel #13
0
static void
alsa_close( void )
{
	snd_pcm_drain( alsa.pcm );
	snd_pcm_close( alsa.pcm );
	alsa.pcm = NULL;
}
Beispiel #14
0
void close_alsa(struct pcm *pcm)
{
	struct alsa *alsa = (struct alsa *)(pcm->data);
	snd_pcm_drain(alsa->pcm);
	snd_pcm_close(alsa->pcm);
	free(alsa);
}
Beispiel #15
0
void
ags_devout_alsa_free(AgsDevout *devout)
{
  snd_pcm_drain(devout->out.alsa.handle);
  snd_pcm_close(devout->out.alsa.handle);
  devout->out.alsa.handle = NULL;
} 
Beispiel #16
0
bool SoundProcessor::RecordAndSend(int sfd,struct sockaddr_in fraddress){
  SetUpRecorder();//this needs to be here otherwise delay of sound will happen
  usleep(300000);
  while (true) {
    framecnt++;
    rc_m = snd_pcm_readi(handle_m, buffer_m, frames);
    if (rc_m == -EPIPE) {
      /* EPIPE means overrun */
      fprintf(stderr, "overrun occurred\n");
      snd_pcm_prepare(handle_m);
    } else if (rc_m < 0) {
      fprintf(stderr,
              "error from read: %s\n",
              snd_strerror(rc_m));
    } else if (rc_m != (int)frames) {
      fprintf(stderr, "short read, read %d frames\n", rc_m);
    }
    // printf("sending packet %d\n",framecnt);
    stringstream response; 
    response << "voice:"<<framecnt<<colon<<buffer_m;
    sendto(sfd,(void*)response.str().c_str(),response.str().length()+1,0,(SA *) &fraddress,sizeof(fraddress));
  }

  snd_pcm_drain(handle_m);
  snd_pcm_close(handle_m);
  free(buffer_m);
  return true;
}
Beispiel #17
0
bool SoundProcessor::Play(){
  while (true) {
    rc_s = read(0, buffer_s, size);
    if (rc_s == 0) {
      fprintf(stderr, "end of file on input\n");
      break;
    } else if (rc_s != size) {
      fprintf(stderr,
              "short read: read %d bytes\n", rc_s);
    }
    rc_s = snd_pcm_writei(handle_s, buffer_s, frames);
    if (rc_s == -EPIPE) {
      /* EPIPE means underrun */
      // fprintf(stderr, "underrun occurred\n");
      snd_pcm_prepare(handle_s);
    } else if (rc_s < 0) {
      fprintf(stderr,
              "error from writei: %s\n",
              snd_strerror(rc_s));
    }  else if (rc_s != (int)frames) {
      fprintf(stderr,
              "short write, write %d frames\n", rc_s);
    }
  }

  snd_pcm_drain(handle_s);
  snd_pcm_close(handle_s);
  free(buffer_s);
  return true;
}
CMAlsaAudioSink::~CMAlsaAudioSink()
{
    if (handle) {
        snd_pcm_drain(handle);
        snd_pcm_close(handle);
    }
}
Beispiel #19
0
void sound_close() {

	switch(sound_type) {
	case SOUND_NO:
	break;
	case SOUND_SDL:
	SDL_CloseAudio();
	break;
#ifdef D_SOUND_OSS
	case SOUND_OSS:
		close(audio_fd);
	break;
#endif
#ifdef D_SOUND_ALSA
	case SOUND_ALSA:
		snd_pcm_drain (_soundDevice);
		snd_pcm_close (_soundDevice);
	break;
#endif
#ifdef D_SOUND_PULSE
	case SOUND_PULSEAUDIO:
		pa_simple_free(pulse_s);
	break;
#endif
#ifdef GEKKO
	case SOUND_ASND:
		ASND_End();
	break;
#endif

	default:
	break;
	}
}
Beispiel #20
0
static bool_t alsa_can_read(snd_pcm_t *dev, int frames)
{
	snd_pcm_sframes_t avail;
	int err;

	avail = snd_pcm_avail_update(dev);
	ms_debug("*** %s %d %d", __FUNCTION__, (long)avail, frames);
	if (avail < 0) {
		ms_error("snd_pcm_avail_update: %s\n", snd_strerror(avail));	// most probably -EPIPE
		/* overrun occured, snd_pcm_state() would return SND_PCM_STATE_XRUN
		 FIXME: handle other error conditions*/
		ms_error("*** alsa_can_read fixup, trying to recover");
		snd_pcm_drain(dev); /* Ignore possible error, at least -EAGAIN.*/
		err = snd_pcm_recover(dev, avail, 0);
		if (err){ 
			ms_error("snd_pcm_recover() failed with err %d: %s", err, snd_strerror(err));
			return FALSE;
		}
		err = snd_pcm_start(dev);
		if (err){ 
			ms_error("snd_pcm_start() failed with err %d: %s\n", err, snd_strerror(err)); 
			return FALSE; 
		}
		ms_message("Recovery done\n");
	}
	return avail >= frames;
}
Beispiel #21
0
void AlsaRenderer::Close()
{
    if (m_PcmHandle != nullptr) {
        snd_pcm_drain(m_PcmHandle);
        snd_pcm_close(m_PcmHandle);
        m_PcmHandle = nullptr;
    }
}
Beispiel #22
0
void pcm_player_free(pcm_player *player)
{
    snd_pcm_drain(player->pcm_handle);
    snd_pcm_close(player->pcm_handle);

    free(player->buffer);
    free(player);
}
Beispiel #23
0
void myterm() {
  mylog(LOG_INFO, "Stopping due to interrupt/term.\n");
  snd_pcm_drain(handle);
  snd_pcm_close(handle);
  if (readfd > 0) close(readfd);
  if (writefd > 0) close(writefd);
  exit(EXIT_SUCCESS);
}
Beispiel #24
0
void CAESinkALSA::Drain()
{
  if (!m_pcm)
    return;

  snd_pcm_drain(m_pcm);
  snd_pcm_prepare(m_pcm);
}
Beispiel #25
0
void audio_deinit(void)
{
    syslog(LOG_INFO,"ALSA: Deinitiating PCM handles\n");
    if (alsa_handle) {
        snd_pcm_drain(alsa_handle);
        snd_pcm_close(alsa_handle);
    }
}
Beispiel #26
0
    bool record(asr_usercommand::UserCommand::Request  &req,
         asr_usercommand::UserCommand::Response &res)
{
     char *filename;
     filename="/home/turtlebot2/asr_ws/src/asr_usercommand/wav/recordfile.wav";
      char *devicename = "default"; 
        int fd; 
        WAVContainer_t wav; 
        SNDPCMContainer_t record; 
         
        memset(&record, 0x0, sizeof(record)); 
     
        remove(filename); 
        if ((fd = open(filename, O_WRONLY | O_CREAT, 0644)) == -1) { 
            fprintf(stderr, "Error open: [%s]/n", filename); 
            return false; 
        } 
     
        if (snd_output_stdio_attach(&record.log, stderr, 0) < 0) { 
            fprintf(stderr, "Error snd_output_stdio_attach/n"); 
            goto Err; 
        } 
     
        if (snd_pcm_open(&record.handle, devicename, SND_PCM_STREAM_CAPTURE, 0) < 0) { 
            fprintf(stderr, "Error snd_pcm_open [ %s]/n", devicename); 
            goto Err; 
        } 
     
        if (SNDWAV_PrepareWAVParams(&wav) < 0) { 
            fprintf(stderr, "Error SNDWAV_PrepareWAVParams/n"); 
            goto Err; 
        } 
     
        if (SNDWAV_SetParams(&record, &wav) < 0) { 
            fprintf(stderr, "Error set_snd_pcm_params/n"); 
            goto Err; 
        } 
        snd_pcm_dump(record.handle, record.log); 
     
        SNDWAV_Record(&record, &wav, fd); 
     
        snd_pcm_drain(record.handle); 
     
        close(fd); 
        free(record.data_buf); 
        snd_output_close(record.log); 
        snd_pcm_close(record.handle); 
        return true; 
     
    Err: 
        close(fd); 
        remove(filename); 
        if (record.data_buf) free(record.data_buf); 
        if (record.log) snd_output_close(record.log); 
        if (record.handle) snd_pcm_close(record.handle); 
        return false; 
}
Beispiel #27
0
static void audio_renderer_cleanup() {
  if (decoder != NULL)
    opus_decoder_destroy(decoder);

  if (handle != NULL) {
    snd_pcm_drain(handle);
    snd_pcm_close(handle);
  }
}
Beispiel #28
0
void audio_capture_close()
{
	printf("- Release memory\n");
	free(buffer);

	printf("- Close Soundcard\n");
	snd_pcm_drain(handle);
	snd_pcm_close(handle);
}
Beispiel #29
0
void *speaker_thread(void* ptr){
  audiobuffer* buf = ((spk_pcm_package*)ptr)->buffer; //cast pointer, get buffer struct
  snd_pcm_t* speaker_handle = ((spk_pcm_package*)ptr)->pcm_handle; //cast pointer, get device pointer
  free(ptr); //free message memory
    
  snd_pcm_nonblock(speaker_handle, SND_PCM_NONBLOCK); //set in nonblocking mode
    
  char started = 0;  //track when to start reading data
  while(!global_kill && !speaker_kill_flag) { //loop until program stops us
    //wait until adequate buffer is achieved and can be written
    if((!started && BUFFER_SIZE(*buf) < (MIN_BUFFER)) \
        || BUFFER_EMPTY(*buf) \
        || !snd_pcm_avail_update(speaker_handle)) {
      //printf("Speaker Waiting\n");
      usleep(PERIOD_UTIME*2); //wait to reduce CPU usage
      continue;     //don't start yet
    } else {
      if(!started) snd_pcm_prepare(speaker_handle); //reset speaker
      started = 1; //indicate that we've startd
    }
    
    //write data to speaker buffer, check responses
    int write_count = MIN(BUFFER_SIZE(*buf), snd_pcm_avail_update(speaker_handle)/(buf->period));
    
#ifdef DEBUG_MODE
    printf("Writing %d packets to speaker\n", write_count);
#endif

    //loop over avaliable buffer entries
    while(write_count-- > 0 && started){
      /* Note: This call performs a syscall to copy data to kernel space 
          so it would be better to write multiple entries in one operation, 
          but using the audiobuffer without the abstraction was something 
          I didn't want to do at the time of writing. */
      int rc = snd_pcm_writei(speaker_handle, GET_QUEUE_HEAD(*buf), buf->period);      
      INC_QUEUE_HEAD(*buf);
      if (rc == -EPIPE){ //Catch underruns (not enough data)
        fprintf(stderr, "underrun occurred\n");
        started = 0;  //stop and wait for buffer to buildup
      } else if (rc < 0) fprintf(stderr, "error from writei: %s\n", snd_strerror(rc)); //other errors
      else if (rc != (int)buf->period) fprintf(stderr, "short write, write %d frames\n", rc);
      //else fprintf(stderr, "audio written correctly\n");
      //snd_pcm_wait(speaker_handle, 1000); //wait for IO to be ready
    }
    
#ifdef DEBUG_MODE
    printf("%d unwritten\n", write_count);
#endif    
  }
  
  // notify kernel to empty/close the speakers
  snd_pcm_drain(speaker_handle);  //finish transferring the audio
  snd_pcm_close(speaker_handle);  //close the device
  printf("Audio Controller: Speaker Thread shutdown\n");
  
  pthread_exit(NULL); //exit thread safetly
}
void CAESinkALSA::Drain()
{
  if (!m_pcm)
    return;

  snd_pcm_nonblock(m_pcm, 0);
  snd_pcm_drain(m_pcm);
  snd_pcm_nonblock(m_pcm, 1);
}