| 1 |
/* |
| 2 |
hextxt: hex or printf text output (ASCII/UTF-8) |
| 3 |
|
| 4 |
copyright 2017-2023 by the mpg123 project |
| 5 |
free software under the terms of the LGPL 2.1 |
| 6 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 7 |
initially written by Thomas Orgis |
| 8 |
|
| 9 |
This just adds something to directly compare with FhG sample |
| 10 |
outputs (hex) or to lazily pipe to a plotter (text, one column per |
| 11 |
channel). |
| 12 |
|
| 13 |
Hex mode supports all possible integer encodings (where I am resonably |
| 14 |
sure that endianess is defined). For text output, printing floats is |
| 15 |
no problem. Printout of high channel counts as well as 24 bit numbers |
| 16 |
is probably rather slow as individual numbers are pushed into a fprintf |
| 17 |
call each. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "out123_int.h" |
| 21 |
#include "hextxt.h" |
| 22 |
#include "../common/debug.h" |
| 23 |
|
| 24 |
int hex_formats(out123_handle *ao) |
| 25 |
{ |
| 26 |
return MPG123_ENC_ANY; |
| 27 |
} |
| 28 |
|
| 29 |
// This is about the same list as the above, but in case |
| 30 |
// any new format is added, the text printout will need |
| 31 |
// explicit changes, while hex printout does only care |
| 32 |
// about a sample's byte count. |
| 33 |
int txt_formats(out123_handle *ao) |
| 34 |
{ |
| 35 |
return |
| 36 |
MPG123_ENC_SIGNED_8 |
| 37 |
| MPG123_ENC_UNSIGNED_8 |
| 38 |
| MPG123_ENC_ULAW_8 // Printed as unsigned integer. |
| 39 |
| MPG123_ENC_ALAW_8 // Printed as unsigned integer. |
| 40 |
| MPG123_ENC_SIGNED_16 |
| 41 |
| MPG123_ENC_UNSIGNED_16 |
| 42 |
| MPG123_ENC_SIGNED_24 |
| 43 |
| MPG123_ENC_UNSIGNED_24 |
| 44 |
| MPG123_ENC_SIGNED_32 |
| 45 |
| MPG123_ENC_UNSIGNED_32 |
| 46 |
| MPG123_ENC_FLOAT_32 |
| 47 |
| MPG123_ENC_FLOAT_64; |
| 48 |
} |
| 49 |
|
| 50 |
static FILE* open_file(const char *path) |
| 51 |
{ |
| 52 |
if(!path || !strcmp("-",path) || !strcmp("",path)) |
| 53 |
return stdout; |
| 54 |
else |
| 55 |
return INT123_compat_fopen(path, "w"); |
| 56 |
} |
| 57 |
|
| 58 |
/* Hex output defaults to what FhG compliance files used. */ |
| 59 |
int hex_open(out123_handle *ao) |
| 60 |
{ |
| 61 |
if(ao->format < 0) |
| 62 |
{ |
| 63 |
ao->rate = 44100; |
| 64 |
ao->channels = 1; |
| 65 |
ao->format = MPG123_ENC_SIGNED_24; |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
ao->userptr = open_file(ao->device); |
| 70 |
return ao->userptr ? 0 : -1; |
| 71 |
} |
| 72 |
|
| 73 |
/* Text output defaults to the usual, CD-like format. */ |
| 74 |
int txt_open(out123_handle *ao) |
| 75 |
{ |
| 76 |
if(ao->format < 0) |
| 77 |
{ |
| 78 |
ao->rate = 44100; |
| 79 |
ao->channels = 2; |
| 80 |
ao->format = MPG123_ENC_SIGNED_16; |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
ao->userptr = ( (ao->format & txt_formats(ao)) == ao->format) |
| 85 |
? open_file(ao->device) |
| 86 |
: NULL; |
| 87 |
return ao->userptr ? 0 : -1; |
| 88 |
} |
| 89 |
|
| 90 |
int hextxt_close(out123_handle *ao) |
| 91 |
{ |
| 92 |
if(ao && ao->userptr) |
| 93 |
{ |
| 94 |
FILE *fp = ao->userptr; |
| 95 |
ao->userptr = NULL; |
| 96 |
if(fp != stdout && INT123_compat_fclose(fp)) |
| 97 |
{ |
| 98 |
if(!AOQUIET) |
| 99 |
error1("problem closing the output: %s\n", INT123_strerror(errno)); |
| 100 |
return -1; |
| 101 |
} |
| 102 |
} |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
int hex_write(out123_handle *ao, unsigned char *buf, int len) |
| 107 |
{ |
| 108 |
FILE *fp; |
| 109 |
int i, block, samples; |
| 110 |
|
| 111 |
if(!ao || !ao->userptr) |
| 112 |
return -1; |
| 113 |
fp = ao->userptr; |
| 114 |
|
| 115 |
block = out123_encsize(ao->format); |
| 116 |
samples = len/block; |
| 117 |
for(i=0; i<samples; ++i) |
| 118 |
{ |
| 119 |
unsigned char *s = &buf[i*block]; |
| 120 |
/* Printout always big-endian, beginning with the highest byte. */ |
| 121 |
switch(block) |
| 122 |
{ |
| 123 |
#ifdef WORDS_BIGENDIAN |
| 124 |
case 1: |
| 125 |
fprintf(fp, "%02x\n", s[0]); |
| 126 |
break; |
| 127 |
case 2: |
| 128 |
fprintf(fp, "%02x%02x\n", s[0], s[1]); |
| 129 |
break; |
| 130 |
case 3: |
| 131 |
fprintf(fp, "%02x%02x%02x\n", s[0], s[1], s[2]); |
| 132 |
break; |
| 133 |
case 4: |
| 134 |
fprintf(fp, "%02x%02x%02x%02x\n", s[0], s[1], s[2], s[3]); |
| 135 |
break; |
| 136 |
#else |
| 137 |
case 1: |
| 138 |
fprintf(fp, "%02x\n", s[0]); |
| 139 |
break; |
| 140 |
case 2: |
| 141 |
fprintf(fp, "%02x%02x\n", s[1], s[0]); |
| 142 |
break; |
| 143 |
case 3: |
| 144 |
fprintf(fp, "%02x%02x%02x\n", s[2], s[1], s[0]); |
| 145 |
break; |
| 146 |
case 4: |
| 147 |
fprintf(fp, "%02x%02x%02x%02x\n", s[3], s[2], s[1], s[0]); |
| 148 |
break; |
| 149 |
#endif |
| 150 |
} |
| 151 |
} |
| 152 |
return i*block; |
| 153 |
} |
| 154 |
|
| 155 |
int txt_write(out123_handle *ao, unsigned char *buf, int len) |
| 156 |
{ |
| 157 |
FILE *fp; |
| 158 |
int i, block, frames; |
| 159 |
|
| 160 |
if(!ao || !ao->userptr) |
| 161 |
return -1; |
| 162 |
fp = ao->userptr; |
| 163 |
|
| 164 |
block = ao->framesize; |
| 165 |
frames = len/block; |
| 166 |
|
| 167 |
for(i=0; i<frames; ++i) |
| 168 |
{ |
| 169 |
void *f = &buf[i*block]; |
| 170 |
switch(ao->format) |
| 171 |
{ |
| 172 |
#define CHANPRINT(type, ptype, fmt) \ |
| 173 |
{ \ |
| 174 |
type *ff = f; \ |
| 175 |
switch(ao->channels) \ |
| 176 |
{ \ |
| 177 |
case 1: \ |
| 178 |
fprintf(fp, fmt"\n", (ptype)ff[0]); \ |
| 179 |
break; \ |
| 180 |
case 2: \ |
| 181 |
fprintf(fp, fmt"\t"fmt"\n", (ptype)ff[0], (ptype)ff[1]); \ |
| 182 |
break; \ |
| 183 |
default: \ |
| 184 |
{ \ |
| 185 |
int c; \ |
| 186 |
for(c=0; c<ao->channels; ++c) \ |
| 187 |
fprintf(fp, "%s"fmt, c ? "\t" : "", (ptype)ff[c]); \ |
| 188 |
fprintf(fp, "\n"); \ |
| 189 |
} \ |
| 190 |
} \ |
| 191 |
} |
| 192 |
/* Am I to paranoid about dancing around the type system? */ |
| 193 |
#ifdef WORDS_BIGENDIAN |
| 194 |
#define CHANPRINT24(type, ptype, fmt) \ |
| 195 |
{ \ |
| 196 |
unsigned char *ff = f; \ |
| 197 |
for(int c=0; c<ao->channels; ++c) \ |
| 198 |
{ \ |
| 199 |
uint32_t tmp = 0; \ |
| 200 |
tmp |= ((uint32_t)(*(ff++))) << 24; \ |
| 201 |
tmp |= ((uint32_t)(*(ff++))) << 16; \ |
| 202 |
tmp |= ((uint32_t)(*(ff++))) << 8; \ |
| 203 |
fprintf(fp, "%s"fmt, c ? "\t" : "", (ptype)(*((type*)&tmp)/256)); \ |
| 204 |
} \ |
| 205 |
fprintf(fp, "\n"); \ |
| 206 |
} |
| 207 |
#else |
| 208 |
#define CHANPRINT24(type, ptype, fmt) \ |
| 209 |
{ \ |
| 210 |
unsigned char *ff = f; \ |
| 211 |
for(int c=0; c<ao->channels; ++c) \ |
| 212 |
{ \ |
| 213 |
uint32_t tmp = 0; \ |
| 214 |
tmp |= ((uint32_t)(*(ff++))) << 8; \ |
| 215 |
tmp |= ((uint32_t)(*(ff++))) << 16; \ |
| 216 |
tmp |= ((uint32_t)(*(ff++))) << 24; \ |
| 217 |
fprintf(fp, "%s"fmt, c ? "\t" : "", (ptype)(*((type*)&tmp)/256)); \ |
| 218 |
} \ |
| 219 |
fprintf(fp, "\n"); \ |
| 220 |
} |
| 221 |
#endif |
| 222 |
case MPG123_ENC_SIGNED_8: |
| 223 |
CHANPRINT(char, int, "%d") |
| 224 |
break; |
| 225 |
case MPG123_ENC_UNSIGNED_8: |
| 226 |
case MPG123_ENC_ULAW_8: |
| 227 |
case MPG123_ENC_ALAW_8: |
| 228 |
CHANPRINT(unsigned char, int, "%u") |
| 229 |
break; |
| 230 |
case MPG123_ENC_SIGNED_16: |
| 231 |
CHANPRINT(int16_t, int, "%d") |
| 232 |
break; |
| 233 |
case MPG123_ENC_UNSIGNED_16: |
| 234 |
CHANPRINT(uint16_t, unsigned int, "%u") |
| 235 |
break; |
| 236 |
case MPG123_ENC_SIGNED_24: |
| 237 |
CHANPRINT24(int32_t, long, "%ld") |
| 238 |
break; |
| 239 |
case MPG123_ENC_UNSIGNED_24: |
| 240 |
CHANPRINT24(uint32_t, unsigned long, "%ld") |
| 241 |
break; |
| 242 |
case MPG123_ENC_SIGNED_32: |
| 243 |
CHANPRINT(int32_t, long, "%ld") |
| 244 |
break; |
| 245 |
case MPG123_ENC_UNSIGNED_32: |
| 246 |
CHANPRINT(uint32_t, unsigned long, "%lu") |
| 247 |
break; |
| 248 |
case MPG123_ENC_FLOAT_32: |
| 249 |
CHANPRINT(float, double, "%e") |
| 250 |
break; |
| 251 |
case MPG123_ENC_FLOAT_64: |
| 252 |
CHANPRINT(double, double, "%e") |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
return i*block; |
| 257 |
} |
| 258 |
|
| 259 |
/* Draining is flushing to disk. Words do suck at times. */ |
| 260 |
void hextxt_drain(out123_handle *ao) |
| 261 |
{ |
| 262 |
if(!ao || !ao->userptr) |
| 263 |
return; |
| 264 |
if(fflush(ao->userptr) && !AOQUIET) |
| 265 |
error1("flushing failed: %s\n", INT123_strerror(errno)); |
| 266 |
} |