123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include "precompile.h"
- #include "audiodsp.h"
- #include "audiocontext.h"
- #include "audiolog.h"
- #include <speex/speex_echo.h>
- #include <speex/speex_preprocess.h>
- static apr_status_t read_frame(void *self, audioframe_t *frame)
- {
- audiodsp_t *dsp = CONTAINING_RECORD(self, audiodsp_t, base);
- apr_status_t status;
- if (dsp->read_preprocess) {
- status = dsp->base.upstream->vtbl->read_frame(dsp->base.upstream, frame);
- if (status != APR_SUCCESS)
- return status;
- speex_preprocess_run(dsp->read_preprocess, (spx_int16_t*)&frame->buffer[0]);
- } else {
- status = dsp->base.upstream->vtbl->read_frame(dsp->base.upstream, frame);
- }
-
- return status;
- }
- static apr_status_t write_frame(void *self, const audioframe_t *frame)
- {
- audiodsp_t *dsp = CONTAINING_RECORD(self, audiodsp_t, base);
- apr_status_t status;
- if (dsp->write_preprocess) {
- dsp->tmp_write_frame.size = SUGGEST_FRAME_SIZE;
- audioframe_copy(&dsp->tmp_write_frame, frame);
- speex_preprocess_run(dsp->write_preprocess, (spx_int16_t*)dsp->tmp_write_frame.buffer);
- status = dsp->base.upstream->vtbl->write_frame(dsp->base.upstream, &dsp->tmp_write_frame);
- } else {
- status = dsp->base.upstream->vtbl->write_frame(dsp->base.upstream, frame);
- }
- return status;
- }
- static audiostream_vtbl_t g_stream_vtbl = {
- &read_frame,
- &write_frame,
- };
- apr_status_t audiodsp_create(apr_pool_t *pool,
- audioengine_t *engine,
- int read_opt,
- int write_opt,
- int clock,
- audiodsp_t **p_dsp)
- {
- audiodsp_t *dsp;
- dsp = apr_palloc(pool, sizeof(audiodsp_t));
- audiostream_init(engine, &g_stream_vtbl, &dsp->base);
- dsp->base.direction = STREAM_DIR_BOTH;
- dsp->clock = clock;
- dsp->read_option = read_opt;
- dsp->write_option = write_opt;
- dsp->frame_samples = FRAME_TIME * clock / 1000;
- dsp->tmp_read_frame.size = SUGGEST_FRAME_SIZE;
- dsp->tmp_read_frame.buffer = apr_palloc(pool, SUGGEST_FRAME_SIZE);
- dsp->tmp_write_frame.size = SUGGEST_FRAME_SIZE;
- dsp->tmp_write_frame.buffer = apr_palloc(pool, SUGGEST_FRAME_SIZE);
- dsp->read_preprocess = NULL;
- dsp->write_preprocess = NULL;
- if (read_opt) {
- int disabled = 0;
- int enabled = 1;
- dsp->read_preprocess = speex_preprocess_state_init(dsp->frame_samples, clock);
- speex_preprocess_ctl(dsp->read_preprocess, SPEEX_PREPROCESS_SET_AGC,
- read_opt & AUDIO_DSP_AGC ? &enabled : &disabled);
- speex_preprocess_ctl(dsp->read_preprocess, SPEEX_PREPROCESS_SET_DENOISE,
- read_opt & AUDIO_DSP_DENOISE ? &enabled : &disabled);
- }
- if (write_opt) {
- int disabled = 0;
- int enabled = 1;
- dsp->write_preprocess = speex_preprocess_state_init(dsp->frame_samples,clock);
- speex_preprocess_ctl(dsp->write_preprocess, SPEEX_PREPROCESS_SET_AGC,
- write_opt & AUDIO_DSP_AGC ? &enabled : &disabled);
- speex_preprocess_ctl(dsp->write_preprocess, SPEEX_PREPROCESS_SET_DENOISE,
- write_opt & AUDIO_DSP_DENOISE ? &enabled : &disabled);
- }
- *p_dsp = dsp;
- return APR_SUCCESS;
- }
- void audiodsp_destroy(audiodsp_t *dsp)
- {
- if (dsp->read_preprocess) {
- speex_preprocess_state_destroy(dsp->read_preprocess);
- dsp->read_preprocess = NULL;
- }
- if (dsp->write_preprocess) {
- speex_preprocess_state_destroy(dsp->write_preprocess);
- dsp->write_preprocess = NULL;
- }
- }
|