# HG changeset patch # User Robert McIntyre # Date 1340511031 18000 # Node ID 7ef5c73ea8fa6d922630278a87d6ab338a34bc2d # Parent d00096b6bf17908558a8c7ba5ae0527f4a7244ad working on recording sound. almost have it, but there is still unexplained popping sounds. diff -r d00096b6bf17 -r 7ef5c73ea8fa .hgignore --- a/.hgignore Sat Jun 23 20:34:14 2012 -0500 +++ b/.hgignore Sat Jun 23 23:10:31 2012 -0500 @@ -16,3 +16,4 @@ java/headers/* java/.ant-targets-build.xml html/* +java/lib/* diff -r d00096b6bf17 -r 7ef5c73ea8fa clojure/com/aurellem/gb/gb_driver.clj --- a/clojure/com/aurellem/gb/gb_driver.clj Sat Jun 23 20:34:14 2012 -0500 +++ b/clojure/com/aurellem/gb/gb_driver.clj Sat Jun 23 23:10:31 2012 -0500 @@ -193,12 +193,15 @@ ([new-data] (store-data @current-state new-data)))) -(def sound-data - (cpu-data Gb/SOUND_SIZE - (fn [arr] - (Gb/getFrameSound arr) - (Gb/setSoundFrameWritten 0)))) - +(let [store (byte-array Gb/SOUND_SIZE)] + (defn sound-data + ([](sound-data @current-state)) + ([state] + (set-state! state) + (Gb/getFrameSound store) + (Gb/setSoundFrameWritten 0) + store))) + (def memory (cpu-data Gb/GB_MEMORY #(Gb/getMemory %))) @@ -304,4 +307,5 @@ (defn rgb->gb-rb [[r g b :as color]] (let [store (int-array 3)] (Gb/translateRGB (int-array color) store) - (vec store))) \ No newline at end of file + (vec store))) + diff -r d00096b6bf17 -r 7ef5c73ea8fa clojure/com/aurellem/run/sound.clj --- a/clojure/com/aurellem/run/sound.clj Sat Jun 23 20:34:14 2012 -0500 +++ b/clojure/com/aurellem/run/sound.clj Sat Jun 23 23:10:31 2012 -0500 @@ -17,4 +17,29 @@ (println (frequencies (sound-data)))) - \ No newline at end of file +(import javax.sound.sampled.AudioFormat) +(import com.aurellem.gb.WaveWriter) + +(def probable-format (AudioFormat. 44100 16 2 true false)) + +(defn test-writing-file! [n] + (set-state! (play-midi pony-csv)) + (let [target-file + (File. "/home/r/proj/vba-clojure/test-sound.wav") + writer (WaveWriter. target-file)] + (dorun + (for [y (range n)] + (do + (let [quanta 30] + (run-moves @current-state (repeat quanta [])) + (let [data (sound-data) + step-section + (byte-array (take (* 2940 quanta) data))] + + (.process writer 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 d00096b6bf17 -r 7ef5c73ea8fa java/build.xml --- a/java/build.xml Sat Jun 23 20:34:14 2012 -0500 +++ b/java/build.xml Sat Jun 23 23:10:31 2012 -0500 @@ -5,6 +5,13 @@ + + + + + + + @@ -14,6 +21,7 @@ diff -r d00096b6bf17 -r 7ef5c73ea8fa java/src/com/aurellem/gb/Gb.java --- a/java/src/com/aurellem/gb/Gb.java Sat Jun 23 20:34:14 2012 -0500 +++ b/java/src/com/aurellem/gb/Gb.java Sat Jun 23 23:10:31 2012 -0500 @@ -101,7 +101,9 @@ public static final int GB_MEMORY = 0x10000; - public static final int SOUND_SIZE = 735 * 30 * 2; + public static final int SOUND_SIZE = + 44100*2; + //1470*2; public static native void getMemory(int[] store); @@ -129,12 +131,12 @@ public static native void getPixels(int[] store); - public static native void getFrameSound(int[] store); - public static native void nwritePNG(String filename); public static native int readMemory(int address); + public static native void getFrameSound(byte[] store); + public static native int getSoundFrameWritten(); public static native void setSoundFrameWritten(int frames); diff -r d00096b6bf17 -r 7ef5c73ea8fa java/src/com/aurellem/gb/WaveWriter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/src/com/aurellem/gb/WaveWriter.java Sat Jun 23 23:10:31 2012 -0500 @@ -0,0 +1,48 @@ +package com.aurellem.gb; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioSystem; + +import org.tritonus.sampled.file.WaveAudioOutputStream; +import org.tritonus.share.sampled.file.TDataOutputStream; +import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream; + + +public class WaveWriter { + public File targetFile; + private WaveAudioOutputStream wao; + private TDataOutputStream tos; + private boolean initialized = false; + + public WaveWriter(File targetFile) + throws FileNotFoundException{ + tos = new TNonSeekableDataOutputStream + (new FileOutputStream(targetFile)); + } + + public void init(AudioFormat format){ + wao = new WaveAudioOutputStream + (format,AudioSystem.NOT_SPECIFIED, tos); + } + + public void process(byte[] audioSamples, + AudioFormat format) { + if (!initialized){ + init(format); + initialized = true; + } + try {wao.write(audioSamples, 0, audioSamples.length);} + catch (IOException e) {e.printStackTrace();} + } + + public void cleanup() { + try {wao.close();} + catch (IOException e) {e.printStackTrace();} + } +} \ No newline at end of file diff -r d00096b6bf17 -r 7ef5c73ea8fa src/clojure/clojure.cpp --- a/src/clojure/clojure.cpp Sat Jun 23 20:34:14 2012 -0500 +++ b/src/clojure/clojure.cpp Sat Jun 23 23:10:31 2012 -0500 @@ -348,20 +348,30 @@ return (jint) gbReadMemory((u16) address); } - /* * Class: com_aurellem_gb_Gb * Method: getFrameSound - * Signature: ([I)V + * Signature: ([B)V */ JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getFrameSound -(JNIEnv *env, jclass clazz, jintArray arr){ - jint *sound_store = env->GetIntArrayElements(arr, 0); +(JNIEnv *env, jclass clazz, jbyteArray arr){ + jbyte *sound_store = env->GetByteArrayElements(arr, 0); int i; - for (i = 0; i < 44100; i++){ - sound_store[i] = (jint) soundFrameSound[i]; + + + u8* soundBytes = (u8*) soundFrameSound; + for (i = 0; i < 44100*2; i++){ + sound_store[i] = (jbyte) soundBytes[i]; } - env->ReleaseIntArrayElements(arr, sound_store, 0); + + /* + u8* soundBytes = (u8*) soundFinalWave; + for (i = 0; i < 1470*2 ; i++){ + sound_store[i] = (jbyte) soundBytes[i]; + } + */ + + env->ReleaseByteArrayElements(arr, sound_store, 0); } diff -r d00096b6bf17 -r 7ef5c73ea8fa src/gb/GB.h --- a/src/gb/GB.h Sat Jun 23 20:34:14 2012 -0500 +++ b/src/gb/GB.h Sat Jun 23 23:10:31 2012 -0500 @@ -66,6 +66,7 @@ extern u8 gbReadMemory(u16 address); extern u16 soundFrameSound[735 * 30 * 2]; +extern u16 soundFinalWave[1470]; extern int32 soundFrameSoundWritten; extern struct EmulatedSystem GBSystem; diff -r d00096b6bf17 -r 7ef5c73ea8fa src/gb/gbSound.cpp --- a/src/gb/gbSound.cpp Sat Jun 23 20:34:14 2012 -0500 +++ b/src/gb/gbSound.cpp Sat Jun 23 23:10:31 2012 -0500 @@ -5,6 +5,7 @@ #include #include +#include #include "../common/System.h" #include "../common/Util.h" @@ -729,7 +730,7 @@ { soundFinalWave[++soundBufferIndex] = res; if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) - /*assert(false)*/; + printf("oh noes!\n"); else soundFrameSound[++soundFrameSoundWritten] = res; } @@ -737,7 +738,7 @@ { soundFinalWave[soundBufferIndex++] = res; if (soundFrameSoundWritten >= countof(soundFrameSound)) - /*assert(false)*/; + printf("oh noes!\n"); else soundFrameSound[soundFrameSoundWritten++] = res; } @@ -816,7 +817,7 @@ { soundFinalWave[-1 + soundBufferIndex++] = res; if ((soundFrameSoundWritten) >= countof(soundFrameSound)) - /*assert(false)*/; + printf("oh noes!\n"); else soundFrameSound[-1 + soundFrameSoundWritten++] = res; } @@ -824,7 +825,7 @@ { soundFinalWave[soundBufferIndex++] = res; if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) - /*assert(false)*/; + printf("oh noes!\n"); else soundFrameSound[soundFrameSoundWritten++] = res; } @@ -848,7 +849,8 @@ soundFinalWave[soundBufferIndex++] = 0; soundFinalWave[soundBufferIndex++] = 0; if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound)) - /*assert(false)*/; + + printf("oh noes!\n"); else { soundFrameSound[soundFrameSoundWritten++] = 0; diff -r d00096b6bf17 -r 7ef5c73ea8fa test-sound.wav Binary file test-sound.wav has changed