| 1 |
/* |
| 2 |
audio: audio output interface |
| 3 |
|
| 4 |
This wraps a bit of out123 now, with a layer of resampling using syn123. |
| 5 |
|
| 6 |
copyright ?-2020 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 7 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 8 |
initially written by Michael Hipp |
| 9 |
|
| 10 |
Pitching can work in three ways: |
| 11 |
- fixed (native) decoder rate, varied hardware playback rate |
| 12 |
- fixed hardware playback rate, any decoder rate, resampler |
| 13 |
- pitched NtoM decoder rate, fixed hardware |
| 14 |
|
| 15 |
Just because ... |
| 16 |
|
| 17 |
If you force an output rate, either the NtoM resampler in libmpg123 or the syn123 |
| 18 |
resampler wrapped here adapts the data. |
| 19 |
|
| 20 |
TODO: If the proper resampler is configured, it should fill in unsupported output |
| 21 |
rates automatically ... or not? Right now, I have the rule that you don't get |
| 22 |
the expensive resampler unless you forced an output rate. But you do get the |
| 23 |
libmpg123 NtoM resampling now if it finds only a working output rate that is |
| 24 |
reachable via that. Well, imposing a resampler that needs more CPU time than |
| 25 |
the decoder is just something that should not happen without being called for. |
| 26 |
So I rather provide a warning to the user that the NtoM resampler has been |
| 27 |
triggered. Yes, inform the user. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <errno.h> |
| 31 |
#include "mpg123app.h" |
| 32 |
#include "audio.h" |
| 33 |
#include "out123.h" |
| 34 |
#include "syn123.h" |
| 35 |
#include "common.h" |
| 36 |
#include "metaprint.h" |
| 37 |
#include "sysutil.h" |
| 38 |
|
| 39 |
#ifdef HAVE_SYS_WAIT_H |
| 40 |
#include <sys/wait.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#include "common/debug.h" |
| 44 |
|
| 45 |
static syn123_handle *sh = NULL; |
| 46 |
static struct mpg123_fmt outfmt = { .encoding=0, .rate=0, .channels=0 }; |
| 47 |
static int outch = 0; // currently used number of output channels |
| 48 |
|
| 49 |
// A convoluted way to say outch*4, for semantic clarity. |
| 50 |
#define RESAMPLE_FRAMESIZE(ch) ((ch)*MPG123_SAMPLESIZE(MPG123_ENC_FLOAT_32)) |
| 51 |
#define OUTPUT_FRAMESIZE(ch) ((ch)*MPG123_SAMPLESIZE(outfmt.encoding)) |
| 52 |
// Resampler buffers, first for resampling output, then for conversion. |
| 53 |
// Instead of sizing them for any possible input rate, I'll try to |
| 54 |
// loop in the output wrapper over a fixed buffer size. The syn123 resampler |
| 55 |
// can tell me how many samples to feed to avoid exceeding the output buffer. |
| 56 |
static char *resample_buffer = NULL; |
| 57 |
static char* resample_outbuf = NULL; |
| 58 |
static size_t resample_block = 0; |
| 59 |
// A good buffer size: |
| 60 |
// 1152*48/44.1*2*4 = 10032 ... let's go 16K. |
| 61 |
// This should work for final output data, too. |
| 62 |
// We'll loop over pieces if the buffer size is not enough for upsampling. |
| 63 |
static size_t resample_bytes = 1<<16; |
| 64 |
int do_resample = 0; |
| 65 |
int do_resample_now = 0; // really apply resampler for current stream. |
| 66 |
|
| 67 |
/* Quick-shot paired table setup with remembering search in it. |
| 68 |
this is for storing pairs of output sampling rate and decoding |
| 69 |
sampling rate. */ |
| 70 |
struct ratepair { long a; long b; }; |
| 71 |
static long *outrates = NULL; |
| 72 |
static struct ratepair *unpitch = NULL; |
| 73 |
|
| 74 |
|
| 75 |
static int audio_capabilities(out123_handle *ao, mpg123_handle *mh); |
| 76 |
|
| 77 |
#define CLEAN_POINTER(p, func) if(p) func(p); p = NULL; |
| 78 |
void audio_cleanup(void) |
| 79 |
{ |
| 80 |
CLEAN_POINTER(outrates, free) |
| 81 |
CLEAN_POINTER(unpitch, free) |
| 82 |
CLEAN_POINTER(sh, syn123_del) |
| 83 |
CLEAN_POINTER(resample_outbuf, free) |
| 84 |
CLEAN_POINTER(resample_buffer, free) |
| 85 |
} |
| 86 |
|
| 87 |
int audio_setup(out123_handle *ao, mpg123_handle *mh) |
| 88 |
{ |
| 89 |
do_resample = (param.force_rate > 0 && param.resample); |
| 90 |
resample_block = 0; |
| 91 |
// Settle formats. |
| 92 |
if(audio_capabilities(ao, mh)) |
| 93 |
return -1; |
| 94 |
// Prepare resample. |
| 95 |
// Resampling only for forced rate for now. In future, pitching should |
| 96 |
// also be handled. |
| 97 |
if(do_resample) |
| 98 |
{ |
| 99 |
int err; |
| 100 |
sh = syn123_new(outfmt.rate, 1, outfmt.encoding, 0, &err); |
| 101 |
if(!sh) |
| 102 |
{ |
| 103 |
merror("Cannot initialize syn123: %s\n", syn123_strerror(err)); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
resample_buffer = malloc(resample_bytes*10); |
| 107 |
resample_outbuf = malloc(resample_bytes*10); |
| 108 |
if(!resample_buffer || !resample_outbuf) |
| 109 |
return -1; |
| 110 |
} |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
int audio_prepare( out123_handle *ao, mpg123_handle *mh |
| 115 |
, long rate, int channels, int encoding ) |
| 116 |
{ |
| 117 |
mdebug( "audio_prepare %ld Hz / %ld Hz, %i ch, enc %s" |
| 118 |
, rate, outfmt.rate, channels, out123_enc_name(encoding) ); |
| 119 |
if(do_resample && param.pitch == 0. && rate == outfmt.rate) |
| 120 |
{ |
| 121 |
do_resample_now = 0; |
| 122 |
if(param.verbose > 1) |
| 123 |
fprintf(stderr, "Note: resampler disabled for native rate\n"); |
| 124 |
} else if(do_resample) |
| 125 |
{ |
| 126 |
do_resample_now = 1; |
| 127 |
// Smooth option could be considered once pitching is implemented with the |
| 128 |
// resampler.The existing state might fit the coming data if this is two |
| 129 |
// seamless tracks. If not, it's jut the first few samples that differ |
| 130 |
// significantly depending on which data went through the resampler |
| 131 |
// previously. |
| 132 |
int err = syn123_setup_resample( sh, pitch_rate(rate), outfmt.rate, channels |
| 133 |
, (param.resample < 2), 0 ); |
| 134 |
if(err) |
| 135 |
{ |
| 136 |
merror("failed to set up resampler: %s", syn123_strerror(err)); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
outch = channels; |
| 140 |
// We can store a certain ammount of frames in the resampler buffer |
| 141 |
// and the final output buffer after conversion. |
| 142 |
size_t frames = resample_bytes / ( |
| 143 |
RESAMPLE_FRAMESIZE(channels) > OUTPUT_FRAMESIZE(channels) |
| 144 |
? RESAMPLE_FRAMESIZE(channels) |
| 145 |
: OUTPUT_FRAMESIZE(channels) ); |
| 146 |
// Minimum amount of input samples to fill the buffer. |
| 147 |
resample_block = syn123_resample_fillcount(pitch_rate(rate), outfmt.rate, frames); |
| 148 |
if(!resample_block) |
| 149 |
return -1; // WTF? No comment. |
| 150 |
if(param.verbose > 1) |
| 151 |
fprintf(stderr, "Note: resampler setup: %ld Hz -> %ld Hz\n", pitch_rate(rate), outfmt.rate); |
| 152 |
rate = outfmt.rate; |
| 153 |
encoding = outfmt.encoding; |
| 154 |
} else if(outfmt.rate) |
| 155 |
rate = outfmt.rate; // That's pitching with NtoM. |
| 156 |
else |
| 157 |
{ |
| 158 |
struct mpg123_frameinfo fi; |
| 159 |
static int ntom_warn = 0; |
| 160 |
if( !ntom_warn && !param.quiet && |
| 161 |
MPG123_OK == mpg123_info(mh, &fi) && fi.rate != rate ) |
| 162 |
{ |
| 163 |
fprintf(stderr, |
| 164 |
"\nWarning: You triggered the NtoM drop-sample resampler inside libmpg123.\n" |
| 165 |
"Warning: You could trade CPU for quality by forcing a supported output rate.\n" ); |
| 166 |
ntom_warn = 1; |
| 167 |
} |
| 168 |
rate = pitch_rate(rate); // That's plain hardware pitching. |
| 169 |
} |
| 170 |
if(param.verbose > 1) |
| 171 |
{ |
| 172 |
const char* encname = out123_enc_name(encoding); |
| 173 |
fprintf( stderr // No extra line break, as this is a follow-up note. |
| 174 |
, "Note: Hardware output format %li Hz, %i channels, encoding %s.\n" |
| 175 |
, rate, channels, encname ? encname : "???" ); |
| 176 |
} |
| 177 |
return out123_start(ao, rate, channels, encoding); |
| 178 |
} |
| 179 |
|
| 180 |
// Loop over blocks with the resampler, think about intflag. |
| 181 |
size_t audio_play(out123_handle *ao, void *buffer, size_t bytes) |
| 182 |
{ |
| 183 |
if(do_resample_now) |
| 184 |
{ |
| 185 |
int fs = RESAMPLE_FRAMESIZE(outch); |
| 186 |
size_t pcmframes = bytes/fs; |
| 187 |
size_t done = 0; |
| 188 |
while(pcmframes && !intflag) |
| 189 |
{ |
| 190 |
size_t block = resample_block > pcmframes |
| 191 |
? pcmframes |
| 192 |
: resample_block; |
| 193 |
size_t oblock = syn123_resample( sh, (float*)resample_buffer |
| 194 |
, (float*)((char*)buffer+done), block ); |
| 195 |
if(!oblock) |
| 196 |
break; |
| 197 |
size_t obytes = 0; |
| 198 |
if(syn123_conv( resample_outbuf, outfmt.encoding, resample_bytes |
| 199 |
, resample_buffer, MPG123_ENC_FLOAT_32, oblock*fs |
| 200 |
, &obytes, NULL, sh )) |
| 201 |
break; |
| 202 |
size_t oplay = out123_play(ao, resample_outbuf, obytes); |
| 203 |
if(oplay < obytes) |
| 204 |
{ |
| 205 |
// Need to translate that. How many input samples got played, |
| 206 |
// actually? A bit of roundoff error doesn't hurt, so let's just |
| 207 |
// wing it. Close is enough. |
| 208 |
size_t iframes = (size_t)((double)oplay/obytes*block); |
| 209 |
while(iframes >= block) |
| 210 |
--iframes; |
| 211 |
done += iframes*fs; |
| 212 |
break; |
| 213 |
} |
| 214 |
pcmframes -= block; |
| 215 |
done += block*fs; |
| 216 |
} |
| 217 |
return done; |
| 218 |
} |
| 219 |
else |
| 220 |
return out123_play(ao, buffer, bytes); |
| 221 |
} |
| 222 |
|
| 223 |
mpg123_string* audio_enclist(void) |
| 224 |
{ |
| 225 |
int i; |
| 226 |
mpg123_string *list; |
| 227 |
size_t enc_count = 0; |
| 228 |
const int *enc_codes = NULL; |
| 229 |
|
| 230 |
/* Only the encodings supported by libmpg123 build |
| 231 |
Those returned by out123_enc_list() are a superset. */ |
| 232 |
mpg123_encodings(&enc_codes, &enc_count); |
| 233 |
if((list = malloc(sizeof(*list)))) |
| 234 |
mpg123_init_string(list); |
| 235 |
/* Further calls to mpg123 string lib are hardened against NULL. */ |
| 236 |
for(i=0;i<enc_count;++i) |
| 237 |
{ |
| 238 |
if(i>0) |
| 239 |
mpg123_add_string(list, " "); |
| 240 |
mpg123_add_string(list, out123_enc_name(enc_codes[i])); |
| 241 |
} |
| 242 |
return list; |
| 243 |
} |
| 244 |
|
| 245 |
static void capline(mpg123_handle *mh, long rate, struct mpg123_fmt *outfmt) |
| 246 |
{ |
| 247 |
int enci; |
| 248 |
const int *encs; |
| 249 |
size_t num_encs; |
| 250 |
mpg123_encodings(&encs, &num_encs); |
| 251 |
fprintf(stderr," %5ld |", outfmt ? outfmt->rate : rate); |
| 252 |
for(enci=0; enci<num_encs; ++enci) |
| 253 |
{ |
| 254 |
int fmt = outfmt |
| 255 |
? (encs[enci] == outfmt->encoding ? outfmt->channels : 0) |
| 256 |
: mpg123_format_support(mh, rate, encs[enci]); |
| 257 |
switch(fmt) |
| 258 |
{ |
| 259 |
case MPG123_MONO: fprintf(stderr, " M "); break; |
| 260 |
case MPG123_STEREO: fprintf(stderr, " S "); break; |
| 261 |
case MPG123_MONO|MPG123_STEREO: fprintf(stderr, " M/S "); break; |
| 262 |
default: fprintf(stderr, " "); |
| 263 |
} |
| 264 |
} |
| 265 |
fprintf(stderr, "\n"); |
| 266 |
} |
| 267 |
|
| 268 |
void print_capabilities(out123_handle *ao, mpg123_handle *mh) |
| 269 |
{ |
| 270 |
int r,e; |
| 271 |
const long *rates; |
| 272 |
size_t num_rates; |
| 273 |
const int *encs; |
| 274 |
size_t num_encs; |
| 275 |
char *name; |
| 276 |
char *dev; |
| 277 |
out123_driver_info(ao, &name, &dev); |
| 278 |
mpg123_rates(&rates, &num_rates); |
| 279 |
mpg123_encodings(&encs, &num_encs); |
| 280 |
fprintf(stderr,"\nAudio driver: %s\nAudio device: ", name); |
| 281 |
print_outstr(stderr, dev, 0, stderr_is_term); |
| 282 |
fprintf(stderr, "\n"); |
| 283 |
fprintf( stderr, "%s", "Audio capabilities:\n" |
| 284 |
"(matrix of [S]tereo or [M]ono support for sample format and rate in Hz)\n" |
| 285 |
"\n" |
| 286 |
" rate |" ); |
| 287 |
for(e=0;e<num_encs;e++) |
| 288 |
{ |
| 289 |
const char *encname = out123_enc_name(encs[e]); |
| 290 |
fprintf(stderr," %4s ", encname ? encname : "???"); |
| 291 |
} |
| 292 |
|
| 293 |
fprintf(stderr,"\n -------"); |
| 294 |
for(e=0;e<num_encs;e++) fprintf(stderr,"------"); |
| 295 |
|
| 296 |
fprintf(stderr, "\n"); |
| 297 |
for(r=0; r<num_rates; ++r) capline(mh, rates[r], NULL); |
| 298 |
|
| 299 |
if(param.force_rate) |
| 300 |
{ |
| 301 |
fprintf(stderr," -------"); |
| 302 |
for(e=0;e<num_encs;e++) fprintf(stderr,"------"); |
| 303 |
fprintf(stderr, "\n"); |
| 304 |
if(do_resample) |
| 305 |
capline(mh, 0, &outfmt); |
| 306 |
else |
| 307 |
capline(mh, bpitch_rate(param.force_rate), NULL); |
| 308 |
} |
| 309 |
fprintf(stderr,"\n"); |
| 310 |
if(do_resample) |
| 311 |
{ |
| 312 |
if(param.pitch != 0.) |
| 313 |
fprintf( stderr, "Resampler with pitch: %g\n" |
| 314 |
, param.pitch ); |
| 315 |
else |
| 316 |
fprintf(stderr, "Resampler configured.\n"); |
| 317 |
fprintf( stderr, "%s\n%s\n" |
| 318 |
, "Decoding to f32 as intermediate if needed." |
| 319 |
, "Resampler output format is in the last line." ); |
| 320 |
} |
| 321 |
else if(param.force_rate) |
| 322 |
fprintf( stderr |
| 323 |
, "%s rate forced. Resulting format support shown in last line.\n" |
| 324 |
, param.pitch != 0. ? "Pitched decoder" : "Decoder" ); |
| 325 |
else if(param.pitch != 0.) |
| 326 |
fprintf( stderr, "Actual output rates adjusted by pitch value %g.\n" |
| 327 |
, param.pitch ); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
long brate(struct ratepair *table, long arate, int count, int *last) |
| 332 |
{ |
| 333 |
int i = 0; |
| 334 |
int j; |
| 335 |
for(j=0; j<2; ++j) |
| 336 |
{ |
| 337 |
i = i ? 0 : *last; |
| 338 |
for(; i<count; ++i) if(table[i].a == arate) |
| 339 |
{ |
| 340 |
*last = i; |
| 341 |
return table[i].b; |
| 342 |
} |
| 343 |
} |
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
// Return one of the given list of encodings matching the mask, |
| 348 |
// in order. Zero if none. |
| 349 |
static int match_enc(int mask, const int *enc_list, size_t enc_count) |
| 350 |
{ |
| 351 |
int enc = 0; |
| 352 |
for(size_t i=0; i<enc_count; ++i) |
| 353 |
{ |
| 354 |
if((enc_list[i] & mask) == enc_list[i]) |
| 355 |
{ |
| 356 |
enc = enc_list[i]; |
| 357 |
break; |
| 358 |
} |
| 359 |
} |
| 360 |
return enc; |
| 361 |
} |
| 362 |
|
| 363 |
/* This uses the currently opened audio device, queries its caps. |
| 364 |
In case of buffered playback, this works _once_ by querying the buffer for the caps before entering the main loop. */ |
| 365 |
static int audio_capabilities(out123_handle *ao, mpg123_handle *mh) |
| 366 |
{ |
| 367 |
int force_fmt = 0; |
| 368 |
size_t ri; |
| 369 |
/* Pitching introduces a difference between decoder rate and playback rate. */ |
| 370 |
long decode_rate; |
| 371 |
const long *rates; |
| 372 |
struct mpg123_fmt *outfmts = NULL; |
| 373 |
int fmtcount; |
| 374 |
size_t num_rates; |
| 375 |
if(param.pitch < -0.99) |
| 376 |
param.pitch = -0.99; |
| 377 |
long ntom_rate = do_resample ? 0 : bpitch_rate(param.force_rate); |
| 378 |
outfmt.rate = param.force_rate; |
| 379 |
outfmt.channels = 0; |
| 380 |
outfmt.encoding = 0; |
| 381 |
|
| 382 |
debug("audio_capabilities"); |
| 383 |
mpg123_rates(&rates, &num_rates); |
| 384 |
|
| 385 |
mpg123_format_none(mh); /* Start with nothing. */ |
| 386 |
|
| 387 |
if(do_resample && param.verbose > 2) |
| 388 |
fprintf( stderr |
| 389 |
, "Note: decoder always forced to %s encoding for resampler\n" |
| 390 |
, out123_enc_name(MPG123_ENC_FLOAT_32) ); |
| 391 |
|
| 392 |
if(param.force_encoding != NULL) |
| 393 |
{ |
| 394 |
if(!param.quiet) |
| 395 |
fprintf(stderr, "Note: forcing output encoding %s\n", param.force_encoding); |
| 396 |
|
| 397 |
force_fmt = out123_enc_byname(param.force_encoding); |
| 398 |
if(!force_fmt) |
| 399 |
{ |
| 400 |
char *fs = NULL; |
| 401 |
outstr(&fs, param.force_encoding, 0, stderr_is_term); |
| 402 |
error1("Failed to find an encoding to match requested \"%s\"!\n" |
| 403 |
, PSTR(fs)); |
| 404 |
free(fs); |
| 405 |
return -1; /* No capabilities at all... */ |
| 406 |
} |
| 407 |
else if(param.verbose > 2) |
| 408 |
fprintf(stderr, "Note: forcing encoding code 0x%x (%s)\n" |
| 409 |
, (unsigned)force_fmt, out123_enc_name(force_fmt)); |
| 410 |
} |
| 411 |
|
| 412 |
// A possible optimization for resampling mode is to keep existing output |
| 413 |
// format support configured and don't even interrupt the output device at |
| 414 |
// all. If you change pitch, you just change a number for the resampler. |
| 415 |
// But currently, the idea of re-opening the output device on format |
| 416 |
// changes is rather ingrained in mpg123. |
| 417 |
|
| 418 |
if(do_resample) |
| 419 |
{ |
| 420 |
// If really doing the extra resampling, output will always run with |
| 421 |
// this setup, regardless of decoder. |
| 422 |
int enc1 = out123_encodings(ao, outfmt.rate, 1); |
| 423 |
int enc2 = out123_encodings(ao, outfmt.rate, 2); |
| 424 |
if(force_fmt) |
| 425 |
{ |
| 426 |
enc1 &= force_fmt; |
| 427 |
enc2 &= force_fmt; |
| 428 |
} else |
| 429 |
{ |
| 430 |
long propflags = 0; |
| 431 |
out123_getparam_int(ao, OUT123_PROPFLAGS, &propflags); |
| 432 |
if(!(propflags & OUT123_PROP_LIVE)) |
| 433 |
{ |
| 434 |
fmtcount = out123_formats(ao, NULL, 0, 1, 2, &outfmts); |
| 435 |
if(fmtcount == 1 && outfmts[0].encoding > 0) |
| 436 |
{ |
| 437 |
const char *encname = out123_enc_name(outfmts[0].encoding); |
| 438 |
if(param.verbose > 1) |
| 439 |
fprintf( stderr, "Note: honouring non-live default encoding of %s\n" |
| 440 |
, encname ? encname : "???" ); |
| 441 |
enc1 &= outfmts[0].encoding; |
| 442 |
enc2 &= outfmts[0].encoding; |
| 443 |
} |
| 444 |
free(outfmts); |
| 445 |
outfmts = NULL; |
| 446 |
} else if(param.verbose > 1) |
| 447 |
fprintf(stderr, "Note: negotiating the best encoding with live sink\n"); |
| 448 |
} |
| 449 |
mdebug("enc mono=0x%x stereo=0x%x", (unsigned)enc1, (unsigned)enc2); |
| 450 |
if(!enc1 && !enc2) |
| 451 |
{ |
| 452 |
error("Output device does not support forced rate and/or encoding."); |
| 453 |
return -1; |
| 454 |
} else if(enc1 && enc2) |
| 455 |
{ |
| 456 |
// Should be the normal case: Mono and Stereo with the same formats. |
| 457 |
// We'll store the common subset, which should normally be all. |
| 458 |
outfmt.encoding = enc1 & enc2; |
| 459 |
outfmt.channels = MPG123_MONO|MPG123_STEREO; |
| 460 |
if(!outfmt.encoding) |
| 461 |
{ |
| 462 |
error("No common decodings for mono and stereo output. Too weird."); |
| 463 |
return -1; |
| 464 |
} |
| 465 |
} else if(enc1) |
| 466 |
{ // Mono only. |
| 467 |
outfmt.encoding = enc1; |
| 468 |
outfmt.channels = 1; |
| 469 |
} else |
| 470 |
{ // Stereo only. |
| 471 |
outfmt.encoding = enc2; |
| 472 |
outfmt.channels = 2; |
| 473 |
} |
| 474 |
int pref_enc[] = { MPG123_ENC_FLOAT_32 |
| 475 |
, MPG123_ENC_SIGNED_32, MPG123_ENC_UNSIGNED_32 |
| 476 |
, MPG123_ENC_SIGNED_24, MPG123_ENC_UNSIGNED_24 |
| 477 |
, MPG123_ENC_SIGNED_16, MPG123_ENC_UNSIGNED_16 }; |
| 478 |
int nenc = match_enc(outfmt.encoding, pref_enc, sizeof(pref_enc)/sizeof(int)); |
| 479 |
if(!nenc) |
| 480 |
{ |
| 481 |
const int *encs; |
| 482 |
size_t num_encs; |
| 483 |
mpg123_encodings(&encs, &num_encs); |
| 484 |
nenc = match_enc(outfmt.encoding, encs, num_encs); |
| 485 |
} |
| 486 |
if(nenc) |
| 487 |
outfmt.encoding = nenc; |
| 488 |
else |
| 489 |
{ |
| 490 |
merror( "Found no encoding to match mask 0x%08x." |
| 491 |
, (unsigned int)outfmt.encoding ); |
| 492 |
if(force_fmt) |
| 493 |
error("Perhaps your forced output encoding is not supported."); |
| 494 |
return -1; |
| 495 |
} |
| 496 |
const char *encname = out123_enc_name(outfmt.encoding); |
| 497 |
if(param.verbose > 1) |
| 498 |
for(int ch=MPG123_MONO; ch<=MPG123_STEREO; ++ch) |
| 499 |
if(outfmt.channels & ch) |
| 500 |
fprintf(stderr, "Note: output format %li Hz, %s, %s\n" |
| 501 |
, outfmt.rate, ch==MPG123_MONO ? "mono" : "stereo" |
| 502 |
, encname ? encname : "???" ); |
| 503 |
} |
| 504 |
|
| 505 |
// Either enable or disable rate forcing, whith ntom_rate non-zero or not. |
| 506 |
if(mpg123_param(mh, MPG123_FORCE_RATE, ntom_rate, 0) != MPG123_OK) |
| 507 |
{ |
| 508 |
merror("Cannot force NtoM rate: %s", mpg123_strerror(mh)); |
| 509 |
return -1; |
| 510 |
} |
| 511 |
|
| 512 |
if(ntom_rate) |
| 513 |
{ |
| 514 |
// Only that one rate is enforced. Nothing else needs to be checked. |
| 515 |
// For pitching, ntom_rate has been adjusted. The output uses outfmt.rate. |
| 516 |
// Need to tell mpg123 about the forced rate to make it work. |
| 517 |
for(int ch=1; ch<=2; ++ch) |
| 518 |
{ |
| 519 |
int fmts = out123_encodings(ao, outfmt.rate, ch); |
| 520 |
if(param.verbose > 2) |
| 521 |
fprintf( stderr |
| 522 |
, "Note: output support for %li Hz, %s: 0x%x\n" |
| 523 |
, outfmt.rate, ch==MPG123_MONO ? "mono" : "stereo", fmts ); |
| 524 |
if(force_fmt) |
| 525 |
fmts = ((fmts & force_fmt) == force_fmt) ? force_fmt : 0; |
| 526 |
mpg123_format(mh, ntom_rate, ch, fmts); |
| 527 |
} |
| 528 |
} else if(do_resample) |
| 529 |
{ |
| 530 |
// Support any decoding rate with float output for the resampler and also |
| 531 |
// direct decoding to confiugred output format. |
| 532 |
// One twist: Disable high rates with signal that the resampler will throw |
| 533 |
// away anyway. This includes pitch. 22040 Hz output rate with pitch 0.5 |
| 534 |
// still wants the full 44100 Hz input data, as original signal up to |
| 535 |
// 22040 Hz will be heard as up to 11020 Hz. So we want pitch_rate() |
| 536 |
// to be above outfmt.rate. Final resampling ratio not above 2. |
| 537 |
for(ri=0; ri<num_rates; ++ri) |
| 538 |
{ |
| 539 |
if(rates[ri] > 12000 && pitch_rate(rates[ri]) > outfmt.rate*2) |
| 540 |
break; |
| 541 |
int fmt = (param.pitch == 0. && rates[ri] == outfmt.rate) |
| 542 |
? outfmt.encoding |
| 543 |
: MPG123_ENC_FLOAT_32; |
| 544 |
mpg123_format(mh, rates[ri], outfmt.channels, fmt); |
| 545 |
} |
| 546 |
} else |
| 547 |
{ |
| 548 |
// Finally, the old style, direct decoding to possibly pitched output. |
| 549 |
if(!outrates) |
| 550 |
outrates = malloc(sizeof(*rates)*num_rates); |
| 551 |
if(!unpitch) |
| 552 |
unpitch = malloc(sizeof(*unpitch)*num_rates); |
| 553 |
if(!outrates || !unpitch) |
| 554 |
{ |
| 555 |
CLEAN_POINTER(outrates, free) |
| 556 |
CLEAN_POINTER(unpitch, free) |
| 557 |
error("DOOM"); |
| 558 |
return -1; |
| 559 |
} |
| 560 |
for(ri = 0; ri<num_rates; ri++) |
| 561 |
{ |
| 562 |
decode_rate = rates[ri]; |
| 563 |
outrates[ri] = pitch_rate(decode_rate); |
| 564 |
unpitch[ri].a = outrates[ri]; |
| 565 |
unpitch[ri].b = decode_rate; |
| 566 |
} |
| 567 |
/* Actually query formats possible with given rates. */ |
| 568 |
fmtcount = out123_formats(ao, outrates, num_rates, 1, 2, &outfmts); |
| 569 |
// Remember: First one is a default format, then come my rates. |
| 570 |
if(fmtcount > 0) |
| 571 |
{ |
| 572 |
int fi; |
| 573 |
int unpitch_i = 0; |
| 574 |
if(param.verbose > 1 && outfmts[0].encoding > 0) |
| 575 |
{ |
| 576 |
const char *encname = out123_enc_name(outfmts[0].encoding); |
| 577 |
fprintf(stderr, "Note: default format %li Hz, %i channels, %s\n" |
| 578 |
, outfmts[0].rate, outfmts[0].channels |
| 579 |
, encname ? encname : "???" ); |
| 580 |
} |
| 581 |
for(fi=1; fi<fmtcount; ++fi) |
| 582 |
{ |
| 583 |
int fmts = outfmts[fi].encoding; |
| 584 |
if(param.verbose > 2) |
| 585 |
fprintf( stderr |
| 586 |
, "Note: output support for %li Hz, %i channels: 0x%x\n" |
| 587 |
, outfmts[fi].rate, outfmts[fi].channels, outfmts[fi].encoding ); |
| 588 |
if(force_fmt) |
| 589 |
fmts = ((fmts & force_fmt) == force_fmt) ? force_fmt : 0; |
| 590 |
decode_rate = brate(unpitch, outfmts[fi].rate, num_rates, &unpitch_i); |
| 591 |
mpg123_format(mh, decode_rate, outfmts[fi].channels, fmts); |
| 592 |
} |
| 593 |
} |
| 594 |
free(outfmts); |
| 595 |
} |
| 596 |
|
| 597 |
if(param.verbose > 1) print_capabilities(ao, mh); |
| 598 |
|
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
int set_pitch(mpg123_handle *fr, out123_handle *ao, double new_pitch) |
| 603 |
{ |
| 604 |
double old_pitch = param.pitch; |
| 605 |
long rate; |
| 606 |
int channels, format; |
| 607 |
int smode = 0; |
| 608 |
|
| 609 |
/* Be safe, check support. */ |
| 610 |
if(mpg123_getformat(fr, &rate, &channels, &format) != MPG123_OK) |
| 611 |
{ |
| 612 |
/* We might just not have a track handy. */ |
| 613 |
error("There is no current audio format, cannot apply pitch. This might get fixed in future."); |
| 614 |
return 0; |
| 615 |
} |
| 616 |
|
| 617 |
if(outfmt.rate && !do_resample) |
| 618 |
{ |
| 619 |
error("Runtime pitching requires either proper resampler or flexible hardware rate."); |
| 620 |
return 0; |
| 621 |
} |
| 622 |
|
| 623 |
param.pitch = new_pitch; |
| 624 |
|
| 625 |
if(channels == 1) smode = MPG123_MONO; |
| 626 |
if(channels == 2) smode = MPG123_STEREO; |
| 627 |
|
| 628 |
out123_stop(ao); |
| 629 |
/* Remember: This takes param.pitch into account. */ |
| 630 |
audio_capabilities(ao, fr); |
| 631 |
if(!do_resample && !(mpg123_format_support(fr, rate, format) & smode)) |
| 632 |
{ |
| 633 |
|
| 634 |
/* Note: When using --pitch command line parameter, you can go higher |
| 635 |
because a lower decoder sample rate is automagically chosen. |
| 636 |
Here, we'd need to switch decoder rate during track... good? */ |
| 637 |
error("Reached a hardware limit there with pitch!"); |
| 638 |
param.pitch = old_pitch; |
| 639 |
audio_capabilities(ao, fr); |
| 640 |
} |
| 641 |
else |
| 642 |
mpg123_decoder(fr, NULL); |
| 643 |
return audio_prepare(ao, fr, rate, channels, format); |
| 644 |
} |
| 645 |
|
| 646 |
int set_mute(out123_handle *ao, int mutestate) |
| 647 |
{ |
| 648 |
return out123_param( ao |
| 649 |
, mutestate ? OUT123_ADD_FLAGS : OUT123_REMOVE_FLAGS |
| 650 |
, OUT123_MUTE, 0, NULL ); |
| 651 |
} |