From a0e9e53d0ce2995cfb327f9a82395bbfc1ed73b6 Mon Sep 17 00:00:00 2001
From: nakeee <nakeee@gmail.com>
Date: Fri, 27 Mar 2009 14:26:44 +0000
Subject: [PATCH] more clean up, starting to add recording support to sound
 stream

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2762 8ced0084-cf51-0410-be5f-012b33b47a6e
---
 Source/Core/AudioCommon/Src/AudioCommon.cpp   | 30 +++++++++
 Source/Core/AudioCommon/Src/AudioCommon.h     |  2 +
 Source/Core/AudioCommon/Src/SoundStream.h     | 25 +++++++-
 Source/Plugins/Plugin_DSP_HLE/Src/main.cpp    | 64 +++++--------------
 .../Plugin_DSP_LLE-testing/Src/main.cpp       | 53 +++------------
 5 files changed, 82 insertions(+), 92 deletions(-)

diff --git a/Source/Core/AudioCommon/Src/AudioCommon.cpp b/Source/Core/AudioCommon/Src/AudioCommon.cpp
index e8f60bc175..bc3d30c25f 100644
--- a/Source/Core/AudioCommon/Src/AudioCommon.cpp
+++ b/Source/Core/AudioCommon/Src/AudioCommon.cpp
@@ -48,4 +48,34 @@ SoundStream *InitSoundStream(std::string backend, CMixer *mixer) {
 	return soundStream;
 }
 
+void ShutdownSoundStream() {
+	NOTICE_LOG(DSPHLE, "Shutting down sound stream");
+
+	if (soundStream) {
+		soundStream->Stop();
+		soundStream->StopLogAudio();
+		delete soundStream;
+		soundStream = NULL;
+	}
+	
+	// Check that soundstream already is stopped.
+	while (soundStream) {
+		ERROR_LOG(DSPHLE, "Waiting for sound stream");
+		Common::SleepCurrentThread(2000);
+	}
+	INFO_LOG(DSPHLE, "Done shutting down sound stream");	
+}
+
+std::vector<std::string> GetSoundBackends() {
+	std::vector<std::string> backends;
+	// Add avaliable output options
+	if (DSound::isValid())
+		backends.push_back("DSound");
+	if (AOSound::isValid())
+		backends.push_back("AOSound");
+	backends.push_back("NullSound");
+   
+	return backends;
+}
+
 } // Namespace
diff --git a/Source/Core/AudioCommon/Src/AudioCommon.h b/Source/Core/AudioCommon/Src/AudioCommon.h
index 6bd1fe09f1..a773d8389a 100644
--- a/Source/Core/AudioCommon/Src/AudioCommon.h
+++ b/Source/Core/AudioCommon/Src/AudioCommon.h
@@ -13,6 +13,8 @@ extern SoundStream *soundStream;
 namespace AudioCommon {
 	
 	SoundStream *InitSoundStream(std::string backend, CMixer *mixer = NULL);
+	void ShutdownSoundStream();
+	 std::vector<std::string> GetSoundBackends();
 } // Namespace	
 
 #endif // AUDIO_COMMON
diff --git a/Source/Core/AudioCommon/Src/SoundStream.h b/Source/Core/AudioCommon/Src/SoundStream.h
index afa1605fd8..b107e9be59 100644
--- a/Source/Core/AudioCommon/Src/SoundStream.h
+++ b/Source/Core/AudioCommon/Src/SoundStream.h
@@ -20,6 +20,7 @@
 
 #include "Common.h"
 #include "Mixer.h"
+#include "WaveFile.h"
 
 class SoundStream
 {
@@ -29,7 +30,9 @@ protected:
     // We set this to shut down the sound thread.
     // 0=keep playing, 1=stop playing NOW.
     volatile int threadData;
-    
+    bool m_logAudio;
+	WaveFileWriter g_wave_writer;
+
 public:   
 	SoundStream(CMixer *mixer) : m_mixer(mixer), threadData(0) {}
 	virtual ~SoundStream() { delete m_mixer;}
@@ -40,6 +43,26 @@ public:
 	virtual void SoundLoop() {}
 	virtual void Stop() {}
 	virtual void Update() {}
+	virtual void StartLogAudio(const char *filename) {
+		if (! m_logAudio) {
+			m_logAudio = true;
+			g_wave_writer.Start(filename);
+			g_wave_writer.SetSkipSilence(false);
+			NOTICE_LOG(DSPHLE, "Starting Audio logging");
+		} else {
+			WARN_LOG(DSPHLE, "Audio logging already started");
+		}
+	}
+
+	virtual void StopLogAudio() {
+		if (m_logAudio) {
+			m_logAudio = false;
+			g_wave_writer.Stop();
+			NOTICE_LOG(DSPHLE, "Starting Audio logging");
+		} else {
+			WARN_LOG(DSPHLE, "Audio logging already stopped");
+		}
+	}
 };
 
 #endif
