changeset 526:2620d6318e8d

sound recording complete.
author Robert McIntyre <rlm@mit.edu>
date Sun, 24 Jun 2012 13:34:15 -0500
parents fa7676dbf6f2
children 8960150ec761
files clojure/com/aurellem/gb/gb_driver.clj clojure/com/aurellem/run/sound.clj java/src/com/aurellem/gb/Gb.java sounds/mother.wav sounds/pony.wav sounds/regret.wav test-sound.wav todo.org
diffstat 8 files changed, 53 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/gb_driver.clj	Sun Jun 24 13:12:51 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/gb_driver.clj	Sun Jun 24 13:34:15 2012 -0500
     1.3 @@ -1,6 +1,7 @@
     1.4  (ns com.aurellem.gb.gb-driver
     1.5      (:import com.aurellem.gb.Gb)
     1.6      (:import java.io.File)
     1.7 +    (:import javax.sound.sampled.AudioFormat)
     1.8      (:import org.apache.commons.io.FileUtils)
     1.9      (:import (java.nio IntBuffer ByteOrder)))
    1.10  
    1.11 @@ -193,21 +194,21 @@
    1.12      ([new-data]
    1.13         (store-data @current-state new-data))))
    1.14  
    1.15 -(let [store (byte-array Gb/SOUND_SIZE)]
    1.16 -  (defn sound-data 
    1.17 -    ([](sound-data @current-state))
    1.18 +(def gb-sound-format
    1.19 +  "44100 hertz, linear PCM, 2 channels with 16 bits per sample."
    1.20 +  (AudioFormat. 44100 16 2 true false))
    1.21 +
    1.22 +(let [store (byte-array Gb/MAX_SOUND_BYTES)]
    1.23 +  (defn sound-bytes
    1.24 +    "Returns a byte array containting the sound samples
    1.25 +     generated this step."
    1.26 +    ([](sound-bytes @current-state))
    1.27      ([state]
    1.28         (set-state! state)
    1.29         (Gb/getFrameSound store)
    1.30 -       store)))
    1.31 -
    1.32 -(let [store (byte-array (* 1470 2))]
    1.33 -  (defn sound-data-2
    1.34 -    ([](sound-data-2 @current-state))
    1.35 -    ([state]
    1.36 -       (set-state! state)
    1.37 -       (Gb/getFrameSound2 store)
    1.38 -       store)))
    1.39 +       (let [actual-bytes (* 2 (Gb/getSoundFrameWritten))]
    1.40 +         (Gb/setSoundFrameWritten 0)
    1.41 +         (byte-array (take actual-bytes store))))))
    1.42  
    1.43  (def memory
    1.44    (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
     2.1 --- a/clojure/com/aurellem/run/sound.clj	Sun Jun 24 13:12:51 2012 -0500
     2.2 +++ b/clojure/com/aurellem/run/sound.clj	Sun Jun 24 13:34:15 2012 -0500
     2.3 @@ -8,45 +8,50 @@
     2.4    (:import [com.aurellem.gb.gb_driver SaveState])
     2.5    (:import java.awt.image.BufferedImage)
     2.6    (:import java.io.File)
     2.7 -  (:import javax.sound.sampled.AudioFormat)
     2.8 +
     2.9    (:import com.aurellem.gb.WaveWriter))
    2.10  
    2.11  (defn sound-test []
    2.12    (step (mid-game))
    2.13 -  (println (frequencies (sound-data)))
    2.14 +  (println (frequencies (sound-bytes)))
    2.15    (run-moves @current-state (repeat 10 []))
    2.16 -  (println (frequencies (sound-data))))
    2.17 +  (println (frequencies (sound-bytes))))
    2.18  
    2.19 -(def probable-format (AudioFormat. 44100 16 2 true false))
    2.20 +(defn record-sound!
    2.21 +  ([^File target-file state n]
    2.22 +     (set-state! state)
    2.23 +     (let [writer (WaveWriter. target-file)]
    2.24 +       (dorun
    2.25 +        (for [y (range n)]
    2.26 +          (do (step)
    2.27 +              (.process writer
    2.28 +                        (sound-bytes)
    2.29 +                        gb-sound-format))))
    2.30 +       (.cleanup writer)
    2.31 +       (Thread/sleep 1000)
    2.32 +       (clojure.java.shell/sh
    2.33 +        "aplay"
    2.34 +        (.getCanonicalPath target-file))))
    2.35 +  ([n] (record-sound!
    2.36 +        (File. "/home/r/proj/vba-clojure/sounds/test-sound.wav")
    2.37 +        (play-midi pony-csv) n)))
    2.38  
    2.39 -(defn test-writing-file! [n]
    2.40 -  (set-state! (play-midi pony-csv))
    2.41 -  (let [target-file
    2.42 -        (File. "/home/r/proj/vba-clojure/test-sound.wav")
    2.43 -        writer (WaveWriter. target-file)]
    2.44 -    (dorun
    2.45 -     (for [y (range n)]
    2.46 -       (do
    2.47 -         (let [quanta 1]
    2.48 -           (run-moves @current-state (repeat quanta []))
    2.49 -           (let [
    2.50 -                 
    2.51 -                 data (sound-data)
    2.52 -                 bytes (* 2 (com.aurellem.gb.Gb/getSoundFrameWritten))
    2.53 -                 step-section
    2.54 -                 (byte-array
    2.55 -                  (take (* bytes quanta) data))
    2.56 +(defn gen-pony! []
    2.57 +  (record-sound!
    2.58 +   (File. user-home "proj/vba-clojure/sounds/pony.wav")
    2.59 +   (play-midi pony-csv)
    2.60 +   1800))
    2.61  
    2.62 +(defn gen-regret! []
    2.63 +  (record-sound!
    2.64 +   (File. user-home "proj/vba-clojure/sounds/regret.wav")
    2.65 +   (play-midi regret-csv)
    2.66 +   3380))
    2.67  
    2.68 -                 ]
    2.69 -             (com.aurellem.gb.Gb/setSoundFrameWritten 0)
    2.70 -             (.process writer
    2.71 -                       ;;data
    2.72 -                       step-section
    2.73 -                       probable-format))))))
    2.74 -     (.cleanup writer)
    2.75 -     (Thread/sleep 1000)
    2.76 -     (clojure.java.shell/sh
    2.77 -      "aplay"
    2.78 -      (.getCanonicalPath target-file))))
    2.79 +(defn gen-mother! []
    2.80 +  (record-sound!
    2.81 +   (File. user-home "proj/vba-clojure/sounds/mother.wav")
    2.82 +   (play-midi mother-csv)
    2.83 +   2200))
    2.84  
    2.85 +
     3.1 --- a/java/src/com/aurellem/gb/Gb.java	Sun Jun 24 13:12:51 2012 -0500
     3.2 +++ b/java/src/com/aurellem/gb/Gb.java	Sun Jun 24 13:34:15 2012 -0500
     3.3 @@ -101,9 +101,7 @@
     3.4  
     3.5      public static final int GB_MEMORY = 0x10000;
     3.6  
     3.7 -    public static final int SOUND_SIZE = 
     3.8 -	44100*2;
     3.9 -    //1470*2;
    3.10 +    public static final int MAX_SOUND_BYTES = 44100*2;
    3.11  
    3.12      public static native void getMemory(int[] store);
    3.13  
     4.1 Binary file sounds/mother.wav has changed
     5.1 Binary file sounds/pony.wav has changed
     6.1 Binary file sounds/regret.wav has changed
     7.1 Binary file test-sound.wav has changed
     8.1 --- a/todo.org	Sun Jun 24 13:12:51 2012 -0500
     8.2 +++ b/todo.org	Sun Jun 24 13:34:15 2012 -0500
     8.3 @@ -1,7 +1,7 @@
     8.4 -* vba-clojure [1/7]
     8.5 +* vba-clojure [2/7]
     8.6    - [X] create ram display
     8.7    - [ ] derpy hack 
     8.8 -  - [ ] sound recording
     8.9 +  - [X] sound recording
    8.10    - [ ] fix unnecessary pauses before take-over
    8.11    - [ ] design demo choreography
    8.12    - [ ] implement choreography