diff src/win32/WavWriter.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/WavWriter.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,92 @@
     1.4 +// WavWriter.cpp: implementation of the WavWriter class.
     1.5 +//
     1.6 +//////////////////////////////////////////////////////////////////////
     1.7 +
     1.8 +#include "stdafx.h"
     1.9 +#include "WavWriter.h"
    1.10 +
    1.11 +#include "../common/Util.h"
    1.12 +
    1.13 +//////////////////////////////////////////////////////////////////////
    1.14 +// Construction/Destruction
    1.15 +//////////////////////////////////////////////////////////////////////
    1.16 +
    1.17 +WavWriter::WavWriter()
    1.18 +{
    1.19 +	m_file    = NULL;
    1.20 +	m_len     = 0;
    1.21 +	m_posSize = 0;
    1.22 +}
    1.23 +
    1.24 +WavWriter::~WavWriter()
    1.25 +{
    1.26 +	if (m_file)
    1.27 +		Close();
    1.28 +}
    1.29 +
    1.30 +void WavWriter::Close()
    1.31 +{
    1.32 +	// calculate the total file length
    1.33 +	u32 len = ftell(m_file)-8;
    1.34 +	fseek(m_file, 4, SEEK_SET);
    1.35 +	u8 data[4];
    1.36 +	utilPutDword(data, len);
    1.37 +	fwrite(data, 1, 4, m_file);
    1.38 +	// write out the size of the data section
    1.39 +	fseek(m_file, m_posSize, SEEK_SET);
    1.40 +	utilPutDword(data, m_len);
    1.41 +	fwrite(data, 1, 4, m_file);
    1.42 +	fclose(m_file);
    1.43 +	m_file = NULL;
    1.44 +}
    1.45 +
    1.46 +bool WavWriter::Open(const char *name)
    1.47 +{
    1.48 +	if (m_file)
    1.49 +		Close();
    1.50 +	m_file = fopen(name, "wb");
    1.51 +
    1.52 +	if (!m_file)
    1.53 +		return false;
    1.54 +	// RIFF header
    1.55 +	u8 data[4] = { 'R', 'I', 'F', 'F' };
    1.56 +	fwrite(data, 1, 4, m_file);
    1.57 +	utilPutDword(data, 0);
    1.58 +	// write 0 for now. Will get filled during close
    1.59 +	fwrite(data, 1, 4, m_file);
    1.60 +	// write WAVE header
    1.61 +	u8 data2[4] = { 'W', 'A', 'V', 'E' };
    1.62 +	fwrite(data2, 1, 4, m_file);
    1.63 +	return true;
    1.64 +}
    1.65 +
    1.66 +void WavWriter::SetFormat(const WAVEFORMATEX *format)
    1.67 +{
    1.68 +	if (m_file == NULL)
    1.69 +		return;
    1.70 +	// write fmt header
    1.71 +	u8 data[4] = { 'f', 'm', 't', ' ' };
    1.72 +	fwrite(data, 1, 4, m_file);
    1.73 +	u32 value = sizeof(WAVEFORMATEX);
    1.74 +	utilPutDword(data, value);
    1.75 +	fwrite(data, 1, 4, m_file);
    1.76 +	fwrite(format, 1, sizeof(WAVEFORMATEX), m_file);
    1.77 +	// start data header
    1.78 +	u8 data2[4] = { 'd', 'a', 't', 'a' };
    1.79 +	fwrite(data2, 1, 4, m_file);
    1.80 +
    1.81 +	m_posSize = ftell(m_file);
    1.82 +	// write 0 for data chunk size. Filled out during Close()
    1.83 +	utilPutDword(data, 0);
    1.84 +	fwrite(data, 1, 4, m_file);
    1.85 +}
    1.86 +
    1.87 +void WavWriter::AddSound(const u8 *data, int len)
    1.88 +{
    1.89 +	if (m_file == NULL)
    1.90 +		return;
    1.91 +	// write a block of sound data
    1.92 +	fwrite(data, 1, len, m_file);
    1.93 +	m_len += len;
    1.94 +}
    1.95 +