/[pcsx2_0.9.7]/branch/debug/0.X/0.9.X/0.9.7/ramdump-lateset/plugins/spu2-x/src/WavFile.cpp
ViewVC logotype

Contents of /branch/debug/0.X/0.9.X/0.9.7/ramdump-lateset/plugins/spu2-x/src/WavFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 314 - (show annotations) (download)
Sun Dec 26 18:56:19 2010 UTC (9 years, 2 months ago) by william
File size: 4204 byte(s)
** merged upstream r4049 (re-integration of GregMiscellaneous branch)
** applied patched to GigTranser.cpp in ZZOgl from r4140 to change 'static int count = 0;' to 'static int path1_count = 0;')
1 /* SPU2-X, A plugin for Emulating the Sound Processing Unit of the Playstation 2
2 * Developed and maintained by the Pcsx2 Development Team.
3 *
4 * The file is based on WavFile.h from SoundTouch library.
5 * Original portions are (c) 2009 by Olli Parviainen (oparviai 'at' iki.fi)
6 *
7 * SPU2-X is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU Lesser General Public License as published by the Free Software Found-
9 * ation, either version 3 of the License, or (at your option) any later version.
10 *
11 * SPU2-X is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with SPU2-X. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Note the file is mostly a copy paste of the WavFile.h from SoundTouch library. It was
20 // shrunken to support only output 16 bits wav files
21
22 #include <stdio.h>
23 #include <stdexcept>
24 #include <string>
25 #include <cstring>
26 #include <assert.h>
27 #include <limits.h>
28
29 #include "WavFile.h"
30
31 using namespace std;
32
33 static const char riffStr[] = "RIFF";
34 static const char waveStr[] = "WAVE";
35 static const char fmtStr[] = "fmt ";
36 static const char dataStr[] = "data";
37
38 //////////////////////////////////////////////////////////////////////////////
39 //
40 // Class WavOutFile
41 //
42
43 WavOutFile::WavOutFile(const char *fileName, int sampleRate, int bits, int channels)
44 {
45 bytesWritten = 0;
46 fptr = fopen(fileName, "wb");
47 if (fptr == NULL)
48 {
49 string msg = "Error : Unable to open file \"";
50 msg += fileName;
51 msg += "\" for writing.";
52 //pmsg = msg.c_str;
53 throw runtime_error(msg);
54 }
55
56 fillInHeader(sampleRate, bits, channels);
57 writeHeader();
58 }
59
60
61 WavOutFile::~WavOutFile()
62 {
63 finishHeader();
64 if (fptr) fclose(fptr);
65 fptr = NULL;
66 }
67
68
69
70 void WavOutFile::fillInHeader(uint sampleRate, uint bits, uint channels)
71 {
72 // fill in the 'riff' part..
73
74 // copy string 'RIFF' to riff_char
75 memcpy(&(header.riff.riff_char), riffStr, 4);
76 // package_len unknown so far
77 header.riff.package_len = 0;
78 // copy string 'WAVE' to wave
79 memcpy(&(header.riff.wave), waveStr, 4);
80
81
82 // fill in the 'format' part..
83
84 // copy string 'fmt ' to fmt
85 memcpy(&(header.format.fmt), fmtStr, 4);
86
87 header.format.format_len = 0x10;
88 header.format.fixed = 1;
89 header.format.channel_number = (short)channels;
90 header.format.sample_rate = (int)sampleRate;
91 header.format.bits_per_sample = (short)bits;
92 header.format.byte_per_sample = (short)(bits * channels / 8);
93 header.format.byte_rate = header.format.byte_per_sample * (int)sampleRate;
94 header.format.sample_rate = (int)sampleRate;
95
96 // fill in the 'data' part..
97
98 // copy string 'data' to data_field
99 memcpy(&(header.data.data_field), dataStr, 4);
100 // data_len unknown so far
101 header.data.data_len = 0;
102 }
103
104
105 void WavOutFile::finishHeader()
106 {
107 // supplement the file length into the header structure
108 header.riff.package_len = bytesWritten + 36;
109 header.data.data_len = bytesWritten;
110
111 writeHeader();
112 }
113
114
115
116 void WavOutFile::writeHeader()
117 {
118 int res;
119
120 // write the supplemented header in the beginning of the file
121 fseek(fptr, 0, SEEK_SET);
122 res = fwrite(&header, sizeof(header), 1, fptr);
123 if (res != 1)
124 {
125 throw runtime_error("Error while writing to a wav file.");
126 }
127
128 // jump back to the end of the file
129 fseek(fptr, 0, SEEK_END);
130 }
131
132
133 void WavOutFile::write(const short *buffer, int numElems)
134 {
135 int res;
136
137 // 16bit format & 16 bit samples
138
139 assert(header.format.bits_per_sample == 16);
140 if (numElems < 1) return; // nothing to do
141
142 res = fwrite(buffer, 2, numElems, fptr);
143
144 if (res != numElems)
145 {
146 throw runtime_error("Error while writing to a wav file.");
147 }
148 bytesWritten += 2 * numElems;
149 }

  ViewVC Help
Powered by ViewVC 1.1.22