changeset 524:7ef5c73ea8fa

working on recording sound. almost have it, but there is still unexplained popping sounds.
author Robert McIntyre <rlm@mit.edu>
date Sat, 23 Jun 2012 23:10:31 -0500
parents d00096b6bf17
children fa7676dbf6f2
files .hgignore clojure/com/aurellem/gb/gb_driver.clj clojure/com/aurellem/run/sound.clj java/build.xml java/src/com/aurellem/gb/Gb.java java/src/com/aurellem/gb/WaveWriter.java src/clojure/clojure.cpp src/gb/GB.h src/gb/gbSound.cpp test-sound.wav
diffstat 10 files changed, 124 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/.hgignore	Sat Jun 23 20:34:14 2012 -0500
     1.2 +++ b/.hgignore	Sat Jun 23 23:10:31 2012 -0500
     1.3 @@ -16,3 +16,4 @@
     1.4  java/headers/*
     1.5  java/.ant-targets-build.xml
     1.6  html/*
     1.7 +java/lib/*
     2.1 --- a/clojure/com/aurellem/gb/gb_driver.clj	Sat Jun 23 20:34:14 2012 -0500
     2.2 +++ b/clojure/com/aurellem/gb/gb_driver.clj	Sat Jun 23 23:10:31 2012 -0500
     2.3 @@ -193,12 +193,15 @@
     2.4      ([new-data]
     2.5         (store-data @current-state new-data))))
     2.6  
     2.7 -(def sound-data
     2.8 -  (cpu-data Gb/SOUND_SIZE
     2.9 -            (fn [arr]
    2.10 -              (Gb/getFrameSound arr)
    2.11 -              (Gb/setSoundFrameWritten 0))))
    2.12 -
    2.13 +(let [store (byte-array Gb/SOUND_SIZE)]
    2.14 +  (defn sound-data 
    2.15 +    ([](sound-data @current-state))
    2.16 +    ([state]
    2.17 +       (set-state! state)
    2.18 +       (Gb/getFrameSound store)
    2.19 +       (Gb/setSoundFrameWritten 0)
    2.20 +       store)))
    2.21 +  
    2.22  (def memory
    2.23    (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
    2.24  
    2.25 @@ -304,4 +307,5 @@
    2.26  (defn rgb->gb-rb [[r g b :as color]]
    2.27    (let [store (int-array 3)]
    2.28      (Gb/translateRGB (int-array color) store)
    2.29 -    (vec store)))
    2.30 \ No newline at end of file
    2.31 +    (vec store)))
    2.32 +
     3.1 --- a/clojure/com/aurellem/run/sound.clj	Sat Jun 23 20:34:14 2012 -0500
     3.2 +++ b/clojure/com/aurellem/run/sound.clj	Sat Jun 23 23:10:31 2012 -0500
     3.3 @@ -17,4 +17,29 @@
     3.4    (println (frequencies (sound-data))))
     3.5  
     3.6  
     3.7 -        
     3.8 \ No newline at end of file
     3.9 +(import javax.sound.sampled.AudioFormat)
    3.10 +(import com.aurellem.gb.WaveWriter)
    3.11 +
    3.12 +(def probable-format (AudioFormat. 44100 16 2 true false))
    3.13 +
    3.14 +(defn test-writing-file! [n]
    3.15 +  (set-state! (play-midi pony-csv))
    3.16 +  (let [target-file
    3.17 +        (File. "/home/r/proj/vba-clojure/test-sound.wav")
    3.18 +        writer (WaveWriter. target-file)]
    3.19 +    (dorun
    3.20 +     (for [y (range n)]
    3.21 +       (do
    3.22 +         (let [quanta 30]
    3.23 +           (run-moves @current-state (repeat quanta []))
    3.24 +           (let [data (sound-data)
    3.25 +                 step-section
    3.26 +                 (byte-array (take (* 2940 quanta) data))]
    3.27 +             
    3.28 +             (.process writer step-section probable-format))))))
    3.29 +     (.cleanup writer)
    3.30 +     (Thread/sleep 1000)
    3.31 +     (clojure.java.shell/sh
    3.32 +      "aplay"
    3.33 +      (.getCanonicalPath target-file))))
    3.34 +      
    3.35 \ No newline at end of file
     4.1 --- a/java/build.xml	Sat Jun 23 20:34:14 2012 -0500
     4.2 +++ b/java/build.xml	Sat Jun 23 23:10:31 2012 -0500
     4.3 @@ -5,6 +5,13 @@
     4.4    <property name="dist" value="dist"/>
     4.5    <property name="headers"  value="headers"/>
     4.6    <property name="artifacts" value="../artifacts"/>
     4.7 +  <property name="lib"  value="lib"/>
     4.8 +
     4.9 +  <path id="classpath">
    4.10 +    <pathelement path="${lib}/tritonus_aos-0.3.6.jar"/>
    4.11 +    <pathelement path="${lib}/tritonus_share-0.3.6.jar"/>
    4.12 +  </path>
    4.13 +
    4.14  
    4.15    <target name="prepare">
    4.16      <mkdir dir="${build}"/>
    4.17 @@ -14,6 +21,7 @@
    4.18  
    4.19    <target name="compile" depends="prepare" >
    4.20      <javac srcdir="${src}" destdir="${build}"
    4.21 +	   classpathref="classpath"
    4.22  	   includeantruntime="false"/>
    4.23    </target>
    4.24  
     5.1 --- a/java/src/com/aurellem/gb/Gb.java	Sat Jun 23 20:34:14 2012 -0500
     5.2 +++ b/java/src/com/aurellem/gb/Gb.java	Sat Jun 23 23:10:31 2012 -0500
     5.3 @@ -101,7 +101,9 @@
     5.4  
     5.5      public static final int GB_MEMORY = 0x10000;
     5.6  
     5.7 -    public static final int SOUND_SIZE = 735 * 30 * 2;
     5.8 +    public static final int SOUND_SIZE = 
     5.9 +	44100*2;
    5.10 +    //1470*2;
    5.11  
    5.12      public static native void getMemory(int[] store);
    5.13  
    5.14 @@ -129,12 +131,12 @@
    5.15  
    5.16      public static native void getPixels(int[] store);
    5.17  
    5.18 -    public static native void getFrameSound(int[] store);
    5.19 -
    5.20      public static native void nwritePNG(String filename);
    5.21  
    5.22      public static native int readMemory(int address);
    5.23      
    5.24 +    public static native void getFrameSound(byte[] store);
    5.25 +
    5.26      public static native int getSoundFrameWritten();
    5.27  
    5.28      public static native void setSoundFrameWritten(int frames);
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/src/com/aurellem/gb/WaveWriter.java	Sat Jun 23 23:10:31 2012 -0500
     6.3 @@ -0,0 +1,48 @@
     6.4 +package com.aurellem.gb;
     6.5 +
     6.6 +import java.io.File;
     6.7 +import java.io.FileNotFoundException;
     6.8 +import java.io.FileOutputStream;
     6.9 +import java.io.IOException;
    6.10 +import java.nio.ByteBuffer;
    6.11 +
    6.12 +import javax.sound.sampled.AudioFormat;
    6.13 +import javax.sound.sampled.AudioSystem;
    6.14 +
    6.15 +import org.tritonus.sampled.file.WaveAudioOutputStream;
    6.16 +import org.tritonus.share.sampled.file.TDataOutputStream;
    6.17 +import org.tritonus.share.sampled.file.TNonSeekableDataOutputStream;
    6.18 +
    6.19 +
    6.20 +public class WaveWriter {
    6.21 +    public  File targetFile;
    6.22 +    private WaveAudioOutputStream wao;
    6.23 +    private TDataOutputStream tos;
    6.24 +    private boolean initialized = false;
    6.25 +	
    6.26 +    public WaveWriter(File targetFile) 
    6.27 +	throws FileNotFoundException{
    6.28 +	tos = new TNonSeekableDataOutputStream
    6.29 +	    (new FileOutputStream(targetFile));
    6.30 +    }
    6.31 +
    6.32 +    public void init(AudioFormat format){
    6.33 +	wao = new WaveAudioOutputStream
    6.34 +	    (format,AudioSystem.NOT_SPECIFIED, tos);
    6.35 +    }
    6.36 +		
    6.37 +    public void process(byte[] audioSamples, 
    6.38 +			AudioFormat format) {
    6.39 +	if (!initialized){
    6.40 +	    init(format);
    6.41 +	    initialized = true;
    6.42 +	}
    6.43 +	try {wao.write(audioSamples, 0, audioSamples.length);}
    6.44 +	catch (IOException e) {e.printStackTrace();}
    6.45 +    }
    6.46 +
    6.47 +    public void cleanup() {
    6.48 +	try {wao.close();}
    6.49 +	catch (IOException e) {e.printStackTrace();}
    6.50 +    }
    6.51 +}
    6.52 \ No newline at end of file
     7.1 --- a/src/clojure/clojure.cpp	Sat Jun 23 20:34:14 2012 -0500
     7.2 +++ b/src/clojure/clojure.cpp	Sat Jun 23 23:10:31 2012 -0500
     7.3 @@ -348,20 +348,30 @@
     7.4    return (jint) gbReadMemory((u16) address); 
     7.5  }
     7.6  
     7.7 -
     7.8  /*
     7.9   * Class:     com_aurellem_gb_Gb
    7.10   * Method:    getFrameSound
    7.11 - * Signature: ([I)V
    7.12 + * Signature: ([B)V
    7.13   */
    7.14  JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getFrameSound
    7.15 -(JNIEnv *env, jclass clazz, jintArray arr){
    7.16 -  jint *sound_store = env->GetIntArrayElements(arr, 0);
    7.17 +(JNIEnv *env, jclass clazz, jbyteArray arr){
    7.18 +  jbyte *sound_store = env->GetByteArrayElements(arr, 0);
    7.19    int i;
    7.20 -  for (i = 0; i < 44100; i++){
    7.21 -    sound_store[i] = (jint) soundFrameSound[i];
    7.22 +
    7.23 +  
    7.24 +  u8* soundBytes = (u8*) soundFrameSound;
    7.25 +  for (i = 0; i < 44100*2; i++){
    7.26 +    sound_store[i] = (jbyte) soundBytes[i];
    7.27    }
    7.28 -  env->ReleaseIntArrayElements(arr, sound_store, 0);
    7.29 +  
    7.30 +  /*
    7.31 +    u8* soundBytes = (u8*) soundFinalWave;
    7.32 +    for (i = 0; i < 1470*2 ; i++){
    7.33 +    sound_store[i] = (jbyte) soundBytes[i];
    7.34 +    }
    7.35 +  */
    7.36 +
    7.37 +  env->ReleaseByteArrayElements(arr, sound_store, 0);
    7.38  }
    7.39  
    7.40  
     8.1 --- a/src/gb/GB.h	Sat Jun 23 20:34:14 2012 -0500
     8.2 +++ b/src/gb/GB.h	Sat Jun 23 23:10:31 2012 -0500
     8.3 @@ -66,6 +66,7 @@
     8.4  extern u8 gbReadMemory(u16 address);
     8.5  
     8.6  extern u16	 soundFrameSound[735 * 30 * 2];
     8.7 +extern u16	 soundFinalWave[1470];
     8.8  extern int32 soundFrameSoundWritten;
     8.9  
    8.10  extern struct EmulatedSystem GBSystem;
     9.1 --- a/src/gb/gbSound.cpp	Sat Jun 23 20:34:14 2012 -0500
     9.2 +++ b/src/gb/gbSound.cpp	Sat Jun 23 23:10:31 2012 -0500
     9.3 @@ -5,6 +5,7 @@
     9.4  
     9.5  #include <cstring>
     9.6  #include <cassert>
     9.7 +#include <stdio.h>
     9.8  
     9.9  #include "../common/System.h"
    9.10  #include "../common/Util.h"
    9.11 @@ -729,7 +730,7 @@
    9.12  	{
    9.13  		soundFinalWave[++soundBufferIndex] = res;
    9.14  		if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))
    9.15 -			/*assert(false)*/;
    9.16 +		  printf("oh noes!\n");
    9.17  		else
    9.18  			soundFrameSound[++soundFrameSoundWritten] = res;
    9.19  	}
    9.20 @@ -737,7 +738,7 @@
    9.21  	{
    9.22  		soundFinalWave[soundBufferIndex++] = res;
    9.23  		if (soundFrameSoundWritten >= countof(soundFrameSound))
    9.24 -			/*assert(false)*/;
    9.25 +		  printf("oh noes!\n");
    9.26  		else
    9.27  			soundFrameSound[soundFrameSoundWritten++] = res;
    9.28  	}
    9.29 @@ -816,7 +817,7 @@
    9.30  	{
    9.31  		soundFinalWave[-1 + soundBufferIndex++]		   = res;
    9.32  		if ((soundFrameSoundWritten) >= countof(soundFrameSound))
    9.33 -			/*assert(false)*/;
    9.34 +		  printf("oh noes!\n");
    9.35  		else
    9.36  			soundFrameSound[-1 + soundFrameSoundWritten++] = res;
    9.37  	}
    9.38 @@ -824,7 +825,7 @@
    9.39  	{
    9.40  		soundFinalWave[soundBufferIndex++]			  = res;
    9.41  		if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))
    9.42 -			/*assert(false)*/;
    9.43 +		  printf("oh noes!\n");
    9.44  		else
    9.45  			soundFrameSound[soundFrameSoundWritten++] = res;
    9.46  	}
    9.47 @@ -848,7 +849,8 @@
    9.48  			soundFinalWave[soundBufferIndex++] = 0;
    9.49  			soundFinalWave[soundBufferIndex++] = 0;
    9.50  			if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))
    9.51 -				/*assert(false)*/;
    9.52 +			  
    9.53 +			printf("oh noes!\n");
    9.54  			else
    9.55  			{
    9.56  				soundFrameSound[soundFrameSoundWritten++] = 0;
    10.1 Binary file test-sound.wav has changed