Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/android/jni/sndaudiotrack.c
2 views
1
/* Copyright 2012 Guillaume Duhamel
2
Copyright 2005-2006 Theo Berkau
3
4
This file is part of Yabause.
5
6
Yabause is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
11
Yabause is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with Yabause; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#include <jni.h>
22
#include <android/log.h>
23
#include "sndaudiotrack.h"
24
#include "debug.h"
25
26
static int SNDAudioTrackInit(void);
27
static void SNDAudioTrackDeInit(void);
28
static int SNDAudioTrackReset(void);
29
static int SNDAudioTrackChangeVideoFormat(int vertfreq);
30
static void SNDAudioTrackUpdateAudio(u32 *leftchanbuffer, u32 *rightchanbuffer, u32 num_samples);
31
static u32 SNDAudioTrackGetAudioSpace(void);
32
static void SNDAudioTrackMuteAudio(void);
33
static void SNDAudioTrackUnMuteAudio(void);
34
static void SNDAudioTrackSetVolume(int volume);
35
36
SoundInterface_struct SNDAudioTrack = {
37
SNDCORE_AUDIOTRACK,
38
"Audio Track Sound Interface",
39
SNDAudioTrackInit,
40
SNDAudioTrackDeInit,
41
SNDAudioTrackReset,
42
SNDAudioTrackChangeVideoFormat,
43
SNDAudioTrackUpdateAudio,
44
SNDAudioTrackGetAudioSpace,
45
SNDAudioTrackMuteAudio,
46
SNDAudioTrackUnMuteAudio,
47
SNDAudioTrackSetVolume
48
};
49
50
extern JavaVM * yvm;
51
52
jobject gtrack = NULL;
53
54
jclass cAudioTrack = NULL;
55
56
jmethodID mWrite = NULL;
57
58
int mbufferSizeInBytes;
59
60
static u16 *stereodata16;
61
static u8 soundvolume;
62
static u8 soundmaxvolume;
63
static u8 soundbufsize;
64
static int soundoffset;
65
66
//////////////////////////////////////////////////////////////////////////////
67
68
static int SNDAudioTrackInit(void)
69
{
70
int sampleRateInHz = 44100;
71
int channelConfig = 12; //AudioFormat.CHANNEL_OUT_STEREO
72
int audioFormat = 2; //AudioFormat.ENCODING_PCM_16BIT
73
JNIEnv * env;
74
jobject mtrack = NULL;
75
jmethodID mPlay = NULL;
76
jmethodID mGetMinBufferSize = NULL;
77
jmethodID mAudioTrack = NULL;
78
79
if ((*yvm)->GetEnv(yvm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
80
return -1;
81
82
cAudioTrack = (*env)->FindClass(env, "android/media/AudioTrack");
83
84
mAudioTrack = (*env)->GetMethodID(env, cAudioTrack, "<init>", "(IIIIII)V");
85
86
mWrite = (*env)->GetMethodID(env, cAudioTrack, "write", "([BII)I");
87
88
mPlay = (*env)->GetMethodID(env, cAudioTrack, "play", "()V");
89
90
mGetMinBufferSize = (*env)->GetStaticMethodID(env, cAudioTrack, "getMinBufferSize", "(III)I");
91
92
mbufferSizeInBytes = (*env)->CallStaticIntMethod(env, cAudioTrack, mGetMinBufferSize, sampleRateInHz, channelConfig, audioFormat);
93
94
mtrack = (*env)->NewObject(env, cAudioTrack, mAudioTrack, 3 /* STREAM_MUSIC */, sampleRateInHz, channelConfig, audioFormat, mbufferSizeInBytes, 1 /* MODE_STREAM */);
95
96
gtrack = (*env)->NewGlobalRef(env, mtrack);
97
98
(*env)->CallNonvirtualVoidMethod(env, gtrack, cAudioTrack, mPlay);
99
100
if ((stereodata16 = (u16 *)malloc(2 * mbufferSizeInBytes)) == NULL)
101
return -1;
102
memset(stereodata16, 0, soundbufsize);
103
104
soundvolume = 100;
105
soundmaxvolume = 100;
106
soundbufsize = 85;
107
soundoffset = 0;
108
109
return 0;
110
}
111
112
//////////////////////////////////////////////////////////////////////////////
113
114
static void SNDAudioTrackDeInit(void)
115
{
116
JNIEnv * env;
117
118
if ((*yvm)->GetEnv(yvm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
119
return;
120
121
free(stereodata16);
122
stereodata16 = NULL;
123
124
(*env)->DeleteGlobalRef(env, gtrack);
125
}
126
127
//////////////////////////////////////////////////////////////////////////////
128
129
static int SNDAudioTrackReset(void)
130
{
131
return 0;
132
}
133
134
//////////////////////////////////////////////////////////////////////////////
135
136
static int SNDAudioTrackChangeVideoFormat(int vertfreq)
137
{
138
return 0;
139
}
140
141
//////////////////////////////////////////////////////////////////////////////
142
143
static void sdlConvert32uto16s(s32 *srcL, s32 *srcR, s16 *dst, u32 len) {
144
u32 i;
145
146
for (i = 0; i < len; i++)
147
{
148
// Left Channel
149
*srcL = ( *srcL *soundvolume ) / soundmaxvolume;
150
if (*srcL > 0x7FFF) *dst = 0x7FFF;
151
else if (*srcL < -0x8000) *dst = -0x8000;
152
else *dst = *srcL;
153
srcL++;
154
dst++;
155
// Right Channel
156
*srcR = ( *srcR *soundvolume ) / soundmaxvolume;
157
if (*srcR > 0x7FFF) *dst = 0x7FFF;
158
else if (*srcR < -0x8000) *dst = -0x8000;
159
else *dst = *srcR;
160
srcR++;
161
dst++;
162
}
163
}
164
165
static void SNDAudioTrackUpdateAudio(u32 *leftchanbuffer, u32 *rightchanbuffer, u32 num_samples)
166
{
167
u32 copy1size=0;
168
169
copy1size = (num_samples * sizeof(s16) * 2);
170
171
sdlConvert32uto16s((s32 *)leftchanbuffer, (s32 *)rightchanbuffer, (s16 *)(((u8 *)stereodata16)+soundoffset), copy1size / sizeof(s16) / 2);
172
173
soundoffset += copy1size;
174
175
if (soundoffset > mbufferSizeInBytes) {
176
JNIEnv * env;
177
if ((*yvm)->GetEnv(yvm, (void**) &env, JNI_VERSION_1_6) != JNI_OK)
178
return;
179
180
jshortArray array = (*env)->NewShortArray(env, soundoffset);
181
if(array) {
182
(*env)->SetShortArrayRegion(env, array, 0, soundoffset, stereodata16);
183
}
184
185
(*env)->CallNonvirtualIntMethod(env, gtrack, cAudioTrack, mWrite, array, 0, soundoffset);
186
187
soundoffset = 0;
188
}
189
}
190
191
//////////////////////////////////////////////////////////////////////////////
192
193
static u32 SNDAudioTrackGetAudioSpace(void)
194
{
195
static int i = 0;
196
i++;
197
if (i == 55) {
198
i = 0;
199
return mbufferSizeInBytes;
200
} else {
201
return 0;
202
}
203
}
204
205
//////////////////////////////////////////////////////////////////////////////
206
207
static void SNDAudioTrackMuteAudio(void)
208
{
209
}
210
211
//////////////////////////////////////////////////////////////////////////////
212
213
static void SNDAudioTrackUnMuteAudio(void)
214
{
215
}
216
217
//////////////////////////////////////////////////////////////////////////////
218
219
static void SNDAudioTrackSetVolume(int volume)
220
{
221
}
222
223