bool ManglerPulse::write(uint8_t *sample, uint32_t length, int channels) {/*{{{*/ if (!pulse_stream) { return false; } if (pa_simple_write(pulse_stream, sample, length, &pulse_error) < 0) { fprintf(stderr, "pulse: pa_simple_write() failed: %s\n", pa_strerror(pulse_error)); return false; } return true; }/*}}}*/
/* Play a buffer */ void SoundPlayer::playBuffer(unsigned char * b, size_t s) { if (connection) { int error; // Play buffer if (pa_simple_write(connection, b, s, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); return; } } }
static gavl_sink_status_t write_func_pulse(void * p, gavl_audio_frame_t * f) { bg_pa_t * priv; int error; priv = p; pa_simple_write(priv->pa, f->samples.u_8, priv->block_align * f->valid_samples, &error); return GAVL_SINK_OK; }
short *Audio_Pulse::write () { if (_audioHandle == NULL) { _errorString = "ERROR: Device not open."; return NULL; } if (pa_simple_write(_audioHandle, _sampleBuffer, _settings.bufSize * 2, NULL) < 0) { _errorString = "Error writing to PA."; } return _sampleBuffer; }
static int pulse_output(struct iaxc_audio_driver *d, void *samples, int nSamples) { int error; pa_simple *p = (pa_simple*)(d->priv2); if(pa_simple_write(p, samples, nSamples*sizeof(short), &error) <0) { fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); } // this is just a bogus return return 0; }
qint64 OutputPulseAudio::writeAudio(unsigned char *data, qint64 maxSize) { int error; if (!m_connection) return -1; int i = 0; if ((i = pa_simple_write(m_connection, data, maxSize, &error)) < 0) { qWarning("OutputPulseAudio: pa_simple_write() failed: %s", pa_strerror(error)); return -1; } return maxSize; }
int main(int argc, char *argv[]) { FILE *fp; int n; int16_t buf[BB], out[BB]; int error; static const pa_sample_spec ss = { .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 1}; pa_simple *s = NULL; if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) { fprintf(stderr, __FILE__ ": pa_simple_new() failed: %s\n", pa_strerror(error)); goto finish; } fp = fopen("out.wav", "rb"); if (!fp) exit(-1); fread(buf, 1, 44, fp); n = fread(buf, 2, BB, fp); while (!feof(fp)) { highpass(buf, out, BB, 1.0f, 10); memcpy(buf, out, BB * 2); lowpass(buf, out, BB, 1.0f / 44100.0f, 0.00002); if (pa_simple_write(s, out, (size_t)n * 2, &error) < 0) { fprintf(stderr, __FILE__ ": pa_simple_write() failed: %s\n", pa_strerror(error)); goto finish; } n = fread(buf, 2, BB, fp); } if (pa_simple_drain(s, &error) < 0) { fprintf(stderr, __FILE__ ": pa_simple_drain() failed: %s\n", pa_strerror(error)); goto finish; } finish: if (s) pa_simple_free(s); return 0; }
UINT8 Pulse_WriteData(void* drvObj, UINT32 dataSize, void* data) { DRV_PULSE* drv = (DRV_PULSE*)drvObj; int retVal; if (dataSize > drv->bufSize) return AERR_TOO_MUCH_DATA; retVal = pa_simple_write(drv->hPulse, data, (size_t) dataSize, NULL); if (retVal > 0) return 0xFF; return AERR_OK; }
void FestivalSynthesizer::synthSpeech ( std::string text ) { if ( pa_simple_ ) { EST_Wave wave; festival_text_to_wave( text.c_str(), wave ); int error; pa_simple_write( pa_simple_, &(wave.a(0)), wave.length()*2, &error ); } else { ROS_ERROR( "Cannot snyth speec. Initilization failed." ); } }
static void pulse_connect(void) { static const pa_sample_spec ss = {.format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 2}; pa_dev = pa_simple_new(pulse_options.server, pulse_options.service_name, PA_STREAM_PLAYBACK, pulse_options.sink, "Shairport Stream", &ss, NULL, NULL, &pa_error); if (!pa_dev) die("Could not connect to pulseaudio server: %s", pa_strerror(pa_error)); } static void deinit(void) { if (pa_dev) pa_simple_free(pa_dev); pa_dev = NULL; } static void start(int sample_rate) { if (sample_rate != 44100) die("unexpected sample rate!"); } static void play(short buf[], int samples) { if (pa_simple_write(pa_dev, (char *)buf, (size_t)samples * 4, &pa_error) < 0) { fprintf(stderr, __FILE__ ": pa_simple_write() failed: %s\n", pa_strerror(pa_error)); if (pa_error == PA_ERR_CONNECTIONTERMINATED) { fprintf(stderr, __FILE__ ": reconnecting."); deinit(); pulse_connect(); } } } static void stop(void) { if (pa_simple_drain(pa_dev, &pa_error) < 0) fprintf(stderr, __FILE__ ": pa_simple_drain() failed: %s\n", pa_strerror(pa_error)); } audio_output audio_pulse = {.name = "pulse", .help = &help, .init = &init, .deinit = &deinit, .start = &start, .stop = &stop, .flush = NULL, .delay = NULL, .play = &play, .volume = NULL, .parameters = NULL, .mute = NULL};
void read_thread(void* arg) { pa_sample_spec capture_profile; capture_profile.channels = 1; capture_profile.rate = 44100; capture_profile.format = PA_SAMPLE_S16LE; int error; int m = *(int*)arg; pa_simple *paPlayHandle = pa_simple_new(NULL,"soundCard", PA_STREAM_PLAYBACK, NULL, "playback", &capture_profile, NULL, NULL, &error); for (int i = 0; i < m; i++) { short buffer[2048]; if(server.recv_n(buffer,2048) > 0) pa_simple_write(paPlayHandle, buffer, 2048, &error); } }
void WaveOutPulseAudio::DoWriteChunk(char *pChunkBuffer, int pChunkSize) { int tRes; if (!mPlaybackStopped) { #ifdef WOPUA_DEBUG_TIMING int64_t tTime = Time::GetTimeStamp(); #endif if (pa_simple_write(mOutputStream, pChunkBuffer, pChunkSize, &tRes) < 0) { LOG(LOG_ERROR, "Couldn't write audio chunk of %d bytes to output stream because %s(%d)", pChunkSize, pa_strerror(tRes), tRes); } #ifdef WOPUA_DEBUG_TIMING LOG(LOG_VERBOSE, "PulseAudio-WRITE took %lld ms", (Time::GetTimeStamp() - tTime) / 1000); #endif } }
bool PulseAudioPCMOutputDriver::WriteBufferToDevice(const unsigned char* buffer, size_t size) { InitDevice(); if(m_driver) { int error(-1); if(pa_simple_write(m_driver, buffer, size, &error) < 0) { AWS_LOGSTREAM_ERROR(CLASS_NAME, " error writing buffer to output device " << pa_strerror(error)); return false; } return true; } return false; }
static void bufdump(struct xmp_context *ctx, int i) { int j, error; void *b; b = xmp_smix_buffer(ctx); do { if ((j = pa_simple_write(s, b, i, &error)) > 0) { i -= j; b += j; } else break; } while (i); if (j < 0) { fprintf(stderr, "pulseaudio error: %s\n", pa_strerror(error)); } }
static ssize_t pulse_write (sw_handle * handle, const float * buf, size_t count) { struct pa_simple * pa ; int error; size_t byte_count; if ((pa = (struct pa_simple *)handle->custom_data) == NULL) return 0; byte_count = count * sizeof (float); if (pa_simple_write(pa, buf, byte_count, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); return 0; } return 1; }
int main(int argc, char*argv[]) { /* The sample type to use */ static const pa_sample_spec ss = { .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 2 }; pa_simple *inStream = NULL; pa_simple *outStream = NULL; int ret = 1; int error; /* Create the recording stream */ if (!(inStream = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) { fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto finish; } /* Create the playback stream */ if (!(outStream = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) { fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); goto finish; } for (;;) { uint8_t buf[BUFSIZE]; /* Record some data ... */ if (pa_simple_read(inStream, buf, sizeof(buf), &error) < 0) { fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); goto finish; } if (pa_simple_write(outStream, buf, sizeof(buf), &error) < 0) { fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); goto finish; } } ret = 0; finish: if (inStream) pa_simple_free(inStream); if (outStream) pa_simple_free(outStream); return ret; }
int softrock_write(float* left_samples,float* right_samples) { int rc; int i; int error; float audio_buffer[SAMPLES_PER_BUFFER*2]; rc=0; if(usb2sdr){ } else{ // interleave samples for(i=0;i<SAMPLES_PER_BUFFER;i++) { audio_buffer[i*2]=right_samples[i]; audio_buffer[(i*2)+1]=left_samples[i]; } rc = pa_simple_write(playback_stream, audio_buffer, sizeof(audio_buffer),&error); if (rc < 0) {if (softrock_get_verbose()) fprintf(stderr,"error writing audio_buffer %s (rc=%d)\n", pa_strerror(error), rc);} } return rc; }
static void pulse_thread(void *context) { #ifdef __linux__ prctl(PR_SET_NAME, "deadbeef-pulse", 0, 0, 0, 0); #endif while (!pulse_terminate) { if (state != OUTPUT_STATE_PLAYING || !deadbeef->streamer_ok_to_read (-1)) { usleep(10000); continue; } int sample_size = plugin.fmt.channels * (plugin.fmt.bps / 8); int bs = buffer_size; int mod = bs % sample_size; if (mod > 0) { bs -= mod; } char buf[bs]; pulse_callback (buf, sizeof (buf)); int error; deadbeef->mutex_lock(mutex); int res = pa_simple_write(s, buf, sizeof (buf), &error); deadbeef->mutex_unlock(mutex); if (res < 0) { fprintf(stderr, "pulse: failed to write buffer\n"); usleep(10000); } } pulse_terminate = 0; trace ("pulse_thread finished\n"); }
int main() { pa_sample_spec ss; ss.format = PA_SAMPLE_S16LE; ss.channels = 1; ss.rate = 96000; pa_buffer_attr ba; ba.maxlength = (uint32_t)-1; ba.tlength = 0; ba.prebuf = (uint32_t)-1; ba.minreq = (uint32_t)-1; ba.fragsize = (uint32_t)-1; pa_simple* s_rec = pa_simple_new(NULL, "echo", PA_STREAM_RECORD, NULL, "Record", &ss, NULL, &ba, NULL); pa_simple* s_play = pa_simple_new(NULL, "echo", PA_STREAM_PLAYBACK, NULL, "Echo", &ss, NULL, &ba, NULL); if (!s_rec) { fprintf(stderr, "Pulse rec failed.\n"); return 1; } if (!s_play) { fprintf(stderr, "Pulse play failed.\n"); return 1; } char buffer[sizeof(short) * 128]; for (;;) { pa_simple_read(s_rec, buffer, sizeof(buffer), NULL); pa_simple_write(s_play, buffer, sizeof(buffer), NULL); } return 0; }
void AudioDriverPulseAudio::thread_func(void* p_udata) { print_line("thread"); AudioDriverPulseAudio* ad = (AudioDriverPulseAudio*)p_udata; while (!ad->exit_thread) { if (!ad->active) { for (unsigned int i=0; i < ad->buffer_size * ad->channels; i++) { ad->samples_out[i] = 0; } } else { ad->lock(); ad->audio_server_process(ad->buffer_size, ad->samples_in); ad->unlock(); for (unsigned int i=0; i < ad->buffer_size * ad->channels;i ++) { ad->samples_out[i] = ad->samples_in[i] >> 16; } } // pa_simple_write always consumes the entire buffer int error_code; int byte_size = ad->buffer_size * sizeof(int16_t) * ad->channels; if (pa_simple_write(ad->pulse, ad->samples_out, byte_size, &error_code) < 0) { // can't recover here fprintf(stderr, "PulseAudio failed and can't recover: %s\n", pa_strerror(error_code)); ad->active = false; ad->exit_thread = true; break; } } ad->thread_exited = true; }
static void* pulse_thread(void* context) { sem_t* init = (sem_t*)context; pa_sample_spec ss; #if TINYAUDIO_FLOAT_BUS ss.format = PA_SAMPLE_FLOAT32LE; #else ss.format = PA_SAMPLE_S16LE; #endif ss.channels = 2; ss.rate = g_sample_rate; int err; g_pulse = pa_simple_new(NULL, g_appname, PA_STREAM_PLAYBACK, NULL, g_appname, &ss, NULL, NULL, &err); if (!g_pulse) { snprintf(g_lasterror, c_nlasterror, "failed to connect to pulse server: %d", err); } sem_post(init); if (!g_pulse) return 0; pa_simple* s = g_pulse; sample_type samples[c_nsamples*2]; g_running = true; while (g_running) { g_callback(samples, c_nsamples); if (0 > pa_simple_write(s, samples, sizeof(samples), NULL)) break; } pa_simple_flush(s, NULL); pa_simple_free(s); g_pulse = 0; return 0; }
static PyObject *simple_write(simple *self, PyObject *args) { int ret = 0; Py_buffer buffer; if (!self->s) { PyErr_SetString(PyExc_RuntimeError, "Not connected to pulseaudio server"); return Py_BuildValue("i", -1); } if (!PyArg_ParseTuple(args, "y*", &buffer)) return Py_BuildValue("i", -2); ret = pa_simple_write(self->s, buffer.buf, buffer.len, &self->error); if (ret < 0) { PyErr_SetString(PyExc_RuntimeError, pa_strerror(self->error)); return Py_BuildValue("i", -3); } PyBuffer_Release(&buffer); return Py_BuildValue("n", buffer.len); }
int main(int argc, char **argv) { if (argc != 1 && argc != 2) { fprintf(stderr, "usage: %s [sink_name] < input_file\n", argv[0]); exit(1); } const char *server_name = NULL; const char *client_name = "example play simple"; const char *sink_name = NULL; const char *stream_name = "example stream"; if (argc > 1) { sink_name = argv[1]; } pa_sample_spec sample_spec = {}; sample_spec.format = PA_SAMPLE_FLOAT32LE; sample_spec.rate = 44100; sample_spec.channels = 2; int error = 0; pa_simple *simple = pa_simple_new( server_name, client_name, PA_STREAM_PLAYBACK, sink_name, stream_name, &sample_spec, NULL, NULL, &error); if (simple == NULL) { fprintf(stderr, "pa_simple_new: %s\n", pa_strerror(error)); exit(1); } const pa_usec_t start_time = pa_rtclock_now(); uint64_t n_bytes = 0; for (;;) { char buf[1024]; ssize_t sz = read(STDIN_FILENO, buf, sizeof(buf)); if (sz == -1) { fprintf(stderr, "read: %s\n", strerror(errno)); exit(1); } if (sz == 0) { break; } print_info(simple, &sample_spec, start_time, n_bytes); if (pa_simple_write(simple, buf, (size_t)sz, &error) != 0) { fprintf(stderr, "pa_simple_write: %s\n", pa_strerror(error)); exit(1); } n_bytes += (uint64_t)sz; } /* wait until all samples are sent and played on server */ if (pa_simple_drain(simple, &error) != 0) { fprintf(stderr, "pa_simple_drain: %s\n", pa_strerror(error)); exit(1); } print_info(simple, &sample_spec, start_time, n_bytes); pa_simple_free(simple); return 0; }
int main(int argc, char*argv[]) { /* The Sample format to use */ static const pa_sample_spec ss = { .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 2 }; pa_simple* dev_out = 0; pa_simple* dev_in = 0; int ret = 1; int error; /* Create a new playback stream */ if (!(dev_out = pa_simple_new(NULL, "Noise Remover", PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) { fprintf(stderr, __FILE__": pa_simple_new() failed: %dev_out\n", pa_strerror(error)); goto finish; } if (!(dev_in = pa_simple_new(NULL, "Noise Remover", PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) { fprintf(stderr, __FILE__": pa_simple_new() failed: %dev_out\n", pa_strerror(error)); goto finish; } { int i; float f; SpeexPreprocessState* pp = speex_preprocess_state_init(BUFSIZE, ss.rate); i = 1; speex_preprocess_ctl(pp, SPEEX_PREPROCESS_SET_DENOISE, &i); i = 1; speex_preprocess_ctl(pp, SPEEX_PREPROCESS_SET_AGC, &i); f = 8000; speex_preprocess_ctl(pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &f); i = 1; speex_preprocess_ctl(pp, SPEEX_PREPROCESS_SET_DEREVERB, &i); f = 0.04; speex_preprocess_ctl(pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &f); f = 0.03; speex_preprocess_ctl(pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &f); double lowest_rms = 99999999999999; int silence_count = 0; for (;;) { int16_t buf[BUFSIZE]; /* Read some data ... */ if (pa_simple_read(dev_in, buf, sizeof(buf), &error) < 0) { fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); goto finish; } /* ... Use speex to de-noise ... */ double total = 0; for(int n = 0; n < sizeof(buf); n++) total += buf[n] * buf[n]; double rms = std::sqrt(total / sizeof(buf)); if(rms < lowest_rms) lowest_rms = rms; if((rms - lowest_rms) < 50) // this value will probably need adjusting for you silence_count = 0; else if(silence_count < 10) silence_count++; if(silence_count == 10) speex_preprocess_run(pp, buf); else continue; // don't write it out... /* ... and play it */ if (pa_simple_write(dev_out, buf, sizeof(buf), &error) < 0) { fprintf(stderr, __FILE__": pa_simple_write() failed: %dev_out\n", pa_strerror(error)); goto finish; } } /* Make sure that every single sample was played */ if (pa_simple_drain(dev_out, &error) < 0) { fprintf(stderr, __FILE__": pa_simple_drain() failed: %dev_out\n", pa_strerror(error)); goto finish; } } ret = 0; finish: if (dev_out) pa_simple_free(dev_out); if (dev_in) pa_simple_free(dev_in); return ret; }
void sound_play() { #if defined(D_SOUND_OSS) || defined(D_SOUND_ALSA) || defined(D_SOUND_PULSE) static int retval,retval2; #endif ordenador.current_buffer=sound[0]; //remove_dc(ordenador.current_buffer,ordenador.buffer_len); switch(sound_type) { case SOUND_NO: // no sound usleep(75000); // wait 1/20 second return; break; case SOUND_SDL: // SDL if (!started_sound_sdl) { SDL_PauseAudio(0); ordenador.current_buffer = sound[0]; buffer0_occupied=1; started_sound_sdl = 1; buffer_reading=0; } //Double buffer while ((buffer0_occupied)&&(buffer1_occupied)){usleep(1000);}; //Wait for one buffer to be free if (!buffer0_occupied) //Buffer 0 is now free {buffer0_occupied=1; ordenador.current_buffer = sound[0]; } else //Buffer 1 is now free {buffer1_occupied=1; ordenador.current_buffer = sound[1]; } return; break; #ifdef D_SOUND_OSS case SOUND_OSS: // OSS retval=write(audio_fd,ordenador.current_buffer,ordenador.buffer_len*ordenador.increment); return; break; #endif #ifdef D_SOUND_ALSA case SOUND_ALSA: // ALSA if (started_sound==0) { snd_pcm_prepare (_soundDevice); started_sound=1; } retval=snd_pcm_writei(_soundDevice,sound[0],ordenador.buffer_len); if (retval<0) { retval=snd_pcm_prepare(_soundDevice); } return; break; #endif #ifdef D_SOUND_PULSE case SOUND_PULSEAUDIO: // PulseAudio // Remove the DC component to avoid losing the sound when multiplexing with other sources sound_remove_dc(ordenador.current_buffer,ordenador.buffer_len); retval=pa_simple_write(pulse_s,sound[0],ordenador.buffer_len,&retval2); return; break; #endif #ifdef GEKKO case SOUND_ASND: // ASND if (!started_sound_asnd) { ASND_SetVoice(1,VOICE_STEREO_16BIT_BE,ordenador.freq,0,sound[0],ordenador.buffer_len*ordenador.increment, 255, 255, callback); started_sound_asnd = 1; } //Double buffer while (!ASND_TestVoiceBufferReady(1)){}; //Wait for one buffer to be free if (!ASND_TestPointer (1, sound[0])) {ASND_AddVoice(1,sound[0],ordenador.buffer_len*ordenador.increment); ordenador.current_buffer = sound[0]; } else {ASND_AddVoice(1,sound[1],ordenador.buffer_len*ordenador.increment); ordenador.current_buffer = sound[1]; } return; break; #endif default: break; } }
static ssize_t regparm pulse_write(const void *buf, size_t count) { return pa_simple_write(pulse_handle, buf, count, NULL); }
void audio_trigger(context_t *context) { int input_fd = -1; static const pa_sample_spec ss = { .format = PA_SAMPLE_S16LE, .rate = 44100, .channels = 1 }; int error; daemon_log(LOG_INFO, "Trigger %s", context->shush_filename); if (context->points_threshold < 0) { daemon_log(LOG_INFO, "Threshold below 0, not triggering sound"); } daemon_log(LOG_INFO, "Opening %s for output", context->output_device ?: "default sink"); pa_simple *pa_output = pa_simple_new( NULL, "shusherd", PA_STREAM_PLAYBACK, context->output_device, "playback", &ss, NULL, NULL, &error); if (!pa_output) { daemon_log(LOG_ERR, "pa_simple_new failed: %s", pa_strerror(error)); goto finish; } input_fd = open(context->shush_filename, O_RDONLY); if (input_fd < 0) { daemon_log(LOG_ERR, "Error reading %s: %s", context->shush_filename, strerror(errno)); goto finish; } for (;;) { uint8_t buf[BUFSIZE]; ssize_t r; r = read(input_fd, buf, sizeof(buf)); if (r < 0) { daemon_log(LOG_ERR, "read() failed: %s", strerror(errno)); goto finish; } if (r == 0) goto finish; if (pa_simple_write(pa_output, buf, (size_t) r, &error) < 0) { daemon_log(LOG_ERR, "pa_simple_write() failed: %s", pa_strerror(errno)); goto finish; } } if (pa_simple_drain(pa_output, &error) < 0) { daemon_log(LOG_ERR, "pa_simple_drain() failed: %s", pa_strerror(errno)); goto finish; } finish: if (input_fd > 0) close(input_fd); if (pa_output) pa_simple_free(pa_output); } void *audio_loop(void *context_p) { context_t *context = (context_t *)context_p; time_t t = time(NULL); double loudness; double points = 0.0; int error; //pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); daemon_log(LOG_INFO, "Starting listening..."); const short buf[BUFSIZE/2]; int bytes = 0; int trigger = 0; int latest_trigger_time = 0; while (context->enable_processing) { if ((bytes = pa_simple_read(context->pa_input, (void *)buf, sizeof(buf), &error)) < 0) { daemon_log(LOG_ERR, "pa_simple_read failed: %s", pa_strerror(error)); assert(0); } if ((time(NULL) - latest_trigger_time) < context->cooldown) { continue; } ebur128_add_frames_short(context->ebur128_state, buf, sizeof(buf)/2); if ((time(NULL) - t) > SAMPLE_TIME) { t = time(NULL); ebur128_loudness_shortterm(context->ebur128_state, &loudness); points += 100 - fabs(loudness); daemon_log(LOG_INFO, "Points: %f (%d) (%f)", points, context->points_threshold, loudness); if (points > context->points_threshold) { trigger = 1; } else { points *= context->decay; } } if (trigger) { latest_trigger_time = time(NULL); audio_trigger(context); points = 0; trigger = 0; daemon_log(LOG_INFO, "Waiting %d seconds before listening again", context->cooldown); } } daemon_log(LOG_INFO, "Stopped listening..."); return 0; }
static gpointer player_av_audio_loop (PlayerAV *self) { pa_simple *s; pa_sample_spec ss; ss.format = PA_SAMPLE_S16LE; ss.rate = self->priv->actx->sample_rate; ss.channels = self->priv->actx->channels; s = pa_simple_new (NULL, "GMediaMP", PA_STREAM_PLAYBACK, NULL, "Music", &ss, NULL, NULL, NULL); int64_t pts; gint len, lcv; short *abuffer = (short*) av_malloc (AVCODEC_MAX_AUDIO_FRAME_SIZE * self->priv->actx->channels * sizeof (uint8_t)); double atime, ctime; while ((len = player_av_get_audio_frame (self, abuffer, &pts)) > 0) { if (pts != AV_NOPTS_VALUE) { atime = pts * av_q2d (self->priv->fctx->streams[self->priv->astream]->time_base); ctime = (av_gettime () - self->priv->start_time) / 1000000.0; self->priv->start_time += (1000000 * (ctime - atime)); } if (len > 0) { gint i; for (i = 0; i < len / sizeof (short); i++) { gdouble val = self->priv->volume * abuffer[i]; if (val > 32767) { abuffer[i] = 32767; } else if (val < -32768) { abuffer[i] = -32768; } else { abuffer[i] = val; } } pa_simple_write (s, abuffer, len, NULL); } if (atime >= self->priv->fctx->duration) { break; } if (self->priv->state != PLAYER_STATE_PLAYING) { break; } } if (self->priv->state == PLAYER_STATE_PLAYING) { self->priv->state = PLAYER_STATE_STOPPED; _player_emit_eos (PLAYER (self)); } av_free (abuffer); pa_simple_flush (s, NULL); pa_simple_free (s); }
int ao_plugin_play(ao_device *device, const char* output_samples, uint_32 num_bytes) { assert(device && device->internal); ao_pulse_internal *internal = (ao_pulse_internal *) device->internal; return pa_simple_write(internal->simple, output_samples, num_bytes, NULL) >= 0; }
int main(int argc, char **argv) { modulation mod = AM; int genwav = 0; int test = 0; int chat = 0; static struct option long_options[] = { {"modulation", required_argument, 0, 'm'}, {"genwav", no_argument, 0, 'g'}, {"test", no_argument, 0, 't'}, {"chat", no_argument, 0, 'c'}, {0, 0, 0, 0} }; int c; int option_index = 0; do { c = getopt_long (argc, argv, "m:gt", long_options, &option_index); switch (c) { case 0: break; case 'm': if (strcmp(optarg, "AM") == 0) { mod = AM; break; } if (strcmp(optarg, "FM") == 0) { mod = FM; break; } fprintf(stderr, "Error: Invalid modulation\n"); return 1; case 't': test = 1; break; case 'g': genwav = 1; break; case 'c': chat = 1; break; case -1: break; default: return 1; break; } } while (c != -1); if (!(test || chat || genwav)) { fprintf(stderr, "Error: You must suply an action\n"); return 1; } int *vbuf; frame *msg; int i; if (genwav || test) { vbuf = get_fdata(PING_STR, strlen(PING_STR), mod); if (chk_wav_data(vbuf, &msg, mod)) printf("Info: PCM data is ok.\n"); else { fprintf(stderr, "Error: PCM data can not be read\n"); for (i = 0; i < FRAME_SIZE; i++) fprintf(stderr, "%hhx", ((char *) msg)[i]); fprintf(stderr, "\n"); } } if (genwav) { FILE *am = fopen("am.wav", "w"); if (am == NULL) { printf("could not open files\n"); exit(1); } write_wav(am, FRAME_BUFFER, vbuf); printf("Info: WAV ready\n"); } if (!chat) return 0; char *mg = (char *) malloc(65536); int j; pa_simple *ch = get_ch(); pa_simple *pl = get_pl(); int err = 0; int *buf = (int *) malloc(sizeof(int) * SAMPLE_BUFFER); sbf = (int *) calloc(sizeof(int), FRAME_BUFFER); int *pbf; fd_set set; int rv; struct timeval tv; printf("Info: Starting soundwave chat.\n"); while (1) { if (pa_simple_read(ch, buf, sizeof(int) * SAMPLE_BUFFER, &err)) fprintf(stderr, "Error: %s\n", pa_strerror(err)); //filter_frq(buf, SAMPLE_BUFFER); mmpush(sbf, FRAME_BUFFER * sizeof(int), buf, SAMPLE_BUFFER * sizeof(int)); msg = get_msg(sbf, mod); if (chk_frm(msg)) { printf("M: %.*s", 61, msg->data); flushfb(); } free(msg); FD_ZERO(&set); FD_SET(STDIN_FILENO, &set); tv.tv_sec = 0; tv.tv_usec = 0; rv = select(STDIN_FILENO + 1, &set, NULL, NULL, &tv); if ((rv != 0) && (rv != -1)) { rv = read(STDIN_FILENO, mg, 65536); for (i = 0; i < rv; i += 61) { j = ((i + 61) <= rv) ? 61 : (rv - i); pbf = get_fdata(mg + i, j, mod); if (pa_simple_write(pl, pbf, FRAME_BUFFER * sizeof(int), &err)) printf("error: %s\n", pa_strerror(err)); free(pbf); } } fflush(stdin); fflush(stdout); fflush(stderr); } printf("\n"); pa_simple_free(ch); pa_simple_free(pl); return 0; }