diff --git a/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp b/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp
index 9ca3b214e5..e7d414c6c7 100644
--- a/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp
+++ b/Source/Plugins/Plugin_DSP_HLE/Src/main.cpp
@@ -27,16 +27,12 @@ CDebugger* m_frame = NULL;
 #endif
 
 #include "ChunkFile.h"
-#include "WaveFile.h"
 #include "HLEMixer.h"
 #include "DSPHandler.h"
 #include "Config.h"
 #include "Setup.h"
 #include "StringUtil.h"
 #include "AudioCommon.h"
-#include "AOSoundStream.h"
-#include "DSoundStream.h"
-#include "NullSoundStream.h"
 
 
 // Declarations and definitions
@@ -47,11 +43,6 @@ extern std::vector<std::string> sMailLog, sMailTime;
 
 SoundStream *soundStream = NULL;
 
-// Set this if you want to log audio. search for log_ai in this file to see the filename.
-bool log_ai = false;
-WaveFileWriter g_wave_writer;
-
-
 // Mailbox utility
 struct DSPState
 {
@@ -181,13 +172,13 @@ void DllConfig(HWND _hParent)
 	// (shuffle2) TODO: reparent dlg with DolphinApp
 	ConfigDialog dlg(NULL);
 
-	// Add avaliable output options
-	if (DSound::isValid())
-		dlg.AddBackend("DSound");
-	if (AOSound::isValid())
-		dlg.AddBackend("AOSound");
-	dlg.AddBackend("NullSound");
+	// add backends
+	std::vector<std::string> backends = AudioCommon::GetSoundBackends();
 
+	for (std::vector<std::string>::const_iterator iter = backends.begin(); 
+		 iter != backends.end(); ++iter) {
+		dlg.AddBackend((*iter).c_str());
+	}
 	// Show the window
 	dlg.ShowModal();
 #endif
@@ -208,43 +199,20 @@ void Initialize(void *init)
 	soundStream->GetMixer()->SetThrottle(g_Config.m_EnableThrottle);
 
 	// Start the sound recording
-	if (log_ai)
-	{
-		g_wave_writer.Start("ai_log.wav");
-		g_wave_writer.SetSkipSilence(false);
-	}
+	/*
+	  if (g_Config.record) {
+	  soundStream->StartLogAudio(FULL_DUMP_DIR g_Config.recordFile);
+	  }
+	*/
 }
 
 void DSP_StopSoundStream()
 {
-	/*	
-	if (!soundStream)
-		PanicAlert("Can't stop non running SoundStream!");
-	soundStream->Stop();
-	delete soundStream;
-	soundStream = NULL;
-*/
 }
 
 void Shutdown()
 {
-	NOTICE_LOG(DSPHLE, "Shutting down DSP plugin");
-
-	if (soundStream) {
-		soundStream->Stop();
-		delete soundStream;
-		soundStream = NULL;
-	}
-	
-	// Check that soundstream already is stopped.
-	while (soundStream) {
-		ERROR_LOG(DSPHLE, "Waiting for sound stream");
-		Common::SleepCurrentThread(2000);
-	}
-
-	// Stop the sound recording
-	if (log_ai)
-		g_wave_writer.Stop();
+	AudioCommon::ShutdownSoundStream();
 
 	// Delete the UCodes
 	CDSPHandler::Destroy();
@@ -259,7 +227,7 @@ void Shutdown()
 		m_frame->sMailEnd.clear();
 	}
 #endif
-	INFO_LOG(DSPHLE, "Done shutting down DSP plugin");	
+	
 }
 
 void DoState(unsigned char **ptr, int mode)
@@ -373,9 +341,9 @@ void DSP_SendAIBuffer(unsigned int address, int sample_rate)
 				samples[i] = Memory_Read_U16(address + i * 2);
 			}
 
