1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
// -*- c-basic-offset: 4 -*-
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <[email protected]>,
Chris Cannam <[email protected]>,
Richard Bown <[email protected]>
The moral right of the authors to claim authorship of this work
has been asserted.
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; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "BWFAudioFile.h"
#include "RealTime.h"
#if (__GNUC__ < 3)
#include <strstream>
#define stringstream strstream
#else
#include <sstream>
#endif
using std::cout;
using std::cerr;
using std::endl;
namespace Rosegarden
{
BWFAudioFile::BWFAudioFile(const unsigned int &id,
const std::string &name,
const std::string &fileName):
RIFFAudioFile(id, name, fileName)
{
m_type = WAV;
}
BWFAudioFile::BWFAudioFile(const std::string &fileName,
unsigned int channels = 1,
unsigned int sampleRate = 48000,
unsigned int bytesPerSecond = 6000,
unsigned int bytesPerFrame = 2,
unsigned int bitsPerSample = 16):
RIFFAudioFile(0, "", fileName)
{
m_type = WAV;
m_bitsPerSample = bitsPerSample;
m_sampleRate = sampleRate;
m_bytesPerSecond = bytesPerSecond;
m_bytesPerFrame = bytesPerFrame;
m_channels = channels;
}
BWFAudioFile::~BWFAudioFile()
{}
bool
BWFAudioFile::open()
{
// if already open
if (m_inFile && (*m_inFile))
return true;
m_inFile = new std::ifstream(m_fileName.c_str(),
std::ios::in | std::ios::binary);
if (!(*m_inFile)) {
m_type = UNKNOWN;
return false;
}
// Get the file size and store it for comparison later
//
m_fileSize = m_fileInfo->size();
try {
parseHeader();
} catch (BadSoundFileException s) {
//throw(s);
return false;
}
return true;
}
// Open the file for writing, write out the header and move
// to the data chunk to accept samples. We fill in all the
// totals when we close().
//
bool
BWFAudioFile::write()
{
// close if we're open
if (m_outFile) {
m_outFile->close();
delete m_outFile;
}
// open for writing
m_outFile = new std::ofstream(m_fileName.c_str(),
std::ios::out | std::ios::binary);
if (!(*m_outFile))
return false;
// write out format header chunk and prepare for sample writing
//
writeFormatChunk();
return true;
}
void
BWFAudioFile::close()
{
if (m_outFile == 0)
return ;
m_outFile->seekp(0, std::ios::end);
unsigned int totalSize = m_outFile->tellp();
// seek to first length position
m_outFile->seekp(4, std::ios::beg);
// write complete file size minus 8 bytes to here
putBytes(m_outFile, getLittleEndianFromInteger(totalSize - 8, 4));
// reseek from start forward 40
m_outFile->seekp(40, std::ios::beg);
// write the data chunk size to end
putBytes(m_outFile, getLittleEndianFromInteger(totalSize - 44, 4));
m_outFile->close();
delete m_outFile;
m_outFile = 0;
}
// Set the AudioFile meta data according to WAV file format specification.
//
void
BWFAudioFile::parseHeader()
{
// Read the format chunk and populate the file data. A plain WAV
// file only has this chunk. Exceptions tumble through.
//
readFormatChunk();
}
std::streampos
BWFAudioFile::getDataOffset()
{
return 0;
}
}
|