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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
* TAudioOutputStream.java
*
* This file is part of Tritonus: http://www.tritonus.org/
*/
/*
* Copyright (c) 2000 by Matthias Pfisterer
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
|<--- this code is formatted to fit into 80 columns --->|
*/
package org.tritonus.share.sampled.file;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import org.tritonus.share.TDebug;
/**
* Base class for classes implementing AudioOutputStream.
*
* @author Matthias Pfisterer
*/
public abstract class TAudioOutputStream
implements AudioOutputStream
{
private AudioFormat m_audioFormat;
private long m_lLength; // in bytes
private long m_lCalculatedLength;
private TDataOutputStream m_dataOutputStream;
private boolean m_bDoBackPatching;
private boolean m_bHeaderWritten;
protected TAudioOutputStream(AudioFormat audioFormat,
long lLength,
TDataOutputStream dataOutputStream,
boolean bDoBackPatching)
{
m_audioFormat = audioFormat;
m_lLength = lLength;
m_lCalculatedLength = 0;
m_dataOutputStream = dataOutputStream;
m_bDoBackPatching = bDoBackPatching;
m_bHeaderWritten = false;
}
public AudioFormat getFormat()
{
return m_audioFormat;
}
/** Gives length of the stream.
* This value is in bytes. It may be AudioSystem.NOT_SPECIFIED
* to express that the length is unknown.
*/
public long getLength()
{
return m_lLength;
}
/** Gives number of bytes already written.
*/
// IDEA: rename this to BytesWritten or something like that ?
public long getCalculatedLength()
{
return m_lCalculatedLength;
}
protected TDataOutputStream getDataOutputStream()
{
return m_dataOutputStream;
}
/** Writes audio data to the destination (file or output stream).
*/
// IDEA: use long?
public int write(byte[] abData, int nOffset, int nLength)
throws IOException
{
if (TDebug.TraceAudioOutputStream)
{
TDebug.out("TAudioOutputStream.write(): wanted length: " + nLength);
}
if (! m_bHeaderWritten)
{
writeHeader();
m_bHeaderWritten = true;
}
// $$fb added
// check that total writes do not exceed specified length
long lTotalLength=getLength();
if (lTotalLength!=AudioSystem.NOT_SPECIFIED && (m_lCalculatedLength+nLength)>lTotalLength) {
if (TDebug.TraceAudioOutputStream) {
TDebug.out("TAudioOutputStream.write(): requested more bytes to write than possible.");
}
nLength=(int) (lTotalLength-m_lCalculatedLength);
// sanity
if (nLength<0) {
nLength=0;
}
}
// TODO: throw an exception if nLength==0 ? (to indicate end of file ?)
if (nLength>0) {
m_dataOutputStream.write(abData, nOffset, nLength);
m_lCalculatedLength += nLength;
}
if (TDebug.TraceAudioOutputStream)
{
TDebug.out("TAudioOutputStream.write(): calculated (total) length: " + m_lCalculatedLength+" bytes = "+(m_lCalculatedLength/getFormat().getFrameSize())+" frames");
}
return nLength;
}
/** Writes the header of the audio file.
*/
protected abstract void writeHeader()
throws IOException;
/** Closes the stream.
* This does write remaining buffered data to the destination,
* backpatch the header, if necessary, and closes the destination.
*/
public void close()
throws IOException
{
if (TDebug.TraceAudioOutputStream)
{
TDebug.out("TAudioOutputStream.close(): called");
}
// flush?
if (m_bDoBackPatching)
{
if (TDebug.TraceAudioOutputStream)
{
TDebug.out("TAudioOutputStream.close(): patching header");
}
patchHeader();
}
m_dataOutputStream.close();
}
protected void patchHeader()
throws IOException
{
TDebug.out("TAudioOutputStream.patchHeader(): called");
// DO NOTHING
}
protected void setLengthFromCalculatedLength()
{
m_lLength = m_lCalculatedLength;
}
}
/*** TAudioOutputStream.java ***/
|