/
s3eSoundboard.cpp
executable file
·277 lines (238 loc) · 6.6 KB
/
s3eSoundboard.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* This file is part of the Marmalade SDK Code Samples.
*
* Copyright (C) 2001-2011 Ideaworks3D Ltd.
* All Rights Reserved.
*
* This source code is intended only as a supplement to Ideaworks Labs
* Development Tools and/or on-line documentation.
*
* THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*/
/*
* Simple soundboard application that will work with whatever .wav files
* are placed in the data folder.
*/
#include "ExamplesMain.h"
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>
#include <memory.h>
#include "s3eSound.h"
#include "s3eSoundPool.h"
#include "IwGx.h"
// The header for the output wave file
struct RiffHeader
{
char m_ChunkID[4];
int32 m_ChunkSize;
char m_RIFFType[4];
RiffHeader()
{
memcpy(m_ChunkID, "RIFF", 4);
memcpy(m_RIFFType, "WAVE", 4);
}
};
struct Chunk
{
char m_ChunkID[4];
uint32 m_ChunkSize;
};
struct FormatChunk
{
Chunk m_Chunk;
uint16 m_CompressionCode;
uint16 m_NumberOfChannels;
uint32 m_SampleRate;
uint32 m_BytesPerSecond;
uint16 m_BlockAlign;
uint16 m_SignificantBits;
};
static bool g_UseSoundPool = true;
#define MAX_SAMPLES 9
static const char* g_Buttons[MAX_SAMPLES];
static int16* g_SampleData[MAX_SAMPLES];
static int g_SampleDataLen[MAX_SAMPLES];
static int g_Samples[MAX_SAMPLES];
static int g_SampleState[MAX_SAMPLES];
int16* LoadWav(const char* filename, int* sizeOut)
{
RiffHeader header;
Chunk chunk;
int16* rtn = NULL;
FILE* f = fopen(filename, "r");
fseek(f, 0, SEEK_END);
int size = ftell(f);
s3eDebugTracePrintf("filesize = %d", size);
fseek(f, 0, SEEK_SET);
fread(&header, 1, sizeof(header), f);
s3eDebugTracePrintf("%d %.4s %.4s %u", sizeof(header), header.m_ChunkID, header.m_RIFFType, header.m_ChunkSize);
while (1)
{
fread(&chunk, 1, sizeof(chunk), f);
if (!strncmp(chunk.m_ChunkID, "data", 4))
{
rtn = (int16*)malloc(chunk.m_ChunkSize);
fread(rtn, 1, chunk.m_ChunkSize, f);
*sizeOut = chunk.m_ChunkSize;
break;
}
fseek(f, chunk.m_ChunkSize, SEEK_CUR);
}
fclose(f);
return rtn;
}
int32 SampleEnded(s3eSoundPoolEndSampleInfo* pInfo, void* userData)
{
s3eDebugTracePrintf("sample ended = %d", pInfo->m_SampleId);
g_SampleState[pInfo->m_SampleId] = 0;
return 1;
}
int32 ChannelEnded(s3eSoundEndSampleInfo* pInfo, void* userData)
{
s3eDebugTracePrintf("channel ended = %d", pInfo->m_Channel);
g_SampleState[pInfo->m_Channel] = 0;
return 1;
}
void RegisterCallbacks()
{
if (g_UseSoundPool)
{
s3eSoundPoolRegister(S3E_SOUNDPOOL_STOP_AUDIO, (s3eCallback)SampleEnded, 0);
}
else
{
for (int i=0; i<MAX_SAMPLES; ++i)
{
s3eSoundChannelRegister(i, S3E_CHANNEL_STOP_AUDIO, (s3eCallback)ChannelEnded, 0);
}
}
}
void Load(int i, const char* pPath)
{
if (g_UseSoundPool)
g_Samples[i] = s3eSoundPoolSampleLoad(pPath);
else
g_SampleData[i] = LoadWav(pPath, &g_SampleDataLen[i]);
}
s3eResult Play(int i, int repeat)
{
if (g_UseSoundPool)
return s3eSoundPoolSamplePlay(g_Samples[i], repeat, 0);
else
return s3eSoundChannelPlay(i, g_SampleData[i], g_SampleDataLen[i]/2, repeat, 0);
}
s3eResult Pause(int i)
{
if (g_UseSoundPool)
return s3eSoundPoolSamplePause(g_Samples[i]);
else
return s3eSoundChannelPause(i);
}
s3eResult Resume(int i)
{
if (g_UseSoundPool)
return s3eSoundPoolSampleResume(g_Samples[i]);
else
return s3eSoundChannelResume(i);
}
void ExampleInit()
{
g_UseSoundPool = s3eSoundPoolAvailable() == S3E_TRUE;
// Read in sound data
// s3eSoundSetInt(S3E_SOUND_DEFAULT_FREQ, 8000);
DIR* d = opendir(".");
int count = 0;
struct dirent* ent;
while ((ent = readdir(d)))
{
int len = strlen(ent->d_name);
if (len < 4 || stricmp(ent->d_name+len-4, ".wav"))
continue;
Load(count, ent->d_name);
s3eDebugTracePrintf("loaded sound %d (%d)", g_Samples[count], g_SampleDataLen[count]);
ent->d_name[len-4] = '\0';
g_Buttons[count] = strdup(ent->d_name);
AddButton(g_Buttons[count], 20, 20 + 70 * count, 300, 50, (s3eKey)(s3eKey1 + count));
if (++count == MAX_SAMPLES)
break;
}
RegisterCallbacks();
}
void ExampleShutDown()
{
for (int i=0; i<MAX_SAMPLES; ++i)
{
free(g_SampleData[i]);
g_SampleData[i] = NULL;
}
}
bool ExampleUpdate()
{
for (int i = 0; i < MAX_SAMPLES; i++)
{
if (!g_Buttons[i])
break;
if (CheckButton(g_Buttons[i]) & S3E_KEY_STATE_RELEASED)
{
s3eDebugTracePrintf("pressed button %d", i);
if (i % 2)
{
if (!Play(i, 1))
g_SampleState[i] = 1;
}
else
{
switch(g_SampleState[i])
{
case 0:
if (!Play(i, 3))
g_SampleState[i] = 1;
break;
case 1:
if (!Pause(i))
g_SampleState[i] = 2;
break;
case 2:
if (!Resume(i))
g_SampleState[i] = 1;
break;
}
}
}
}
return true;
}
void ExampleRender()
{
int y = 150;
IwGxPrintString(30, y, g_UseSoundPool ? "Using Sound Pool" : "Using Sound Streaming");
y += 20;
for (int i = 0; i < MAX_SAMPLES; i++)
{
if (!g_Buttons[i])
break;
char buffer[0x100];
const char* pState;
switch (g_SampleState[i])
{
case 1:
pState = "Playing";
break;
case 2:
pState = "Paused";
break;
default:
pState = "Stopped";
break;
}
sprintf(buffer, "Sample: %d State: %s", i, pState);
IwGxPrintString(30, y, buffer);
y += 20;
}
IwGxFlush();
IwGxSwapBuffers();
}