testing playing .wav files. lol sounds

This commit is contained in:
Cracker64 2011-02-01 20:35:41 -05:00
parent c558df240b
commit 5383311cba
2 changed files with 87 additions and 4 deletions

View File

@ -3866,7 +3866,7 @@ char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show
clearrect(vid_buf, 0, 0, XRES+BARSIZE, 220);//anyway to make it transparent? clearrect(vid_buf, 0, 0, XRES+BARSIZE, 220);//anyway to make it transparent?
draw_line(vid_buf, 1, 219, XRES, 219, 228, 228, 228, XRES+BARSIZE); draw_line(vid_buf, 1, 219, XRES, 219, 228, 228, 228, XRES+BARSIZE);
drawtext(vid_buf, 100, 15, "Welcome to The Powder Toy console v.2 (by cracker64)\n" drawtext(vid_buf, 100, 15, "Welcome to The Powder Toy console v.2 (by cracker64)\n"
"Current commands are quit, set, reset, load, create, file, kill\n" "Current commands are quit, set, reset, load, create, file, kill, sound\n"
"You can set type, temp, ctype, life, x, y, vx, vy using this format ('set life particle# 9001')\n" "You can set type, temp, ctype, life, x, y, vx, vy using this format ('set life particle# 9001')\n"
"You can also use 'all' instead of a particle number to do it to everything.\n" "You can also use 'all' instead of a particle number to do it to everything.\n"
"You can now use particle names (ex. set type all deut)\n" "You can now use particle names (ex. set type all deut)\n"
@ -3875,6 +3875,7 @@ char *console_ui(pixel *vid_buf,char error[255]) { //TODO: error messages, show
"Create particles with 'create deut x,y' where x and y are the coords\n" "Create particles with 'create deut x,y' where x and y are the coords\n"
"Run scripts from file 'file filename'\n" "Run scripts from file 'file filename'\n"
"You can delete/kill a particle with 'kill x,y'" "You can delete/kill a particle with 'kill x,y'"
"Play a sound with (sound blah.wav)"
,255, 187, 187, 255); ,255, 187, 187, 255);
cc = 0; cc = 0;

View File

@ -28,6 +28,7 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_audio.h>
#include <bzlib.h> #include <bzlib.h>
#include <time.h> #include <time.h>
@ -51,6 +52,69 @@
#include <air.h> #include <air.h>
#include <icon.h> #include <icon.h>
#define NUM_SOUNDS 2
struct sample {
Uint8 *data;
Uint32 dpos;
Uint32 dlen;
} sounds[NUM_SOUNDS];
void mixaudio(void *unused, Uint8 *stream, int len)
{
int i;
Uint32 amount;
for ( i=0; i<NUM_SOUNDS; ++i ) {
amount = (sounds[i].dlen-sounds[i].dpos);
if ( amount > len ) {
amount = len;
}
SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos], amount, SDL_MIX_MAXVOLUME);
sounds[i].dpos += amount;
}
}
void PlaySound(char *file)
{
int index;
SDL_AudioSpec wave;
Uint8 *data;
Uint32 dlen;
SDL_AudioCVT cvt;
/* Look for an empty (or finished) sound slot */
for ( index=0; index<NUM_SOUNDS; ++index ) {
if ( sounds[index].dpos == sounds[index].dlen ) {
break;
}
}
if ( index == NUM_SOUNDS )
return;
/* Load the sound file and convert it to 16-bit stereo at 22kHz */
if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
return;
}
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,
AUDIO_S16, 2, 22050);
cvt.buf = malloc(dlen*cvt.len_mult);
memcpy(cvt.buf, data, dlen);
cvt.len = dlen;
SDL_ConvertAudio(&cvt);
SDL_FreeWAV(data);
/* Put the sound data in the slot (it starts playing immediately) */
if ( sounds[index].data ) {
free(sounds[index].data);
}
SDL_LockAudio();
sounds[index].data = cvt.buf;
sounds[index].dlen = cvt.len_cvt;
sounds[index].dpos = 0;
SDL_UnlockAudio();
}
static const char *it_msg = static const char *it_msg =
"\brThe Powder Toy - http://powdertoy.co.uk/\n" "\brThe Powder Toy - http://powdertoy.co.uk/\n"
"\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\n" "\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F\n"
@ -1136,6 +1200,21 @@ int main(int argc, char *argv[])
int save_mode=0, save_x=0, save_y=0, save_w=0, save_h=0, copy_mode=0; int save_mode=0, save_x=0, save_y=0, save_w=0, save_h=0, copy_mode=0;
GSPEED = 1; GSPEED = 1;
SDL_AudioSpec fmt;
/* Set 16-bit stereo audio at 22Khz */
fmt.freq = 22050;
fmt.format = AUDIO_S16;
fmt.channels = 2;
fmt.samples = 512;
fmt.callback = mixaudio;
fmt.userdata = NULL;
/* Open the audio device and start playing sound! */
if ( SDL_OpenAudio(&fmt, NULL) < 0 )
{
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
exit(1);
}
SDL_PauseAudio(0);
#ifdef MT #ifdef MT
numCores = core_count(); numCores = core_count();
#endif #endif
@ -2303,7 +2382,6 @@ int main(int argc, char *argv[])
cb_bmap[cby][cbx] = bmap[cby][cbx]; cb_bmap[cby][cbx] = bmap[cby][cbx];
cb_emap[cby][cbx] = emap[cby][cbx]; cb_emap[cby][cbx] = emap[cby][cbx];
} }
create_parts(x, y, bsx, bsy, c); create_parts(x, y, bsx, bsy, c);
lx = x; lx = x;
ly = y; ly = y;
@ -2547,7 +2625,7 @@ int main(int argc, char *argv[])
} }
} }
SDL_CloseAudio();
http_done(); http_done();
return 0; return 0;
} }
@ -2595,6 +2673,10 @@ char *console5;
else else
sprintf(console_error, "%s does not exist", console3); sprintf(console_error, "%s does not exist", console3);
} }
else if(strcmp(console2, "sound")==0 && console3)
{
PlaySound(console3);
}
else if(strcmp(console2, "load")==0 && console3) else if(strcmp(console2, "load")==0 && console3)
{ {
j = atoi(console3); j = atoi(console3);