| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <pjmedia.h> |
| 32 |
#include <pjlib-util.h> |
| 33 |
#include <pjlib.h> |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
|
| 38 |
#include "util.h" |
| 39 |
|
| 40 |
|
| 41 |
#define THIS_FILE "resampleplay.c" |
| 42 |
|
| 43 |
|
| 44 |
static const char *desc = |
| 45 |
" FILE \n" |
| 46 |
" \n" |
| 47 |
" resampleplay.c \n" |
| 48 |
" \n" |
| 49 |
" PURPOSE \n" |
| 50 |
" \n" |
| 51 |
" Demonstrate how use resample port to play a WAV file to sound \n" |
| 52 |
" device using different sampling rate. \n" |
| 53 |
" \n" |
| 54 |
" USAGE \n" |
| 55 |
" \n" |
| 56 |
" resampleplay [options] FILE.WAV \n" |
| 57 |
" \n" |
| 58 |
" where options: \n" |
| 59 |
SND_USAGE |
| 60 |
" \n" |
| 61 |
" The WAV file could have mono or stereo channels with arbitrary \n" |
| 62 |
" sampling rate, but MUST contain uncompressed (i.e. 16bit) PCM. \n"; |
| 63 |
|
| 64 |
|
| 65 |
int main(int argc, char *argv[]) |
| 66 |
{ |
| 67 |
pj_caching_pool cp; |
| 68 |
pjmedia_endpt *med_endpt; |
| 69 |
pj_pool_t *pool; |
| 70 |
pjmedia_port *file_port; |
| 71 |
pjmedia_port *resample_port; |
| 72 |
pjmedia_snd_port *snd_port; |
| 73 |
char tmp[10]; |
| 74 |
pj_status_t status; |
| 75 |
|
| 76 |
int dev_id = -1; |
| 77 |
int sampling_rate = CLOCK_RATE; |
| 78 |
int channel_count = NCHANNELS; |
| 79 |
int samples_per_frame = NSAMPLES; |
| 80 |
int bits_per_sample = NBITS; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
status = pj_init(); |
| 86 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if (get_snd_options(THIS_FILE, argc, argv, &dev_id, &sampling_rate, |
| 91 |
&channel_count, &samples_per_frame, &bits_per_sample)) |
| 92 |
{ |
| 93 |
puts(""); |
| 94 |
puts(desc); |
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
if (!argv[pj_optind]) { |
| 99 |
puts("Error: no file is specified"); |
| 100 |
puts(desc); |
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 112 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 113 |
|
| 114 |
|
| 115 |
pool = pj_pool_create( &cp.factory, |
| 116 |
"app", |
| 117 |
4000, |
| 118 |
4000, |
| 119 |
NULL |
| 120 |
); |
| 121 |
|
| 122 |
|
| 123 |
status = pjmedia_wav_player_port_create( pool, argv[pj_optind], 0, 0, |
| 124 |
0, &file_port); |
| 125 |
if (status != PJ_SUCCESS) { |
| 126 |
app_perror(THIS_FILE, "Unable to open file", status); |
| 127 |
return 1; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
if (PJMEDIA_PIA_CCNT(&file_port->info) != (unsigned)channel_count) { |
| 132 |
PJ_LOG(3,(THIS_FILE, "Error: file has different number of channels. " |
| 133 |
"Perhaps you'd need -c option?")); |
| 134 |
pjmedia_port_destroy(file_port); |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
status = pjmedia_resample_port_create( pool, file_port, |
| 143 |
sampling_rate, 0, |
| 144 |
&resample_port); |
| 145 |
if (status != PJ_SUCCESS) { |
| 146 |
app_perror(THIS_FILE, "Unable to create resample port", status); |
| 147 |
return 1; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
status = pjmedia_snd_port_create( |
| 152 |
pool, |
| 153 |
dev_id, |
| 154 |
dev_id, |
| 155 |
sampling_rate, |
| 156 |
channel_count, |
| 157 |
samples_per_frame, |
| 158 |
bits_per_sample, |
| 159 |
0, |
| 160 |
&snd_port |
| 161 |
); |
| 162 |
if (status != PJ_SUCCESS) { |
| 163 |
app_perror(THIS_FILE, "Unable to open sound device", status); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
status = pjmedia_snd_port_connect( snd_port, resample_port); |
| 169 |
if (status != PJ_SUCCESS) { |
| 170 |
app_perror(THIS_FILE, "Error connecting sound ports", status); |
| 171 |
return 1; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
dump_pool_usage(THIS_FILE, &cp); |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
pj_thread_sleep(100); |
| 185 |
|
| 186 |
|
| 187 |
printf("Playing %s at sampling rate %d (original file sampling rate=%d)\n", |
| 188 |
argv[pj_optind], sampling_rate, |
| 189 |
PJMEDIA_PIA_SRATE(&file_port->info)); |
| 190 |
puts(""); |
| 191 |
puts("Press <ENTER> to stop playing and quit"); |
| 192 |
|
| 193 |
if (fgets(tmp, sizeof(tmp), stdin) == NULL) { |
| 194 |
puts("EOF while reading stdin, will quit now.."); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
status = pjmedia_snd_port_destroy( snd_port ); |
| 202 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
status = pjmedia_port_destroy( resample_port ); |
| 209 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
pj_pool_release( pool ); |
| 214 |
|
| 215 |
|
| 216 |
pjmedia_endpt_destroy( med_endpt ); |
| 217 |
|
| 218 |
|
| 219 |
pj_caching_pool_destroy( &cp ); |
| 220 |
|
| 221 |
|
| 222 |
pj_shutdown(); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
return 0; |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|