| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <stdint.h> |
| 4 |
#include <string.h> |
| 5 |
#include "bs_codec.h" |
| 6 |
#include "oki_codec.h" |
| 7 |
#include "yma_codec.h" |
| 8 |
#include "ymb_codec.h" |
| 9 |
#include "ymz_codec.h" |
| 10 |
|
| 11 |
void encode(uint8_t *source,uint8_t *dest,long length,int adpcm_mode) |
| 12 |
{ |
| 13 |
if(adpcm_mode == 0) |
| 14 |
bs_encode((int16_t*)source,dest,length); |
| 15 |
else if(adpcm_mode == 1) |
| 16 |
ymz_encode((int16_t*)source,dest,length); |
| 17 |
else if(adpcm_mode == 2) |
| 18 |
oki_encode((int16_t*)source,dest,length); |
| 19 |
else if(adpcm_mode == 3) |
| 20 |
oki6258_encode((int16_t*)source,dest,length); |
| 21 |
else if(adpcm_mode == 4) |
| 22 |
yma_encode((int16_t*)source,dest,length); |
| 23 |
else if(adpcm_mode == 5) |
| 24 |
ymb_encode((int16_t*)source,dest,length); |
| 25 |
else if(adpcm_mode == 6) |
| 26 |
aica_encode((int16_t*)source,dest,length); |
| 27 |
else |
| 28 |
exit(-1); |
| 29 |
} |
| 30 |
|
| 31 |
void decode(uint8_t *source,uint8_t *dest,long length,int adpcm_mode) |
| 32 |
{ |
| 33 |
if(adpcm_mode == 0) |
| 34 |
bs_decode(source,(int16_t*)dest,length); |
| 35 |
else if(adpcm_mode == 1) |
| 36 |
ymz_decode(source,(int16_t*)dest,length); |
| 37 |
else if(adpcm_mode == 2) |
| 38 |
oki_decode(source,(int16_t*)dest,length); |
| 39 |
else if(adpcm_mode == 3) |
| 40 |
oki6258_decode(source,(int16_t*)dest,length); |
| 41 |
else if(adpcm_mode == 4) |
| 42 |
yma_decode(source,(int16_t*)dest,length); |
| 43 |
else if(adpcm_mode == 5) |
| 44 |
ymb_decode(source,(int16_t*)dest,length); |
| 45 |
else if(adpcm_mode == 6) |
| 46 |
aica_decode(source,(int16_t*)dest,length); |
| 47 |
else |
| 48 |
exit(-1); |
| 49 |
} |
| 50 |
|
| 51 |
int main(int argc, char* argv []) |
| 52 |
{ |
| 53 |
|
| 54 |
unsigned long i; |
| 55 |
|
| 56 |
printf("PCM <-> ADPCM conversion tool\n"); |
| 57 |
printf("2018-2022 by ctr\n"); |
| 58 |
printf("Input format: signed 16 bit PCM little endian\n"); |
| 59 |
|
| 60 |
if(argc<4) |
| 61 |
{ |
| 62 |
printf("Usage: <s|o|x|a|b|c|z><d|e> <file> <destination file> \n"); |
| 63 |
printf("List of codecs:\n"); |
| 64 |
printf("\ts - Brian Schmidt (BSMT2000 / QSound)\n"); |
| 65 |
printf("\to - Oki/Dialogic VOX (MSM6295)\n"); |
| 66 |
printf("\tx - Oki X68000 (MSM6258)\n"); |
| 67 |
printf("\ta - Yamaha ADPCM-A (YM2610)\n"); |
| 68 |
printf("\tb - Yamaha ADPCM-B (Y8950 / YM2608 / YM2610)\n"); |
| 69 |
printf("\tc - Yamaha AICA (AICA)\n"); |
| 70 |
printf("\tz - Yamaha / Creative (YMZ280B)\n"); |
| 71 |
exit(EXIT_FAILURE); |
| 72 |
} |
| 73 |
|
| 74 |
char* mode = argv[1]; |
| 75 |
char* file1 = argv[2]; |
| 76 |
char* file2 = argv[3]; |
| 77 |
|
| 78 |
int res; |
| 79 |
|
| 80 |
FILE* sourcefile; |
| 81 |
/* Load sample file */ |
| 82 |
sourcefile = fopen(file1,"rb"); |
| 83 |
if(!sourcefile) |
| 84 |
{ |
| 85 |
printf("Could not open %s\n",file1); |
| 86 |
exit(EXIT_FAILURE); |
| 87 |
} |
| 88 |
fseek(sourcefile,0,SEEK_END); |
| 89 |
unsigned long sourcefile_size = ftell(sourcefile); |
| 90 |
rewind(sourcefile); |
| 91 |
uint8_t* source = (uint8_t*)malloc(sourcefile_size+1); |
| 92 |
uint8_t* dest = (uint8_t*)malloc(sourcefile_size*4+1); |
| 93 |
res = fread(source,1,sourcefile_size,sourcefile); |
| 94 |
if(res != sourcefile_size) |
| 95 |
{ |
| 96 |
printf("Reading error\n"); |
| 97 |
exit(EXIT_FAILURE); |
| 98 |
} |
| 99 |
fclose(sourcefile); |
| 100 |
|
| 101 |
int length; |
| 102 |
int adpcm_mode = -1; |
| 103 |
|
| 104 |
if(*mode == 's') |
| 105 |
{ |
| 106 |
adpcm_mode = 0; |
| 107 |
printf("Using Brian Schmidt algorithm\n"); |
| 108 |
mode++; |
| 109 |
} |
| 110 |
else if(*mode == 'z') |
| 111 |
{ |
| 112 |
adpcm_mode = 1; |
| 113 |
printf("Using Yamaha YMZ280B algorithm\n"); |
| 114 |
mode++; |
| 115 |
} |
| 116 |
else if(*mode == 'o') |
| 117 |
{ |
| 118 |
adpcm_mode = 2; |
| 119 |
printf("Using OKI/Dialogic VOX (MSM6295) algorithm\n"); |
| 120 |
mode++; |
| 121 |
} |
| 122 |
else if(*mode == 'x') |
| 123 |
{ |
| 124 |
adpcm_mode = 3; |
| 125 |
printf("Using OKI X68000 (MSM6258) algorithm\n"); |
| 126 |
mode++; |
| 127 |
} |
| 128 |
else if(*mode == 'a') |
| 129 |
{ |
| 130 |
adpcm_mode = 4; |
| 131 |
printf("Using Yamaha ADPCM-A algorithm\n"); |
| 132 |
mode++; |
| 133 |
} |
| 134 |
else if(*mode == 'b') |
| 135 |
{ |
| 136 |
adpcm_mode = 5; |
| 137 |
printf("Using Yamaha ADPCM-B algorithm\n"); |
| 138 |
mode++; |
| 139 |
} |
| 140 |
else if(*mode == 'c') |
| 141 |
{ |
| 142 |
adpcm_mode = 6; |
| 143 |
printf("Using Yamaha AICA algorithm\n"); |
| 144 |
mode++; |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
printf("Please specify algorithm\n"); |
| 149 |
exit(-1); |
| 150 |
} |
| 151 |
|
| 152 |
if(*mode == 'e') |
| 153 |
{ |
| 154 |
length = sourcefile_size/2; |
| 155 |
if(length & 1) |
| 156 |
length++; // make the size even |
| 157 |
printf("encoding... (len= %d)\n",length); |
| 158 |
encode(source,dest,length,adpcm_mode); |
| 159 |
length /= 2; |
| 160 |
} |
| 161 |
|
| 162 |
else if(*mode == 'd') |
| 163 |
{ |
| 164 |
length = sourcefile_size*2; |
| 165 |
printf("decoding... (len= %d)\n",length); |
| 166 |
decode(source,dest,length,adpcm_mode); |
| 167 |
length *= 2; |
| 168 |
} |
| 169 |
|
| 170 |
FILE *destfile; |
| 171 |
destfile = fopen(file2,"wb"); |
| 172 |
if(!destfile) |
| 173 |
{ |
| 174 |
printf("Could not open %s\n",file2); |
| 175 |
exit(EXIT_FAILURE); |
| 176 |
} |
| 177 |
for(i=0;i<length;i++) |
| 178 |
{ |
| 179 |
putc(*((uint8_t*)dest+i),destfile); |
| 180 |
} |
| 181 |
fclose(destfile); |
| 182 |
printf("write ok %lu\n",length); |
| 183 |
|
| 184 |
free(source); |
| 185 |
} |