changeset 525:fa7676dbf6f2

sound recording test now works.
author Robert McIntyre <rlm@mit.edu>
date Sun, 24 Jun 2012 13:12:51 -0500
parents 7ef5c73ea8fa
children 2620d6318e8d
files clojure/com/aurellem/gb/gb_driver.clj clojure/com/aurellem/run/sound.clj java/src/com/aurellem/gb/Gb.java src/clojure/clojure.cpp src/gb/GB.cpp src/gb/GB.h src/gb/gbSound.cpp src/sdl/SDL.cpp src/sdl/SoundSDL.cpp test-sound.wav
diffstat 10 files changed, 160 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/gb_driver.clj	Sat Jun 23 23:10:31 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/gb_driver.clj	Sun Jun 24 13:12:51 2012 -0500
     1.3 @@ -199,9 +199,16 @@
     1.4      ([state]
     1.5         (set-state! state)
     1.6         (Gb/getFrameSound store)
     1.7 -       (Gb/setSoundFrameWritten 0)
     1.8         store)))
     1.9 -  
    1.10 +
    1.11 +(let [store (byte-array (* 1470 2))]
    1.12 +  (defn sound-data-2
    1.13 +    ([](sound-data-2 @current-state))
    1.14 +    ([state]
    1.15 +       (set-state! state)
    1.16 +       (Gb/getFrameSound2 store)
    1.17 +       store)))
    1.18 +
    1.19  (def memory
    1.20    (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
    1.21  
     2.1 --- a/clojure/com/aurellem/run/sound.clj	Sat Jun 23 23:10:31 2012 -0500
     2.2 +++ b/clojure/com/aurellem/run/sound.clj	Sun Jun 24 13:12:51 2012 -0500
     2.3 @@ -7,8 +7,9 @@
     2.4    (:require clojure.string)
     2.5    (:import [com.aurellem.gb.gb_driver SaveState])
     2.6    (:import java.awt.image.BufferedImage)
     2.7 -  (:import java.io.File))
     2.8 -
     2.9 +  (:import java.io.File)
    2.10 +  (:import javax.sound.sampled.AudioFormat)
    2.11 +  (:import com.aurellem.gb.WaveWriter))
    2.12  
    2.13  (defn sound-test []
    2.14    (step (mid-game))
    2.15 @@ -16,10 +17,6 @@
    2.16    (run-moves @current-state (repeat 10 []))
    2.17    (println (frequencies (sound-data))))
    2.18  
    2.19 -
    2.20 -(import javax.sound.sampled.AudioFormat)
    2.21 -(import com.aurellem.gb.WaveWriter)
    2.22 -
    2.23  (def probable-format (AudioFormat. 44100 16 2 true false))
    2.24  
    2.25  (defn test-writing-file! [n]
    2.26 @@ -30,16 +27,26 @@
    2.27      (dorun
    2.28       (for [y (range n)]
    2.29         (do
    2.30 -         (let [quanta 30]
    2.31 +         (let [quanta 1]
    2.32             (run-moves @current-state (repeat quanta []))
    2.33 -           (let [data (sound-data)
    2.34 +           (let [
    2.35 +                 
    2.36 +                 data (sound-data)
    2.37 +                 bytes (* 2 (com.aurellem.gb.Gb/getSoundFrameWritten))
    2.38                   step-section
    2.39 -                 (byte-array (take (* 2940 quanta) data))]
    2.40 -             
    2.41 -             (.process writer step-section probable-format))))))
    2.42 +                 (byte-array
    2.43 +                  (take (* bytes quanta) data))
    2.44 +
    2.45 +
    2.46 +                 ]
    2.47 +             (com.aurellem.gb.Gb/setSoundFrameWritten 0)
    2.48 +             (.process writer
    2.49 +                       ;;data
    2.50 +                       step-section
    2.51 +                       probable-format))))))
    2.52       (.cleanup writer)
    2.53       (Thread/sleep 1000)
    2.54       (clojure.java.shell/sh
    2.55        "aplay"
    2.56        (.getCanonicalPath target-file))))
    2.57 -      
    2.58 \ No newline at end of file
    2.59 +
     3.1 --- a/java/src/com/aurellem/gb/Gb.java	Sat Jun 23 23:10:31 2012 -0500
     3.2 +++ b/java/src/com/aurellem/gb/Gb.java	Sun Jun 24 13:12:51 2012 -0500
     3.3 @@ -141,4 +141,6 @@
     3.4  
     3.5      public static native void setSoundFrameWritten(int frames);
     3.6  
     3.7 +    public static native void getFrameSound2(byte[] store);
     3.8 +
     3.9  }
     4.1 --- a/src/clojure/clojure.cpp	Sat Jun 23 23:10:31 2012 -0500
     4.2 +++ b/src/clojure/clojure.cpp	Sun Jun 24 13:12:51 2012 -0500
     4.3 @@ -365,10 +365,10 @@
     4.4    }
     4.5    
     4.6    /*
     4.7 -    u8* soundBytes = (u8*) soundFinalWave;
     4.8 -    for (i = 0; i < 1470*2 ; i++){
     4.9 +  u8* soundBytes = (u8*) soundFinalWave;
    4.10 +  for (i = 0; i < 1470*2 ; i++){
    4.11      sound_store[i] = (jbyte) soundBytes[i];
    4.12 -    }
    4.13 +  }
    4.14    */
    4.15  
    4.16    env->ReleaseByteArrayElements(arr, sound_store, 0);
    4.17 @@ -395,3 +395,23 @@
    4.18    soundFrameSoundWritten = newSoundFrameWritten;
    4.19  }
    4.20  
    4.21 +
    4.22 +/*
    4.23 + * Class:     com_aurellem_gb_Gb
    4.24 + * Method:    getFrameSound2
    4.25 + * Signature: ([B)V
    4.26 + */
    4.27 +JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getFrameSound2
    4.28 +(JNIEnv *env, jclass clazz, jbyteArray arr){
    4.29 +  setbuf(stdout, NULL);
    4.30 +
    4.31 +  jbyte *sound_store = env->GetByteArrayElements(arr, 0);
    4.32 +  int i;
    4.33 +
    4.34 +  for (i = 0; i < 1470*2; i++){
    4.35 +    sound_store[i] = (jbyte) soundCopyBuffer[i];
    4.36 +  }
    4.37 +  
    4.38 +  env->ReleaseByteArrayElements(arr, sound_store, 0);
    4.39 +
    4.40 +}
     5.1 --- a/src/gb/GB.cpp	Sat Jun 23 23:10:31 2012 -0500
     5.2 +++ b/src/gb/GB.cpp	Sun Jun 24 13:12:51 2012 -0500
     5.3 @@ -3223,7 +3223,7 @@
     5.4  	}
     5.5  
     5.6        extButtons = (newmask >> 18);
     5.7 -      speedup	   = (extButtons & 1) != 0;
     5.8 +      speedup	   =  (extButtons & 1) != 0;
     5.9  
    5.10        VBAMovieResetIfRequested();
    5.11        //printf("RLM: before Lua functions\n");
    5.12 @@ -3753,7 +3753,7 @@
    5.13        while (soundTicks < 0) // must be < 1 when soundtick_t is real data type
    5.14  	{
    5.15  	  soundTicks += SOUND_CLOCK_TICKS;
    5.16 -
    5.17 +	  printf("gbSoundTick()\n");
    5.18  	  gbSoundTick();
    5.19  	}
    5.20  
     6.1 --- a/src/gb/GB.h	Sat Jun 23 23:10:31 2012 -0500
     6.2 +++ b/src/gb/GB.h	Sun Jun 24 13:12:51 2012 -0500
     6.3 @@ -68,6 +68,8 @@
     6.4  extern u16	 soundFrameSound[735 * 30 * 2];
     6.5  extern u16	 soundFinalWave[1470];
     6.6  extern int32 soundFrameSoundWritten;
     6.7 +extern u8        soundCopyBuffer[1470 * 2];
     6.8 +
     6.9  
    6.10  extern struct EmulatedSystem GBSystem;
    6.11  extern struct EmulatedSystemCounters &GBSystemCounters;
     7.1 --- a/src/gb/gbSound.cpp	Sat Jun 23 23:10:31 2012 -0500
     7.2 +++ b/src/gb/gbSound.cpp	Sun Jun 24 13:12:51 2012 -0500
     7.3 @@ -730,7 +730,8 @@
     7.4  	{
     7.5  		soundFinalWave[++soundBufferIndex] = res;
     7.6  		if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))
     7.7 -		  printf("oh noes!\n");
     7.8 +		  
     7.9 +		  ; //printf("oh noes!\n");
    7.10  		else
    7.11  			soundFrameSound[++soundFrameSoundWritten] = res;
    7.12  	}
    7.13 @@ -738,7 +739,7 @@
    7.14  	{
    7.15  		soundFinalWave[soundBufferIndex++] = res;
    7.16  		if (soundFrameSoundWritten >= countof(soundFrameSound))
    7.17 -		  printf("oh noes!\n");
    7.18 +		  ; //printf("oh noes!\n");
    7.19  		else
    7.20  			soundFrameSound[soundFrameSoundWritten++] = res;
    7.21  	}
    7.22 @@ -817,7 +818,7 @@
    7.23  	{
    7.24  		soundFinalWave[-1 + soundBufferIndex++]		   = res;
    7.25  		if ((soundFrameSoundWritten) >= countof(soundFrameSound))
    7.26 -		  printf("oh noes!\n");
    7.27 +		  ;//printf("oh noes!\n");
    7.28  		else
    7.29  			soundFrameSound[-1 + soundFrameSoundWritten++] = res;
    7.30  	}
    7.31 @@ -825,7 +826,7 @@
    7.32  	{
    7.33  		soundFinalWave[soundBufferIndex++]			  = res;
    7.34  		if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))
    7.35 -		  printf("oh noes!\n");
    7.36 +		  ;//printf("oh noes!\n");
    7.37  		else
    7.38  			soundFrameSound[soundFrameSoundWritten++] = res;
    7.39  	}
    7.40 @@ -833,49 +834,49 @@
    7.41  
    7.42  void gbSoundTick()
    7.43  {
    7.44 -	if (systemSoundOn)
    7.45 +  if (systemSoundOn)
    7.46 +    {
    7.47 +      if (soundMasterOn)
    7.48  	{
    7.49 -		if (soundMasterOn)
    7.50 +	  gbSoundChannel1();
    7.51 +	  gbSoundChannel2();
    7.52 +	  gbSoundChannel3();
    7.53 +	  gbSoundChannel4();
    7.54 +
    7.55 +	  gbSoundMix();
    7.56 +	}
    7.57 +      else
    7.58 +	{
    7.59 +	  soundFinalWave[soundBufferIndex++] = 0;
    7.60 +	  soundFinalWave[soundBufferIndex++] = 0;
    7.61 +	  if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))
    7.62 +			  
    7.63 +	    ;//printf("oh noes!\n");
    7.64 +	  else
    7.65 +	    {
    7.66 +	      soundFrameSound[soundFrameSoundWritten++] = 0;
    7.67 +	      soundFrameSound[soundFrameSoundWritten++] = 0;
    7.68 +	    }
    7.69 +	}
    7.70 +
    7.71 +      soundIndex++;
    7.72 +
    7.73 +      if (2 * soundBufferIndex >= soundBufferLen)
    7.74 +	{
    7.75 +	  if (systemSoundOn)
    7.76 +	    {
    7.77 +	      if (soundPaused)
    7.78  		{
    7.79 -			gbSoundChannel1();
    7.80 -			gbSoundChannel2();
    7.81 -			gbSoundChannel3();
    7.82 -			gbSoundChannel4();
    7.83 -
    7.84 -			gbSoundMix();
    7.85 -		}
    7.86 -		else
    7.87 -		{
    7.88 -			soundFinalWave[soundBufferIndex++] = 0;
    7.89 -			soundFinalWave[soundBufferIndex++] = 0;
    7.90 -			if ((soundFrameSoundWritten + 1) >= countof(soundFrameSound))
    7.91 -			  
    7.92 -			printf("oh noes!\n");
    7.93 -			else
    7.94 -			{
    7.95 -				soundFrameSound[soundFrameSoundWritten++] = 0;
    7.96 -				soundFrameSound[soundFrameSoundWritten++] = 0;
    7.97 -			}
    7.98 +		  extern void soundResume();
    7.99 +		  soundResume();
   7.100  		}
   7.101  
   7.102 -		soundIndex++;
   7.103 -
   7.104 -		if (2 * soundBufferIndex >= soundBufferLen)
   7.105 -		{
   7.106 -			if (systemSoundOn)
   7.107 -			{
   7.108 -				if (soundPaused)
   7.109 -				{
   7.110 -					extern void soundResume();
   7.111 -					soundResume();
   7.112 -				}
   7.113 -
   7.114 -				systemSoundWriteToBuffer();
   7.115 -			}
   7.116 -			soundIndex		 = 0;
   7.117 -			soundBufferIndex = 0;
   7.118 -		}
   7.119 +	      systemSoundWriteToBuffer();
   7.120 +	    }
   7.121 +	  soundIndex		 = 0;
   7.122 +	  soundBufferIndex = 0;
   7.123  	}
   7.124 +    }
   7.125  }
   7.126  
   7.127  void gbSoundReset()
     8.1 --- a/src/sdl/SDL.cpp	Sat Jun 23 23:10:31 2012 -0500
     8.2 +++ b/src/sdl/SDL.cpp	Sun Jun 24 13:12:51 2012 -0500
     8.3 @@ -3092,10 +3092,24 @@
     8.4  
     8.5  void soundCallback(void *,u8 *stream,int len){}
     8.6  
     8.7 +
     8.8 +int writeCounter = 0;
     8.9 +u8 soundCopyBuffer[1470 * 2];
    8.10 +
    8.11 +
    8.12 +
    8.13  void systemSoundWriteToBuffer(){
    8.14 -	soundDriver->write(soundFinalWave, soundBufferLen);  
    8.15 +  //printf("sound write counter (len) : %07d (%d)\n", 
    8.16 +  //writeCounter++, soundBufferLen);
    8.17 +  //int i;
    8.18 +  //u8* soundBytes = (u8*) soundFinalWave;
    8.19 +  //for (i = 0; i <   1470 * 2; i++){
    8.20 +  //  soundCopyBuffer[i] = soundBytes[i];
    8.21 +  //}
    8.22 +  soundDriver->write(soundFinalWave, soundBufferLen);  
    8.23  }
    8.24  
    8.25 +
    8.26  void systemSoundClearBuffer()
    8.27  {
    8.28  	SDL_mutexP(mutex);
     9.1 --- a/src/sdl/SoundSDL.cpp	Sat Jun 23 23:10:31 2012 -0500
     9.2 +++ b/src/sdl/SoundSDL.cpp	Sun Jun 24 13:12:51 2012 -0500
     9.3 @@ -16,6 +16,8 @@
     9.4  // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     9.5  
     9.6  #include "SoundSDL.h"
     9.7 +#include <stdio.h>
     9.8 +
     9.9  
    9.10  extern int emulating;
    9.11  extern bool speedup;
    9.12 @@ -37,53 +39,61 @@
    9.13  
    9.14  void SoundSDL::read(u16 * stream, int length)
    9.15  {
    9.16 -	if (!_initialized || length <= 0 || !emulating)
    9.17 -		return;
    9.18 +  if (!_initialized || length <= 0 || !emulating)
    9.19 +    return;
    9.20  
    9.21 -	SDL_mutexP(_mutex);
    9.22 -	_rbuf.read(stream, std::min(static_cast<std::size_t>(length) / 2, _rbuf.used()));
    9.23 +  SDL_mutexP(_mutex);
    9.24 +  _rbuf.read(stream, std::min(static_cast<std::size_t>(length) / 2, _rbuf.used()));
    9.25  
    9.26 -	SDL_CondSignal(_cond);
    9.27 -	SDL_mutexV(_mutex);
    9.28 +  SDL_CondSignal(_cond);
    9.29 +  SDL_mutexV(_mutex);
    9.30  }
    9.31  
    9.32  void SoundSDL::write(u16 * finalWave, int length)
    9.33  {
    9.34 -	if (!_initialized)
    9.35 -		return;
    9.36 +  if (!_initialized)
    9.37 +    return;
    9.38  
    9.39 -	if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)
    9.40 -		SDL_PauseAudio(0);
    9.41 +  if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING){
    9.42 +    SDL_PauseAudio(0);
    9.43 +    printf("SDLPauseAudio\n");
    9.44 +  }
    9.45  
    9.46 -	SDL_mutexP(_mutex);
    9.47 +  SDL_mutexP(_mutex);
    9.48  
    9.49 -	unsigned int samples = length / 4;
    9.50 +  //printf("RLM: length = %d\n", length);
    9.51  
    9.52 -	std::size_t avail;
    9.53 -	while ((avail = _rbuf.avail() / 2) < samples)
    9.54 +  unsigned int samples = length / 4;
    9.55 +
    9.56 +  // printf("RLM: length / 4 = %d\n", samples);
    9.57 +
    9.58 +  std::size_t avail;
    9.59 +  while ((avail = _rbuf.avail() / 2) < samples)
    9.60 +    {
    9.61 +      _rbuf.write(finalWave, avail * 2);
    9.62 +
    9.63 +      finalWave += avail * 2;
    9.64 +      samples -= avail;
    9.65 +
    9.66 +      // If emulating and not in speed up mode, synchronize to audio
    9.67 +      // by waiting till there is enough room in the buffer
    9.68 +      if (emulating && !speedup)
    9.69  	{
    9.70 -		_rbuf.write(finalWave, avail * 2);
    9.71 +	  //printf("SDL_CondWait\n");
    9.72 +	  SDL_CondWait(_cond,_mutex);
    9.73 +	}
    9.74 +      else
    9.75 +	{
    9.76 +	  // Drop the remainder of the audio data
    9.77 +	  printf("RLM: Drop samples!\n");
    9.78 +	  SDL_mutexV(_mutex);
    9.79 +	  return;
    9.80 +	}
    9.81 +    }
    9.82  
    9.83 -		finalWave += avail * 2;
    9.84 -		samples -= avail;
    9.85 -
    9.86 -		// If emulating and not in speed up mode, synchronize to audio
    9.87 -		// by waiting till there is enough room in the buffer
    9.88 -		if (emulating && !speedup)
    9.89 -		{
    9.90 -			SDL_CondWait(_cond,_mutex);
    9.91 -		}
    9.92 -		else
    9.93 -		{
    9.94 -			// Drop the remaining of the audio data
    9.95 -			SDL_mutexV(_mutex);
    9.96 -			return;
    9.97 -		}
    9.98 -	}
    9.99 -
   9.100 -	_rbuf.write(finalWave, samples * 2);
   9.101 -
   9.102 -	SDL_mutexV(_mutex);
   9.103 +  _rbuf.write(finalWave, samples * 2);
   9.104 +  //printf("RLM: writing %d samples\n", samples);
   9.105 +  SDL_mutexV(_mutex);
   9.106  }
   9.107  
   9.108  
    10.1 Binary file test-sound.wav has changed