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