-			// Write the audio to a file
-			if (log_ai)
-				g_wave_writer.AddStereoSamples(samples, 8);
+			// FIXME: Write the audio to a file
+			//if (log_ai)
+			//				g_wave_writer.AddStereoSamples(samples, 8);
 		}
 		soundStream->GetMixer()->PushSamples(samples, 32 / 4);
 	}
diff --git a/Source/Plugins/Plugin_DSP_LLE-testing/Src/main.cpp b/Source/Plugins/Plugin_DSP_LLE-testing/Src/main.cpp
index 01ce22da9f..c47b0859dd 100644
--- a/Source/Plugins/Plugin_DSP_LLE-testing/Src/main.cpp
+++ b/Source/Plugins/Plugin_DSP_LLE-testing/Src/main.cpp
@@ -17,7 +17,6 @@
 
 
 #include "Common.h" // Common
-#include "WaveFile.h"
 #include "CommonTypes.h"
 #include "Mixer.h"
 
@@ -32,10 +31,6 @@
 #endif
 
 #include "AudioCommon.h"
-#include "AOSoundStream.h"
-#include "DSoundStream.h"
-#include "NullSoundStream.h"
-
 #include "Logging/Logging.h" // For Logging
 
 #ifdef _WIN32
@@ -64,11 +59,6 @@ bool AXTask(u32& _uMail);
 
 bool bCanWork = false;
 
-// Set this if you want to log audio. search for log_ai in this file to see the filename.
-static bool log_ai = false;
-WaveFileWriter g_wave_writer;
-
-
 #ifdef _WIN32
 BOOL APIENTRY DllMain(HINSTANCE hinstDLL, // DLL module handle
 		DWORD dwReason,             // reason called
@@ -124,12 +114,13 @@ void DllConfig(HWND _hParent)
 	// (shuffle2) TODO: reparent dlg with DolphinApp
 	ConfigDialog dlg(NULL);
 
-	// Add avaliable output options
-	if (DSound::isValid())
-		dlg.AddBackend("DSound");
-	if (AOSound::isValid())
-		dlg.AddBackend("AOSound");
-	dlg.AddBackend("NullSound");
+	// add backends
+	std::vector<std::string> backends = AudioCommon::GetSoundBackends();
+
+	for (std::vector<std::string>::const_iterator iter = backends.begin(); 
+		 iter != backends.end(); ++iter) {
+		dlg.AddBackend((*iter).c_str());
+	}
 
 	// Show the window
 	dlg.ShowModal();
@@ -251,12 +242,6 @@ void Initialize(void *init)
 	soundStream = AudioCommon::InitSoundStream(g_Config.sBackend);
 	
 	soundStream->GetMixer()->SetThrottle(g_Config.m_EnableThrottle);
-	// Start the sound recording
-	if (log_ai)
-	{
-		g_wave_writer.Start("ai_log.wav");
-		g_wave_writer.SetSkipSilence(false);
-	}
 }
 
 void DSP_StopSoundStream()
@@ -265,25 +250,7 @@ void DSP_StopSoundStream()
 
 void Shutdown(void)
 {
-	NOTICE_LOG(DSPHLE, "Shutting down DSP plugin");
-
-	if (soundStream) {
-		soundStream->Stop();
-		delete soundStream;
-		soundStream = NULL;
-	}
-	
-	// Check that soundstream already is stopped.
-	while (soundStream) {
-		ERROR_LOG(DSPHLE, "Waiting for sound stream");
-		Common::SleepCurrentThread(2000);
-	}
-
-	// Stop the sound recording
-	if (log_ai)
-		g_wave_writer.Stop();
-	
-	INFO_LOG(DSPHLE, "Done shutting down DSP plugin");	
+	AudioCommon::ShutdownSoundStream();
 }
 
 u16 DSP_WriteControlRegister(u16 _uFlag)
@@ -401,8 +368,8 @@ void DSP_SendAIBuffer(unsigned int address, int sample_rate)
 			}
 
 			// Write the audio to a file
-			if (log_ai)
-				g_wave_writer.AddStereoSamples(samples, 8);
+			//if (log_ai)
+			//				g_wave_writer.AddStereoSamples(samples, 8);
 		}
 		soundStream->GetMixer()->PushSamples(samples, 32 / 4);
 	}