summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichal Idziorek <m.i@gmx.at>2017-09-23 11:11:56 +0200
committerMichal Idziorek <m.i@gmx.at>2017-09-23 11:11:56 +0200
commitd23b5c21e100c075bf9b806cec45f0ad0f48301d (patch)
tree1655df6992fbd27b5ebc5f055cc9f6882cc6ebe4
parent8ea31cecb5d03158f0b8a8778848962b6f522095 (diff)
working on sound
-rw-r--r--Makefile3
-rw-r--r--main.cpp79
-rw-r--r--shell_minimal.html2
3 files changed, 77 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index 70672a9..a5714ec 100644
--- a/Makefile
+++ b/Makefile
@@ -10,8 +10,9 @@ clean:
rm -f game
rm -f *.o
rm -f *.bc
+ rm -f lunatic_out.*
browser-game:
emcc --std=c++11 -O2 World.cpp -o World.bc
emcc --std=c++11 -O2 main.cpp -o main.bc
- emcc -O2 World.bc main.bc -o out.html -s USE_SDL=2 --preload-file coin.bmp --preload-file earth01.bmp --preload-file fonts.bmp --preload-file gridder01.bmp --preload-file guy01.bmp --preload-file guy02.bmp --preload-file gridder02.bmp --preload-file gridder03.bmp -s TOTAL_MEMORY=536870912 --shell-file shell_minimal.html
+ emcc -O2 World.bc main.bc -o lunatic_out.html -s USE_SDL=2 --preload-file coin.wav --preload-file coin.bmp --preload-file earth01.bmp --preload-file fonts.bmp --preload-file gridder01.bmp --preload-file guy01.bmp --preload-file guy02.bmp --preload-file gridder02.bmp --preload-file gridder03.bmp -s TOTAL_MEMORY=536870912 --shell-file shell_minimal.html
diff --git a/main.cpp b/main.cpp
index 42c547b..8fda90d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -48,6 +48,57 @@ struct Context
};
+static Uint32 wavLength;
+static Uint8 *wavBuffer;
+
+int sample=0;
+
+void audiomixer(void *userdata, Uint8 *stream, int len)
+{
+ int volume=15000;
+
+ for(int i=0;i<len;i++)
+ {
+ stream[i]=wavBuffer[sample];
+ if(sample>wavLength)stream[i]=0;
+ sample++;
+ }
+ return;
+
+ int samples = len / 4;
+ short *buf = (short*)stream;
+
+/*
+ for (i = 0; i < samples*2; i+=2)
+ {
+ buf[i]=buf[i+1] = volume*sin(sample*440/4410/2);
+ if((sample/44100)%2==1)buf[i]=buf[i+1]=0;
+ buf[i]+=volume*sin(sample*261/4410/2);
+ sample++;
+ }
+*/
+
+
+}
+
+void show_AudioSpec(SDL_AudioSpec *as)
+{
+ std::cout << "freq: " << as->freq << std::endl;
+
+ std::cout << "bitsize: " << SDL_AUDIO_BITSIZE(as->format) << std::endl;
+ std::cout << "float: " << (bool)SDL_AUDIO_ISFLOAT(as->format) << std::endl;
+ std::cout << "bigendian: " << (bool)SDL_AUDIO_ISBIGENDIAN(as->format) << std::endl;
+ std::cout << "signed: " << (bool)SDL_AUDIO_ISSIGNED(as->format) << std::endl;
+ std::cout << "int: " << (bool)SDL_AUDIO_ISINT(as->format) << std::endl;
+ std::cout << "littleendian: " << (bool)SDL_AUDIO_ISLITTLEENDIAN(as->format) << std::endl;
+ std::cout << "unsigned: " << (bool)SDL_AUDIO_ISUNSIGNED(as->format) << std::endl;
+
+
+ std::cout << "channels: " << (int)as->channels << std::endl;
+ std::cout << "samples (buffer): " << as->samples << std::endl;
+ std::cout << "callback func: " << as->callback << std::endl;
+}
+
// audio from http://www.brainybetty.com/soundsforpowerpoint2.htm
// https://freesound.org/people/ProjectsU012/sounds/341695/
void sdl_play_sound()
@@ -55,15 +106,32 @@ void sdl_play_sound()
static bool first=true;
static SDL_AudioSpec wavSpec;
- static Uint32 wavLength;
- static Uint8 *wavBuffer;
static SDL_AudioDeviceID deviceId;
if (first)
{
- SDL_LoadWAV("coin.wav", &wavSpec, &wavBuffer, &wavLength);
+ std::cout << "---desired load wav---" << std::endl;
+ show_AudioSpec(&wavSpec);
+
+ SDL_AudioSpec *spec=SDL_LoadWAV("coin.wav", &wavSpec, &wavBuffer, &wavLength);
+
+ std::cout << "---desired load wav---" << std::endl;
+ show_AudioSpec(&wavSpec);
+ std::cout << "---wav source data---" << std::endl;
+ if(spec==NULL)
+ {
+
+ std::cout << "SDL_LoadWavError: " << SDL_GetError() << std::endl;
+ exit(-1);
+ }
+ show_AudioSpec(spec);
// open most reasonable default device for playback.
+ std::cout << "---" << std::endl;
+ wavSpec.callback = audiomixer;
deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);
+ show_AudioSpec(&wavSpec);
+ std::cout << "---" << std::endl;
+
SDL_PauseAudioDevice(deviceId, 0);
first=false;
}
@@ -72,7 +140,7 @@ void sdl_play_sound()
// SDL_LoadWAV("music.wav", &wavSpec, &wavBuffer, &wavLength);
// SDL_LoadWAV("win.wav", &wavSpec, &wavBuffer, &wavLength);
- int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength);
+// int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength);
#ifndef __EMSCRIPTEN__
#endif
@@ -342,8 +410,8 @@ void main_loop(void *arg)
SDL_RenderPresent(ctx->ren);
if(ctx->world.player.collected_coin)
{
- sdl_play_sound();
ctx->world.player.collected_coin=false;
+ sample=0;
}
// progress world simu
@@ -385,6 +453,7 @@ int main(int, char**){
ctx->show_tiles_back=ctx->win_width/ctx->show_tiles_size*0.25;
ctx->show_tiles_front=ctx->win_width/ctx->show_tiles_size*1;
+ sdl_play_sound();
#ifdef __EMSCRIPTEN__
int simulate_infinite_loop = 1;
diff --git a/shell_minimal.html b/shell_minimal.html
index 33a28b5..3f5a9ac 100644
--- a/shell_minimal.html
+++ b/shell_minimal.html
@@ -5,7 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten-Generated Code</title>
<style>
- #output{display:none;}
+ #output{display:block;}
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
textarea.emscripten { font-family: monospace; width: 80%; }
div.emscripten { text-align: center; }