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