-
Notifications
You must be signed in to change notification settings - Fork 33
/
audio.c
163 lines (123 loc) · 4.09 KB
/
audio.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
/*
Copyright 2014 StapleButter
This file is part of blargSnes.
blargSnes is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
blargSnes is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with blargSnes. If not, see http://www.gnu.org/licenses/.
*/
#include <3ds.h>
#include "dsp.h"
#include "mixrate.h"
// 0 = none, 1 = CSND, 2 = DSP
int Audio_Type;
s16* Audio_Buffer0;
s16* Audio_Buffer1;
s16* Audio_Buffer;
u8 curbuffer = 0;
u32 cursample = 0;
u32 curpos = 0;
void Audio_Init()
{
Result res;
Audio_Type = 0;
Audio_Buffer0 = (s16*)linearAlloc(MIXBUFSIZE*4*4);
Audio_Buffer1 = &Audio_Buffer0[MIXBUFSIZE*4];
memset(Audio_Buffer0, 0, MIXBUFSIZE*4*4*sizeof(s16));
curbuffer = 0;
Audio_Buffer = Audio_Buffer0;
cursample = 0;
curpos = 0;
// try using CSND
res = CSND_initialize(NULL);
if (!res)
{
Audio_Type = 1;
}
// TODO: DSP black magic
}
void Audio_DeInit()
{
if (Audio_Type == 1)
{
CSND_setchannel_playbackstate(8, 0);
CSND_setchannel_playbackstate(9, 0);
CSND_setchannel_playbackstate(10, 0);
CSND_setchannel_playbackstate(11, 0);
CSND_sharedmemtype0_cmdupdatestate(0);
CSND_shutdown();
}
}
void Audio_Pause()
{
// stop
if (Audio_Type == 1)
{
CSND_setchannel_playbackstate(8, 0);
CSND_setchannel_playbackstate(9, 0);
CSND_setchannel_playbackstate(10, 0);
CSND_setchannel_playbackstate(11, 0);
CSND_sharedmemtype0_cmdupdatestate(0);
}
memset(Audio_Buffer0, 0, MIXBUFSIZE*4*4*sizeof(s16));
GSPGPU_FlushDataCache(NULL, Audio_Buffer0, MIXBUFSIZE*4*4*sizeof(s16));
}
void Audio_Mix()
{
DspMixSamplesStereo(DSPMIXBUFSIZE, &Audio_Buffer[cursample]);
cursample += DSPMIXBUFSIZE;
cursample &= ((MIXBUFSIZE << 1) - 1);
}
// volume control
void myCSND_sharedmemtype0_cmd9(u32 channel, u16 leftvol, u16 rightvol)
{
u32 cmdparams[0x18>>2];
memset(cmdparams, 0, 0x18);
cmdparams[0] = channel & 0x1f;
cmdparams[1] = leftvol | (rightvol<<16);
CSND_writesharedmem_cmdtype0(0x9, (u8*)&cmdparams);
}
// tweaked CSND_playsound() version. Allows setting multiple channels and calling updatestate once.
// the last two parameters are also repurposed for volume control
void myCSND_playsound(u32 channel, u32 looping, u32 encoding, u32 samplerate, u32 *vaddr0, u32 *vaddr1, u32 totalbytesize, u32 leftvol, u32 rightvol)
{
u32 physaddr0 = 0;
u32 physaddr1 = 0;
physaddr0 = osConvertVirtToPhys((u32)vaddr0);
physaddr1 = osConvertVirtToPhys((u32)vaddr1);
CSND_sharedmemtype0_cmde(channel, looping, encoding, samplerate, 2/*unk0*/, 1/*unk1*/, physaddr0, physaddr1, totalbytesize);
CSND_sharedmemtype0_cmd8(channel, samplerate);
if(looping)
{
if(physaddr1>physaddr0)totalbytesize -= (u32)physaddr1 - (u32)physaddr0;
CSND_sharedmemtype0_cmd3(channel, physaddr1, totalbytesize);
}
CSND_sharedmemtype0_cmd8(channel, samplerate);
myCSND_sharedmemtype0_cmd9(channel, leftvol, rightvol); // volume
CSND_setchannel_playbackstate(channel, 1);
}
void Audio_MixFinish()
{
GSPGPU_FlushDataCache(NULL, Audio_Buffer, MIXBUFSIZE*4*4*sizeof(s16));
curpos++;
if (curpos >= (MIXBUFSIZE/256))
{
if (Audio_Type == 1)
{
int newbuffer = curbuffer^1;
myCSND_playsound(8+newbuffer, 1, CSND_ENCODING_PCM16, 32000, (u32*)&Audio_Buffer[0], (u32*)&Audio_Buffer[(MIXBUFSIZE*2)-1], MIXBUFSIZE*4, 0xFFFF, 0);
myCSND_playsound(10+newbuffer, 1, CSND_ENCODING_PCM16, 32000, (u32*)&Audio_Buffer[MIXBUFSIZE*2], (u32*)&Audio_Buffer[(MIXBUFSIZE*4)-1], MIXBUFSIZE*4, 0, 0xFFFF);
CSND_setchannel_playbackstate(8+curbuffer, 0);
CSND_setchannel_playbackstate(10+curbuffer, 0);
CSND_sharedmemtype0_cmdupdatestate(0);
}
curbuffer ^= 1;
Audio_Buffer = curbuffer ? Audio_Buffer1 : Audio_Buffer0;
curpos = 0;
}
}