-
Notifications
You must be signed in to change notification settings - Fork 1
/
music.c~
213 lines (182 loc) · 5.55 KB
/
music.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
music.c
Copyright 2007 Sebastien Delestaing
This file is part of "the_cube".
"the_cube" 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.
"the_cube" 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/************************************ INCLUDES ***************************************/
/************************************* DEFINES ***************************************/
/************************************ TYPEDEFS ***************************************/
/********************************* EXTERNAL VARS *************************************/
/********************************** GLOBAL VARS **************************************/
/************************************* STATICS ***************************************/
/********************************* LOCAL FUNCTIONS ***********************************/
/******************************* EXPORTED FUNCTIONS **********************************/
#include "common.h"
#include "music.h"
#include "psp_mreader.h"
static int si_done = 0;
static int si_thread_id = -1;
static MODULE *mod = NULL;
static int si_initialized = false;
extern int _mm_errno;
extern BOOL _mm_critical;
extern char *_mm_errmsg[];
MREADER *my_mreader = NULL;
void music_error_handler (void)
{
pspDebugScreenPrintf ("_mm_critical %d\n", MikMod_critical);
pspDebugScreenPrintf ("_mm_errno %d\n", MikMod_errno);
pspDebugScreenPrintf ("%s\n", MikMod_strerror(MikMod_errno));
return;
}
static int music_audio_channel_thread (int args, void *argp)
{
while (!si_done)
{
MikMod_Update ();
sceKernelDelayThread (1);
}
return (0);
}
int musicInit (void)
{
if (si_initialized == true)
{
pspDebugScreenPrintf ("MikMod already initialized\n");
return 1;
}
if (MikMod_InitThreads ())
{
pspDebugScreenPrintf ("MikMod thread init failed\n");
return -1;
}
MikMod_RegisterErrorHandler (music_error_handler);
MikMod_RegisterAllDrivers ();
MikMod_RegisterAllLoaders ();
/* initialize the library */
md_mode = DMODE_16BITS | DMODE_STEREO | DMODE_SOFT_SNDFX | DMODE_SOFT_MUSIC;
md_reverb = 0;
md_pansep = 128;
if (MikMod_Init (""))
{
pspDebugScreenPrintf ("Could not initialize sound, reason: %s\n", MikMod_strerror (MikMod_errno));
return -2;
}
MikMod_SetNumVoices (-1, 8);
/* get ready to play */
MikMod_EnableOutput ();
si_initialized = true;
return 0;
}
int musicPlayMODFromMemory (unsigned char *pc_mod_start, unsigned char *pc_mod_end)
{
if (si_initialized == false)
return -1;
// create mikmod update thread
if ((si_thread_id = sceKernelCreateThread ("MikMod", (void*)&music_audio_channel_thread, 0x12, 0x10000, 0, NULL)) > 0)
{
sceKernelStartThread (si_thread_id, 0 , NULL);
}
else
return -2;
// create PSP memory reader (pass the mod buffer as argument)
my_mreader = psp_mreader_new (pc_mod_start, pc_mod_end);
if (my_mreader == NULL)
{
pspDebugScreenPrintf ("could not create mreader\n");
return -3;
}
mod = Player_LoadGeneric (my_mreader, 128, 0);
if (mod != NULL)
{
mod->wrap = 1;
Player_Start (mod);
}
else
{
pspDebugScreenPrintf ("could not play (generic)\n");
return -4;
}
return 0;
}
int musicPlayMODFromFile (char *pc_file_name)
{
if (si_initialized == false)
{
//pspDebugScreenPrintf ("MikMod library not initialized\n");
return -1;
}
// create mikmod update thread
if ((si_thread_id = sceKernelCreateThread ("MikMod", (void*)&music_audio_channel_thread, 0x12, 0x10000, 0, NULL)) > 0)
{
sceKernelStartThread (si_thread_id, 0 , NULL);
}
else
{
//pspDebugScreenPrintf ("Play thread create failed!\n");
return -2;
}
// max_chan is hardcoded to 128
mod = Player_Load (pc_file_name, 128, 0);
if (mod != NULL)
{
mod->wrap = 1;
Player_Start (mod);
}
else
{
//pspDebugScreenPrintf ("Loading MOD file %s failed!\n", pc_file_name);
return -3;
}
return 0;
}
void musicStop (void)
{
if (si_initialized == false)
{
pspDebugScreenPrintf ("MikMod library not initialized\n");
return;
}
// allow audio thread to terminate cleanly
si_done = true;
if (si_thread_id > 0)
{
SceUInt timeout = 100000;
sceKernelWaitThreadEnd (si_thread_id, &timeout);
// not 100% sure if this is necessary after a clean exit, but just to make sure any resources are freed:
sceKernelDeleteThread (si_thread_id);
}
if (mod != NULL)
{
Player_Stop ();
Player_Free (mod);
psp_mreader_free (my_mreader);
}
si_done = 0;
si_thread_id = -1;
mod = NULL;
}
void musicDispose (void)
{
if (si_initialized == false)
{
pspDebugScreenPrintf ("MikMod library not initialized\n");
return;
}
if (mod != NULL)
{
musicStop ();
}
MikMod_Exit ();
si_initialized = false;
}