adpcmresample.c
3.48 KB
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
#include <abi.h>
#include <stdio.h>
#include <math.h>
/* Size value come from u-code assembly, locations are arbitrary,
but should be used for load command in simulator.
The command list is written by this program and sits right after
the task header in DRAM, which is loaded at 0x00000000
*/
#define AUD_UCODE_POINTER 0x7000
#define AUD_UCODE_SIZE 4095 /* In bytes */
#define AUD_PDATA_POINTER 0x8000
#define AUD_PDATA_SIZE 1536 /* In bytes */
#define ADPCM_TABLE_LOC 48
#define ADPCM_TABLE_SIZE (8*16)
#define AUD_CL_POINTER (ADPCM_TABLE_LOC + ADPCM_TABLE_SIZE)
short adpcmtable[][16] = {
{0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0,
1920, 1800, 1688, 1582, 1483, 1390, 1304, 1222},
{-1920, -3660, -5177, -6437, -7418, -8105, -8496, -8597,
3904, 5522, 6866, 7912, 8645, 9062, 9170, 8985},
{-1664, -2990, -4021, -4795, -5350, -5717, -5925, -6002,
3680, 4949, 5902, 6584, 7036, 7293, 7388, 7349}
};
#define PSCALE 0x8000
#define PI 3.1415926
main(int argc, char *argv[])
{
Task tlist, *tlistp;
Acmd alist[8192], *alistp;
int w0, w1, i, outpoint, inpoint, pitch, ninput, noutput, dmout;
int clcount;
FILE *outp;
float fninput, delta;
Acmd *adpcmdec();
if((outp = fopen(argv[1], "w")) == NULL){
fprintf(stderr,"Can't open %s\n",argv[1]);
exit(1);
}
tlistp = &tlist;
alistp = alist;
noutput = 352;
pitch = (int) 1.0*PSCALE; /* PSCALE is one */
adpcmsetdram(0, 0); /* Initialize ADPCM object */
adpcminit(2, 0);
inpoint = 0;
outpoint = 0;
aSegment(alistp++, 0, 0xa000); /* Bitstream in DRAM 0x4000 */
aSegment(alistp++, 1, 0x100000); /* Decoded samples in DRAM 0x100000 */
aSegment(alistp++, 2, 0x9fe0); /* Use this for ADPCM state */
aSegment(alistp++, 3, 0x9fc0); /* Use this for RESAMPLE state */
aSegment(alistp++, 4, 0x0);
aLoadADPCM(alistp++, ADPCM_TABLE_SIZE, 4<<26 | ADPCM_TABLE_LOC);
/* Number of input samples */
fninput = (float) noutput * pitch/PSCALE;
ninput = (int) floorf(fninput);
delta = (fninput - ninput);
alistp = adpcmdec(alistp, 0, 1280, ninput, 0, 1, &dmout);
aSetBuffer(alistp++, 0, dmout, 0, noutput<<1);
aSetVolume(alistp++, 0, 32767, 32767, 32767);
aResample(alistp++, A_INIT, pitch, 3<<26);
aSaveBuffer(alistp++, 1<<26 | outpoint);
outpoint += noutput<<1;
for (i=0; i<200; i++){
/* Update pitch */
pitch = (int) PSCALE * (0.1*cos(2*PI*i*noutput/44100) + 0.75);
fninput = delta + (float) noutput * pitch/PSCALE;
ninput = (int) floorf(fninput);
delta = (fninput - ninput);
alistp = adpcmdec(alistp, 0, 1280, ninput, 0, 1, &dmout);
aSetBuffer(alistp++, 0, dmout, 0, noutput<<1);
aResample(alistp++, 0, pitch, 3<<26);
aSaveBuffer(alistp++, 1<<26 | outpoint);
outpoint += noutput<<1;
}
clcount = (int) (alistp - alist);
alistp = alist;
tlistp->t.dl = AUD_CL_POINTER;
tlistp->t.len = clcount*8;
tlistp->t.type = M_AUDTASK | ((AUD_UCODE_SIZE)<<12);
tlistp->t.ucode = (unsigned long *) AUD_UCODE_POINTER;
tlistp->t.ucode_data = (unsigned long *) AUD_PDATA_POINTER;
tlistp->t.ucode_data_len = AUD_PDATA_SIZE;
fwrite(tlistp, 1, 48, outp);
fwrite(adpcmtable, 1, ADPCM_TABLE_SIZE, outp);
for (i=0; i<clcount; i++){
w0 = alistp->words.w0;
w1 = alistp->words.w1;
fwrite(&w0, 1, 4, outp);
fwrite(&w1, 1, 4, outp);
alistp++;
}
fclose(outp);
fprintf(stderr,"Command list length: %d\n",clcount);
}