annotate src/win32/WavWriter.cpp @ 2:3549bbe597ed

adding makefile.am
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:47 -0600
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 // WavWriter.cpp: implementation of the WavWriter class.
rlm@1 2 //
rlm@1 3 //////////////////////////////////////////////////////////////////////
rlm@1 4
rlm@1 5 #include "stdafx.h"
rlm@1 6 #include "WavWriter.h"
rlm@1 7
rlm@1 8 #include "../common/Util.h"
rlm@1 9
rlm@1 10 //////////////////////////////////////////////////////////////////////
rlm@1 11 // Construction/Destruction
rlm@1 12 //////////////////////////////////////////////////////////////////////
rlm@1 13
rlm@1 14 WavWriter::WavWriter()
rlm@1 15 {
rlm@1 16 m_file = NULL;
rlm@1 17 m_len = 0;
rlm@1 18 m_posSize = 0;
rlm@1 19 }
rlm@1 20
rlm@1 21 WavWriter::~WavWriter()
rlm@1 22 {
rlm@1 23 if (m_file)
rlm@1 24 Close();
rlm@1 25 }
rlm@1 26
rlm@1 27 void WavWriter::Close()
rlm@1 28 {
rlm@1 29 // calculate the total file length
rlm@1 30 u32 len = ftell(m_file)-8;
rlm@1 31 fseek(m_file, 4, SEEK_SET);
rlm@1 32 u8 data[4];
rlm@1 33 utilPutDword(data, len);
rlm@1 34 fwrite(data, 1, 4, m_file);
rlm@1 35 // write out the size of the data section
rlm@1 36 fseek(m_file, m_posSize, SEEK_SET);
rlm@1 37 utilPutDword(data, m_len);
rlm@1 38 fwrite(data, 1, 4, m_file);
rlm@1 39 fclose(m_file);
rlm@1 40 m_file = NULL;
rlm@1 41 }
rlm@1 42
rlm@1 43 bool WavWriter::Open(const char *name)
rlm@1 44 {
rlm@1 45 if (m_file)
rlm@1 46 Close();
rlm@1 47 m_file = fopen(name, "wb");
rlm@1 48
rlm@1 49 if (!m_file)
rlm@1 50 return false;
rlm@1 51 // RIFF header
rlm@1 52 u8 data[4] = { 'R', 'I', 'F', 'F' };
rlm@1 53 fwrite(data, 1, 4, m_file);
rlm@1 54 utilPutDword(data, 0);
rlm@1 55 // write 0 for now. Will get filled during close
rlm@1 56 fwrite(data, 1, 4, m_file);
rlm@1 57 // write WAVE header
rlm@1 58 u8 data2[4] = { 'W', 'A', 'V', 'E' };
rlm@1 59 fwrite(data2, 1, 4, m_file);
rlm@1 60 return true;
rlm@1 61 }
rlm@1 62
rlm@1 63 void WavWriter::SetFormat(const WAVEFORMATEX *format)
rlm@1 64 {
rlm@1 65 if (m_file == NULL)
rlm@1 66 return;
rlm@1 67 // write fmt header
rlm@1 68 u8 data[4] = { 'f', 'm', 't', ' ' };
rlm@1 69 fwrite(data, 1, 4, m_file);
rlm@1 70 u32 value = sizeof(WAVEFORMATEX);
rlm@1 71 utilPutDword(data, value);
rlm@1 72 fwrite(data, 1, 4, m_file);
rlm@1 73 fwrite(format, 1, sizeof(WAVEFORMATEX), m_file);
rlm@1 74 // start data header
rlm@1 75 u8 data2[4] = { 'd', 'a', 't', 'a' };
rlm@1 76 fwrite(data2, 1, 4, m_file);
rlm@1 77
rlm@1 78 m_posSize = ftell(m_file);
rlm@1 79 // write 0 for data chunk size. Filled out during Close()
rlm@1 80 utilPutDword(data, 0);
rlm@1 81 fwrite(data, 1, 4, m_file);
rlm@1 82 }
rlm@1 83
rlm@1 84 void WavWriter::AddSound(const u8 *data, int len)
rlm@1 85 {
rlm@1 86 if (m_file == NULL)
rlm@1 87 return;
rlm@1 88 // write a block of sound data
rlm@1 89 fwrite(data, 1, len, m_file);
rlm@1 90 m_len += len;
rlm@1 91 }
rlm@1 92