# HG changeset patch # User Robert McIntyre # Date 1340561571 18000 # Node ID fa7676dbf6f2352c625f320aa72d2cefcfd57425 # Parent 7ef5c73ea8fa6d922630278a87d6ab338a34bc2d sound recording test now works. diff -r 7ef5c73ea8fa -r fa7676dbf6f2 clojure/com/aurellem/gb/gb_driver.clj --- a/clojure/com/aurellem/gb/gb_driver.clj Sat Jun 23 23:10:31 2012 -0500 +++ b/clojure/com/aurellem/gb/gb_driver.clj Sun Jun 24 13:12:51 2012 -0500 @@ -199,9 +199,16 @@ ([state] (set-state! state) (Gb/getFrameSound store) - (Gb/setSoundFrameWritten 0) store))) - + +(let [store (byte-array (* 1470 2))] + (defn sound-data-2 + ([](sound-data-2 @current-state)) + ([state] + (set-state! state) + (Gb/getFrameSound2 store) + store))) + (def memory (cpu-data Gb/GB_MEMORY #(Gb/getMemory %))) diff -r 7ef5c73ea8fa -r fa7676dbf6f2 clojure/com/aurellem/run/sound.clj --- a/clojure/com/aurellem/run/sound.clj Sat Jun 23 23:10:31 2012 -0500 +++ b/clojure/com/aurellem/run/sound.clj Sun Jun 24 13:12:51 2012 -0500 @@ -7,8 +7,9 @@ (:require clojure.string) (:import [com.aurellem.gb.gb_driver SaveState]) (:import java.awt.image.BufferedImage) - (:import java.io.File)) - + (:import java.io.File) + (:import javax.sound.sampled.AudioFormat) + (:import com.aurellem.gb.WaveWriter)) (defn sound-test [] (step (mid-game)) @@ -16,10 +17,6 @@ (run-moves @current-state (repeat 10 [])) (println (frequencies (sound-data)))) - -(import javax.sound.sampled.AudioFormat) -(import com.aurellem.gb.WaveWriter) - (def probable-format (AudioFormat. 44100 16 2 true false)) (defn test-writing-file! [n] @@ -30,16 +27,26 @@ (dorun (for [y (range n)] (do - (let [quanta 30] + (let [quanta 1] (run-moves @current-state (repeat quanta [])) - (let [data (sound-data) + (let [ + + data (sound-data) + bytes (* 2 (com.aurellem.gb.Gb/getSoundFrameWritten)) step-section - (byte-array (take (* 2940 quanta) data))] - - (.process writer step-section probable-format)))))) + (byte-array + (take (* bytes quanta) data)) + + + ] + (com.aurellem.gb.Gb/setSoundFrameWritten 0) + (.process writer + ;;data + step-section + probable-format)))))) (.cleanup writer) (Thread/sleep 1000) (clojure.java.shell/sh "aplay" (.getCanonicalPath target-file)))) - \ No newline at end of file + diff -r 7ef5c73ea8fa -r fa7676dbf6f2 java/src/com/aurellem/gb/Gb.java --- a/java/src/com/aurellem/gb/Gb.java Sat Jun 23 23:10:31 2012 -0500 +++ b/java/src/com/aurellem/gb/Gb.java Sun Jun 24 13:12:51 2012 -0500 @@ -141,4 +141,6 @@ public static native void setSoundFrameWritten(int frames); + public static native void getFrameSound2(byte[] store); + } diff -r 7ef5c73ea8fa -r fa7676dbf6f2 src/clojure/clojure.cpp --- a/src/clojure/clojure.cpp Sat Jun 23 23:10:31 2012 -0500 +++ b/src/clojure/clojure.cpp Sun Jun 24 13:12:51 2012 -0500 @@ -365,10 +365,10 @@ } /* - u8* soundBytes = (u8*) soundFinalWave; - for (i = 0; i < 1470*2 ; i++){ + u8* soundBytes = (u8*) soundFinalWave; + for (i = 0; i < 1470*2 ; i++){ sound_store[i] = (jbyte) soundBytes[i]; - } + } */ env->ReleaseByteArrayElements(arr, sound_store, 0); @@ -395,3 +395,23 @@ soundFrameSoundWritten = newSoundFrameWritten; } + +/* + * Class: com_aurellem_gb_Gb + * Method: getFrameSound2 + * Signature: ([B)V + */ +JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getFrameSound2 +(JNIEnv *env, jclass clazz, jbyteArray arr){ + setbuf(stdout, NULL); + + jbyte *sound_store = env->GetByteArrayElements(arr, 0); + int i; + + for (i = 0; i < 1470*2; i++){ + sound_store[i] = (jbyte) soundCopyBuffer[i]; + } + + env->ReleaseByteArrayElements(arr, sound_store, 0); + +} diff -r 7ef5c73ea8fa -r fa7676dbf6f2 src/gb/GB.cpp --- a/src/gb/GB.cpp Sat Jun 23 23:10:31 2012 -0500 +++ b/src/gb/GB.cpp Sun Jun 24 13:12:51 2012 -0500 @@ -3223,7 +3223,7 @@ } extButtons = (newmask >> 18); - speedup = (extButtons & 1) != 0; + speedup = (extButtons & 1) != 0; VBAMovieResetIfRequested(); //printf("RLM: before Lua functions\n"); @@ -3753,7 +3753,7 @@ while (soundTicks < 0) // must be < 1 when soundtick_t is real data type { soundTicks += SOUND_CLOCK_TICKS; - + printf("gbSoundTick()\n"); gbSoundTick(); } diff -r 7ef5c73ea8fa -r fa7676dbf6f2 src/gb/GB.h --- a/src/gb/GB.h Sat Jun 23 23:10:31 2012 -0500 +++ b/src/gb/GB.h Sun Jun 24 13:12:51 2012 -0500 @@ -68,6 +68,8 @@ extern u16 soundFrameSound[735 * 30 * 2]; extern u16 soundFinalWave[1470]; extern int32 soundFrameSoundWritten; +extern u8 soundCopyBuffer[1470 * 2]; + extern struct EmulatedSystem GBSystem; extern struct EmulatedSystemCounters &GBSystemCounters; diff -r 7ef5c73ea8fa -r fa7676dbf6f2 src/gb/gbSound.cpp --- a/src/gb/gbSound.cpp Sat Jun 23 23:10:31 2012 -0500 +++ b/src/gb/gbSound.cpp Sun Jun 24 13:12:51 2012 -0500 @@ -730,7 +730,8 @@ { soundFinalWave[++soundBufferIndex] = res; if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) - printf("oh noes!\n"); + + ; //printf("oh noes!\n"); else soundFrameSound[++soundFrameSoundWritten] = res; } @@ -738,7 +739,7 @@ { soundFinalWave[soundBufferIndex++] = res; if (soundFrameSoundWritten >= countof(soundFrameSound)) - printf("oh noes!\n"); + ; //printf("oh noes!\n"); else soundFrameSound[soundFrameSoundWritten++] = res; } @@ -817,7 +818,7 @@ { soundFinalWave[-1 + soundBufferIndex++] = res; if ((soundFrameSoundWritten) >= countof(soundFrameSound)) - printf("oh noes!\n"); + ;//printf("oh noes!\n"); else soundFrameSound[-1 + soundFrameSoundWritten++] = res; } @@ -825,7 +826,7 @@ { soundFinalWave[soundBufferIndex++] = res; if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) - printf("oh noes!\n"); + ;//printf("oh noes!\n"); else soundFrameSound[soundFrameSoundWritten++] = res; } @@ -833,49 +834,49 @@ void gbSoundTick() { - if (systemSoundOn) + if (systemSoundOn) + { + if (soundMasterOn) { - if (soundMasterOn) + gbSoundChannel1(); + gbSoundChannel2(); + gbSoundChannel3(); + gbSoundChannel4(); + + gbSoundMix(); + } + else + { + soundFinalWave[soundBufferIndex++] = 0; + soundFinalWave[soundBufferIndex++] = 0; + if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) + + ;//printf("oh noes!\n"); + else + { + soundFrameSound[soundFrameSoundWritten++] = 0; + soundFrameSound[soundFrameSoundWritten++] = 0; + } + } + + soundIndex++; + + if (2 * soundBufferIndex >= soundBufferLen) + { + if (systemSoundOn) + { + if (soundPaused) { - gbSoundChannel1(); - gbSoundChannel2(); - gbSoundChannel3(); - gbSoundChannel4(); - - gbSoundMix(); - } - else - { - soundFinalWave[soundBufferIndex++] = 0; - soundFinalWave[soundBufferIndex++] = 0; - if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) - - printf("oh noes!\n"); - else - { - soundFrameSound[soundFrameSoundWritten++] = 0; - soundFrameSound[soundFrameSoundWritten++] = 0; - } + extern void soundResume(); + soundResume(); } - soundIndex++; - - if (2 * soundBufferIndex >= soundBufferLen) - { - if (systemSoundOn) - { - if (soundPaused) - { - extern void soundResume(); - soundResume(); - } - - systemSoundWriteToBuffer(); - } - soundIndex = 0; - soundBufferIndex = 0; - } + systemSoundWriteToBuffer(); + } + soundIndex = 0; + soundBufferIndex = 0; } + } } void gbSoundReset() diff -r 7ef5c73ea8fa -r fa7676dbf6f2 src/sdl/SDL.cpp --- a/src/sdl/SDL.cpp Sat Jun 23 23:10:31 2012 -0500 +++ b/src/sdl/SDL.cpp Sun Jun 24 13:12:51 2012 -0500 @@ -3092,10 +3092,24 @@ void soundCallback(void *,u8 *stream,int len){} + +int writeCounter = 0; +u8 soundCopyBuffer[1470 * 2]; + + + void systemSoundWriteToBuffer(){ - soundDriver->write(soundFinalWave, soundBufferLen); + //printf("sound write counter (len) : %07d (%d)\n", + //writeCounter++, soundBufferLen); + //int i; + //u8* soundBytes = (u8*) soundFinalWave; + //for (i = 0; i < 1470 * 2; i++){ + // soundCopyBuffer[i] = soundBytes[i]; + //} + soundDriver->write(soundFinalWave, soundBufferLen); } + void systemSoundClearBuffer() { SDL_mutexP(mutex); diff -r 7ef5c73ea8fa -r fa7676dbf6f2 src/sdl/SoundSDL.cpp --- a/src/sdl/SoundSDL.cpp Sat Jun 23 23:10:31 2012 -0500 +++ b/src/sdl/SoundSDL.cpp Sun Jun 24 13:12:51 2012 -0500 @@ -16,6 +16,8 @@ // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "SoundSDL.h" +#include + extern int emulating; extern bool speedup; @@ -37,53 +39,61 @@ void SoundSDL::read(u16 * stream, int length) { - if (!_initialized || length <= 0 || !emulating) - return; + if (!_initialized || length <= 0 || !emulating) + return; - SDL_mutexP(_mutex); - _rbuf.read(stream, std::min(static_cast(length) / 2, _rbuf.used())); + SDL_mutexP(_mutex); + _rbuf.read(stream, std::min(static_cast(length) / 2, _rbuf.used())); - SDL_CondSignal(_cond); - SDL_mutexV(_mutex); + SDL_CondSignal(_cond); + SDL_mutexV(_mutex); } void SoundSDL::write(u16 * finalWave, int length) { - if (!_initialized) - return; + if (!_initialized) + return; - if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING) - SDL_PauseAudio(0); + if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING){ + SDL_PauseAudio(0); + printf("SDLPauseAudio\n"); + } - SDL_mutexP(_mutex); + SDL_mutexP(_mutex); - unsigned int samples = length / 4; + //printf("RLM: length = %d\n", length); - std::size_t avail; - while ((avail = _rbuf.avail() / 2) < samples) + unsigned int samples = length / 4; + + // printf("RLM: length / 4 = %d\n", samples); + + std::size_t avail; + while ((avail = _rbuf.avail() / 2) < samples) + { + _rbuf.write(finalWave, avail * 2); + + finalWave += avail * 2; + samples -= avail; + + // If emulating and not in speed up mode, synchronize to audio + // by waiting till there is enough room in the buffer + if (emulating && !speedup) { - _rbuf.write(finalWave, avail * 2); + //printf("SDL_CondWait\n"); + SDL_CondWait(_cond,_mutex); + } + else + { + // Drop the remainder of the audio data + printf("RLM: Drop samples!\n"); + SDL_mutexV(_mutex); + return; + } + } - finalWave += avail * 2; - samples -= avail; - - // If emulating and not in speed up mode, synchronize to audio - // by waiting till there is enough room in the buffer - if (emulating && !speedup) - { - SDL_CondWait(_cond,_mutex); - } - else - { - // Drop the remaining of the audio data - SDL_mutexV(_mutex); - return; - } - } - - _rbuf.write(finalWave, samples * 2); - - SDL_mutexV(_mutex); + _rbuf.write(finalWave, samples * 2); + //printf("RLM: writing %d samples\n", samples); + SDL_mutexV(_mutex); } diff -r 7ef5c73ea8fa -r fa7676dbf6f2 test-sound.wav Binary file test-sound.wav has changed