/* * main() */ int main(int argc, char *argv[]) { pj_caching_pool cp; pjmedia_endpt *med_endpt; pj_pool_t *pool; pjmedia_port *play_port; pjmedia_port *rec_port; pjmedia_port *bidir_port; pjmedia_snd_port *snd; char tmp[10]; pj_status_t status; if (argc != 3) { puts("Error: arguments required"); puts(desc); return 1; } /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* * Initialize media endpoint. * This will implicitly initialize PJMEDIA too. */ status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Create memory pool for our file player */ pool = pj_pool_create( &cp.factory, /* pool factory */ "wav", /* pool name. */ 4000, /* init size */ 4000, /* increment size */ NULL /* callback on error */ ); /* Create file media port from the WAV file */ status = pjmedia_wav_player_port_create( pool, /* memory pool */ argv[1], /* file to play */ PTIME, /* ptime. */ 0, /* flags */ 0, /* default buffer */ &play_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open input WAV file", status); return 1; } if (play_port->info.channel_count != 1) { puts("Error: input WAV must have 1 channel audio"); return 1; } if (play_port->info.bits_per_sample != 16) { puts("Error: input WAV must be encoded as 16bit PCM"); return 1; } #ifdef PJ_DARWINOS /* Need to force clock rate on MacOS */ if (play_port->info.clock_rate != 44100) { pjmedia_port *resample_port; status = pjmedia_resample_port_create(pool, play_port, 44100, 0, &resample_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create resampling port", status); return 1; } data.play_port = resample_port; } #endif /* Create WAV output file port */ status = pjmedia_wav_writer_port_create(pool, argv[2], play_port->info.clock_rate, play_port->info.channel_count, play_port->info.samples_per_frame, play_port->info.bits_per_sample, 0, 0, &rec_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open output file", status); return 1; } /* Create bidirectional port from the WAV ports */ pjmedia_bidirectional_port_create(pool, play_port, rec_port, &bidir_port); /* Create sound device. */ status = pjmedia_snd_port_create(pool, -1, -1, play_port->info.clock_rate, play_port->info.channel_count, play_port->info.samples_per_frame, play_port->info.bits_per_sample, 0, &snd); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open sound device", status); return 1; } /* Customize AEC */ pjmedia_snd_port_set_ec(snd, pool, TAIL_LENGTH, 0); /* Connect sound to the port */ pjmedia_snd_port_connect(snd, bidir_port); puts(""); printf("Playing %s and recording to %s\n", argv[1], argv[2]); puts("Press <ENTER> to quit"); fgets(tmp, sizeof(tmp), stdin); /* Start deinitialization: */ /* Destroy sound device */ status = pjmedia_snd_port_destroy( snd ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Destroy file port(s) */ status = pjmedia_port_destroy( play_port ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); status = pjmedia_port_destroy( rec_port ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Release application pool */ pj_pool_release( pool ); /* Destroy media endpoint. */ pjmedia_endpt_destroy( med_endpt ); /* Destroy pool factory */ pj_caching_pool_destroy( &cp ); /* Shutdown PJLIB */ pj_shutdown(); /* Done. */ return 0; }
int main(int argc, char *argv[]) { pj_caching_pool cp; pjmedia_endpt *med_endpt; pj_pool_t *pool; pjmedia_port *file_port; pjmedia_port *resample_port; pjmedia_snd_port *snd_port; char tmp[10]; pj_status_t status; int dev_id = -1; int sampling_rate = CLOCK_RATE; int channel_count = NCHANNELS; int samples_per_frame = NSAMPLES; int bits_per_sample = NBITS; //int ptime; //int down_samples; /* Must init PJLIB first: */ status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Get options */ if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &sampling_rate, &channel_count, &samples_per_frame, &bits_per_sample)) { puts(""); puts(desc); return 1; } if (!argv[pj_optind]) { puts("Error: no file is specified"); puts(desc); return 1; } /* Must create a pool factory before we can allocate any memory. */ pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); /* * Initialize media endpoint. * This will implicitly initialize PJMEDIA too. */ status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Create memory pool for our file player */ pool = pj_pool_create( &cp.factory, /* pool factory */ "app", /* pool name. */ 4000, /* init size */ 4000, /* increment size */ NULL /* callback on error */ ); /* Create the file port. */ status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0, 0, &file_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open file", status); return 1; } /* File must have same number of channels. */ if (PJMEDIA_PIA_CCNT(&file_port->info) != (unsigned)channel_count) { PJ_LOG(3,(THIS_FILE, "Error: file has different number of channels. " "Perhaps you'd need -c option?")); pjmedia_port_destroy(file_port); return 1; } /* Calculate number of samples per frame to be taken from file port */ //ptime = samples_per_frame * 1000 / sampling_rate; /* Create the resample port. */ status = pjmedia_resample_port_create( pool, file_port, sampling_rate, 0, &resample_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to create resample port", status); return 1; } /* Create sound player port. */ status = pjmedia_snd_port_create( pool, /* pool */ dev_id, /* device */ dev_id, /* device */ sampling_rate, /* clock rate. */ channel_count, /* # of channels. */ samples_per_frame, /* samples per frame. */ bits_per_sample, /* bits per sample. */ 0, /* options */ &snd_port /* returned port */ ); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Unable to open sound device", status); return 1; } /* Connect resample port to sound device */ status = pjmedia_snd_port_connect( snd_port, resample_port); if (status != PJ_SUCCESS) { app_perror(THIS_FILE, "Error connecting sound ports", status); return 1; } /* Dump memory usage */ dump_pool_usage(THIS_FILE, &cp); /* * File should be playing and looping now, using sound device's thread. */ /* Sleep to allow log messages to flush */ pj_thread_sleep(100); printf("Playing %s at sampling rate %d (original file sampling rate=%d)\n", argv[pj_optind], sampling_rate, PJMEDIA_PIA_SRATE(&file_port->info)); puts(""); puts("Press <ENTER> to stop playing and quit"); if (fgets(tmp, sizeof(tmp), stdin) == NULL) { puts("EOF while reading stdin, will quit now.."); } /* Start deinitialization: */ /* Destroy sound device */ status = pjmedia_snd_port_destroy( snd_port ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Destroy resample port. * This will destroy all downstream ports (e.g. the file port) */ status = pjmedia_port_destroy( resample_port ); PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); /* Release application pool */ pj_pool_release( pool ); /* Destroy media endpoint. */ pjmedia_endpt_destroy( med_endpt ); /* Destroy pool factory */ pj_caching_pool_destroy( &cp ); /* Shutdown PJLIB */ pj_shutdown(); /* Done. */ return 0; }
pjs_audio_combine::pjs_audio_combine(pjs_global &global, pjs_media_sync &sync, unsigned output_clock_rate, unsigned input_clock_rate, unsigned input_samples_per_frame, bool oneway) : m_global(global), m_sync(sync), m_combiner(NULL), m_input1(NULL), m_input2( NULL), m_resample1(NULL), m_resample2(NULL), m_oneway(oneway) { m_clock_rate = output_clock_rate; m_started = false; m_ready = false; m_putbuffer = NULL; m_buffer_size = 0; if (input_clock_rate != output_clock_rate) m_samples_per_frame = 2 * input_samples_per_frame * output_clock_rate /input_clock_rate; else m_samples_per_frame = 2 * input_samples_per_frame; m_pool = pj_pool_create(m_global.get_pool_factory(), "pjs_audio_combine", 128, 128, NULL); if (m_pool) { if (pjmedia_splitcomb_create(m_pool, output_clock_rate, 2, m_samples_per_frame, 16, 0, &m_combiner) == PJ_SUCCESS) { if (pjmedia_splitcomb_create_rev_channel(m_pool, m_combiner, 0, 0, &m_input1) == PJ_SUCCESS) { if (pjmedia_splitcomb_create_rev_channel(m_pool, m_combiner, 1, 0, &m_input2) == PJ_SUCCESS) { if (input_clock_rate != output_clock_rate) { if (pjmedia_resample_port_create(m_pool, m_input1, output_clock_rate, PJMEDIA_RESAMPLE_DONT_DESTROY_DN | PJMEDIA_RESAMPLE_USE_SMALL_FILTER, &m_resample1)==PJ_SUCCESS) { if (pjmedia_resample_port_create(m_pool, m_input2, output_clock_rate, PJMEDIA_RESAMPLE_DONT_DESTROY_DN | PJMEDIA_RESAMPLE_USE_SMALL_FILTER, &m_resample2)==PJ_SUCCESS) { m_ready = true; } else m_resample2 = NULL; } else m_resample1 = NULL; } else m_ready = true; if (m_ready) { m_buffer_size = m_combiner->info.samples_per_frame * m_combiner->info.channel_count * m_combiner->info.bits_per_sample / 8; m_getbuffer = (pj_int16_t*) pj_pool_alloc(m_pool, m_buffer_size); if (!m_oneway) { m_putbuffer = (pj_int16_t*) pj_pool_alloc(m_pool, m_buffer_size); m_ready = m_getbuffer && m_putbuffer; } else { m_ready = m_getbuffer; m_putbuffer = NULL; } } } else m_input2 = NULL; } else m_input1 = NULL; } else m_combiner = NULL; } }