| 1 |
#include "config.h" |
| 2 |
#define SYN123_PORTABLE_API |
| 3 |
#include <syn123.h> |
| 4 |
#include <out123.h> |
| 5 |
|
| 6 |
#include "../compat/compat.h" |
| 7 |
|
| 8 |
static long block_rate(long rate, double speed, double factor, off_t bi) |
| 9 |
{ |
| 10 |
static long maxrate = -1; |
| 11 |
if(maxrate < 0) |
| 12 |
maxrate = syn123_resample_maxrate(); |
| 13 |
double foutrate = (double)rate/(speed*pow(factor, bi)); |
| 14 |
long outrate = foutrate > maxrate ? maxrate : (long)foutrate; |
| 15 |
return outrate > 0 ? outrate : 1; |
| 16 |
} |
| 17 |
|
| 18 |
int main(int argc, char **argv) |
| 19 |
{ |
| 20 |
int ret = 0; |
| 21 |
if(argc < 7) |
| 22 |
{ |
| 23 |
fprintf( stderr |
| 24 |
, "Usage: %s <rate> <freq> <block> <speed> <speedfactor> <smooth>" |
| 25 |
" [duration [outfile]]\n" |
| 26 |
, argv[0] ); |
| 27 |
return 1; |
| 28 |
} |
| 29 |
long rate = atol(argv[1]); |
| 30 |
double freq = atof(argv[2]); |
| 31 |
long block = atol(argv[3]); |
| 32 |
double speed = atof(argv[4]); |
| 33 |
double factor = atof(argv[5]); |
| 34 |
if(factor < 1e-10) |
| 35 |
{ |
| 36 |
fprintf(stderr, "Funny. Give me a speed factor properly > 0!\n"); |
| 37 |
return -6; |
| 38 |
} |
| 39 |
int smooth = atoi(argv[6]); |
| 40 |
double duration = argc > 7 ? atof(argv[7]) : 5; |
| 41 |
// Trivially ensuring that it's > 0, playing a bit longer than required. |
| 42 |
// Could cut things in the end, if I really care. |
| 43 |
off_t out_limit = (off_t)(duration*rate)/block+1; |
| 44 |
char *outfile = argc > 8 ? argv[8] : NULL; |
| 45 |
syn123_handle *syn = syn123_new(rate, 1, MPG123_ENC_FLOAT_32, 0, NULL); |
| 46 |
if(!syn) |
| 47 |
return -1; |
| 48 |
out123_handle *out = out123_new(); |
| 49 |
if(!out) |
| 50 |
return -2; |
| 51 |
if(out123_open(out, outfile ? "wav" : NULL, outfile)) |
| 52 |
return -3; |
| 53 |
if(out123_start(out, rate, 1, MPG123_ENC_FLOAT_32)) |
| 54 |
return -3; |
| 55 |
if(syn123_setup_waves(syn, 1, NULL, &freq, NULL, NULL, NULL)) |
| 56 |
return -4; |
| 57 |
off_t bi = 0; |
| 58 |
// Biggest output rate to plan for. |
| 59 |
long firstrate = block_rate(rate, speed, factor, 0); |
| 60 |
long lastrate = block_rate(rate, speed, factor, (out_limit-1)); |
| 61 |
long maxrate = firstrate > lastrate ? firstrate : lastrate; |
| 62 |
long minrate = firstrate < lastrate ? firstrate : lastrate; |
| 63 |
fprintf(stderr, "rate %ld to [%ld;%ld]\n", rate, minrate, maxrate); |
| 64 |
// Need to add 1 to incount to account for rates in between. |
| 65 |
// A slightly lower rate than maxrate might cause an increase in |
| 66 |
// the max incount for that rate, resulting in _more_ outcount |
| 67 |
// than the lower incount for the maxrate. |
| 68 |
size_t maxoutblock = syn123_resample_count( rate, maxrate |
| 69 |
, syn123_resample_incount(rate, maxrate, block)+1 ); |
| 70 |
size_t maxinblock = syn123_resample_incount(rate, minrate, block); |
| 71 |
fprintf( stderr, "maxinblock %zu, maxoutblock %zu\n" |
| 72 |
, maxinblock, maxoutblock ); |
| 73 |
if(!maxoutblock || !maxinblock) |
| 74 |
return -7; |
| 75 |
float *outbuf = malloc(sizeof(float)*maxoutblock); |
| 76 |
float *inbuf = malloc(sizeof(float)*maxinblock); |
| 77 |
if(!outbuf || !inbuf) |
| 78 |
{ |
| 79 |
fprintf(stderr, "D'OOM!\n"); |
| 80 |
return -13; |
| 81 |
} |
| 82 |
|
| 83 |
off_t intotal = 0; |
| 84 |
off_t outtotal = 0; |
| 85 |
while(bi < out_limit) |
| 86 |
{ |
| 87 |
long outrate = block_rate(rate, speed, factor, bi); |
| 88 |
fprintf(stderr, "block %" PRIiMAX " rate %ld\n", (intmax_t)bi, outrate); |
| 89 |
if(syn123_setup_resample(syn, rate, outrate, 1, 0, smooth)) |
| 90 |
{ |
| 91 |
ret = -11; |
| 92 |
break; |
| 93 |
} |
| 94 |
// Determine how many input samples to feed to get block output samples. |
| 95 |
int err; |
| 96 |
size_t inblock = syn123_resample_in(syn, block, &err); |
| 97 |
if(err || inblock <= 0 || inblock > maxinblock) |
| 98 |
{ |
| 99 |
fprintf(stderr, "bad inblock: %zu (%i)\n", inblock, err); |
| 100 |
ret = -15; |
| 101 |
break; |
| 102 |
} |
| 103 |
size_t outblock = syn123_resample_out(syn, inblock, &err); |
| 104 |
fprintf(stderr, "in %zu out %zu\n", inblock, outblock); |
| 105 |
if(err || outblock <= 0 || outblock > maxoutblock || outblock < block) |
| 106 |
{ |
| 107 |
fprintf(stderr, "bad outblock: %zu (%d)\n", outblock, err); |
| 108 |
ret = -16; |
| 109 |
break; |
| 110 |
} |
| 111 |
size_t bytes = syn123_read(syn, inbuf, sizeof(float)*inblock); |
| 112 |
if(bytes != sizeof(float)*inblock) |
| 113 |
{ |
| 114 |
ret = -14; |
| 115 |
break; |
| 116 |
} |
| 117 |
size_t outsamples = syn123_resample(syn, outbuf, inbuf, inblock); |
| 118 |
if(outsamples != outblock) |
| 119 |
{ |
| 120 |
fprintf( stderr, "output prediction wrong in block %" |
| 121 |
PRIiMAX": %zu != %zu\n", bi, outsamples, outblock ); |
| 122 |
ret = -17; |
| 123 |
break; |
| 124 |
} |
| 125 |
intotal += inblock; |
| 126 |
outtotal += outblock; |
| 127 |
//fprintf( stderr, "ratio: %f %" PRIiMAX ":%" PRIiMAX " to %" PRIiMAX ":%" PRIiMAX "\n" |
| 128 |
//, (double)intotal/outtotal, (intmax_t)intotal |
| 129 |
//, (intmax_t)syn123_resample_intotal(rate, outrate, outtotal) |
| 130 |
//, (intmax_t)outtotal, (intmax_t)syn123_resample_total(rate, outrate, intotal) ); |
| 131 |
syn123_amp(outbuf, MPG123_ENC_FLOAT_32, outsamples, syn123_db2lin(-12), 0, NULL, NULL); |
| 132 |
if(out123_play(out, outbuf, sizeof(float)*outsamples) != sizeof(float)*outsamples) |
| 133 |
{ |
| 134 |
ret = -10; |
| 135 |
break; |
| 136 |
} |
| 137 |
#if 0 |
| 138 |
float spacer[100]; |
| 139 |
if(outsamples) |
| 140 |
{ |
| 141 |
for(unsigned int i=0; i<sizeof(spacer)/sizeof(float); ++i) |
| 142 |
spacer[i] = outbuf[outsamples-1]; |
| 143 |
} |
| 144 |
else |
| 145 |
memset(spacer, 0, sizeof(spacer)); |
| 146 |
out123_play(out, spacer, sizeof(spacer)); |
| 147 |
#endif |
| 148 |
++bi; |
| 149 |
} |
| 150 |
free(outbuf); |
| 151 |
free(inbuf); |
| 152 |
out123_del(out); |
| 153 |
syn123_del(syn); |
| 154 |
return ret; |
| 155 |
} |