| 
									
										
										
										
											2009-07-28 21:32:10 +00:00
										 |  |  | // Copyright (C) 2003 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, version 2.0.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU General Public License 2.0 for more details.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // A copy of the GPL 2.0 should have been included with the program.
 | 
					
						
							|  |  |  | // If not, see http://www.gnu.org/licenses/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Official SVN repository and contact information can be found at
 | 
					
						
							|  |  |  | // http://code.google.com/p/dolphin-emu/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Common.h"
 | 
					
						
							|  |  |  | #include "WaveFile.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum {BUF_SIZE = 32*1024}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WaveFileWriter::WaveFileWriter() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-03-28 23:04:57 +00:00
										 |  |  | 	file = NULL; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	conv_buffer = 0; | 
					
						
							|  |  |  | 	skip_silence = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WaveFileWriter::~WaveFileWriter() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	delete [] conv_buffer; | 
					
						
							|  |  |  | 	Stop(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool WaveFileWriter::Start(const char *filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!conv_buffer) | 
					
						
							|  |  |  | 		conv_buffer = new short[BUF_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	// Check if the file is already open
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	if (file) | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-01-15 02:30:36 +00:00
										 |  |  | 		PanicAlertT("The file %s was already open, the file header will not be written.", filename); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	file = fopen(filename, "wb"); | 
					
						
							|  |  |  | 	if (!file) | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 		PanicAlertT("The file %s could not be opened for writing. Please check if it's already opened by another program.", filename); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 	// -----------------
 | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	// Write file header
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 	// -----------------
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	Write4("RIFF"); | 
					
						
							|  |  |  | 	Write(100 * 1000 * 1000);  // write big value in case the file gets truncated
 | 
					
						
							|  |  |  | 	Write4("WAVE"); | 
					
						
							|  |  |  | 	Write4("fmt "); | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	Write(16);  // size of fmt block
 | 
					
						
							|  |  |  | 	Write(0x00020001); //two channels, uncompressed
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	//const u32 sample_rate = 32000;
 | 
					
						
							|  |  |  | 	const u32 sample_rate = 48000; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	Write(sample_rate); | 
					
						
							|  |  |  | 	Write(sample_rate * 2 * 2); //two channels, 16bit
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	Write(0x00100004); | 
					
						
							|  |  |  | 	Write4("data"); | 
					
						
							|  |  |  | 	Write(100 * 1000 * 1000 - 32); | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	// We are now at offset 44
 | 
					
						
							| 
									
										
										
										
											2010-12-04 03:50:55 +00:00
										 |  |  | 	if (ftello(file) != 44) | 
					
						
							| 
									
										
										
										
											2010-12-05 15:59:11 +00:00
										 |  |  | 		PanicAlert("wrong offset: %lld", (long long)ftello(file)); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WaveFileWriter::Stop() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!file) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2010-12-04 03:50:55 +00:00
										 |  |  | 	// u32 file_size = (u32)ftello(file);
 | 
					
						
							|  |  |  | 	fseeko(file, 4, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	Write(audio_size + 36); | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-04 03:50:55 +00:00
										 |  |  | 	fseeko(file, 40, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	Write(audio_size); | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	fclose(file); | 
					
						
							|  |  |  | 	file = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WaveFileWriter::Write(u32 value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	fwrite(&value, 4, 1, file); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WaveFileWriter::Write4(const char *ptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	fwrite(ptr, 4, 1, file); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WaveFileWriter::AddStereoSamples(const short *sample_data, int count) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!file) | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 		PanicAlertT("WaveFileWriter - file not open."); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	if (skip_silence) { | 
					
						
							|  |  |  | 		bool all_zero = true; | 
					
						
							|  |  |  | 		for (int i = 0; i < count * 2; i++) | 
					
						
							| 
									
										
										
										
											2009-02-14 01:07:20 +00:00
										 |  |  | 			if (sample_data[i]) all_zero = false; | 
					
						
							|  |  |  | 		if (all_zero) return; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	fwrite(sample_data, count * 4, 1, file); | 
					
						
							|  |  |  | 	audio_size += count * 4; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void WaveFileWriter::AddStereoSamplesBE(const short *sample_data, int count) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!file) | 
					
						
							| 
									
										
										
										
											2011-01-13 02:05:58 +00:00
										 |  |  | 		PanicAlertT("WaveFileWriter - file not open."); | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	if (count > BUF_SIZE * 2) | 
					
						
							|  |  |  | 		PanicAlert("WaveFileWriter - buffer too small (count = %i).", count); | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (skip_silence)  | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		bool all_zero = true; | 
					
						
							|  |  |  | 		for (int i = 0; i < count * 2; i++) | 
					
						
							|  |  |  | 			if (sample_data[i]) | 
					
						
							|  |  |  | 				all_zero = false; | 
					
						
							|  |  |  | 		if (all_zero) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < count * 2; i++) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		conv_buffer[i] = Common::swap16((u16)sample_data[i]); | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	fwrite(conv_buffer, count * 4, 1, file); | 
					
						
							|  |  |  | 	audio_size += count * 4; | 
					
						
							|  |  |  | } |