Skip to content

Commit f3be07c

Browse files
authored
Merge pull request #50 from benrr101/hotfix/aiff-file-size
Hotfix: AIFF File Size Fix
2 parents 4e916fd + ab88655 commit f3be07c

3 files changed

Lines changed: 16 additions & 29 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "node-taglib-sharp",
33
"description": "Read and write audio/video/picture tags using a similar interface to TagLib#",
4-
"version": "4.0.0",
4+
"version": "4.0.1",
55
"license": "LGPL-2.1-or-later",
66
"author": "Ben Russell <benrr101@outlook.com> (https://github.com/benrr101)",
77
"repository": "github:benrr101/node-taglib-sharp",

src/aiff/aiffFile.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,17 @@ export default class AiffFile extends File {
129129
const readResult = this.read(false, ReadStyle.None);
130130

131131
// If tagging info cannot be found, place it at the end of the file
132-
if (readResult.tagStart < 12 || readResult.tagEnd < readResult.tagStart) {
132+
if (readResult.tagStart < 0 || readResult.tagEnd < 0) {
133133
readResult.tagStart = readResult.tagEnd = this.length;
134134
}
135-
let length = readResult.tagEnd - readResult.tagStart + 8;
135+
const originalTagChunkLength = readResult.tagEnd - readResult.tagStart + 8;
136136

137137
// Insert the tagging data
138-
this.insert(id3Chunk, readResult.tagStart, length);
138+
this.insert(id3Chunk, readResult.tagStart, originalTagChunkLength);
139139

140-
// If the data size changed, update the AIFF size
141-
if (id3Chunk.length - length !== 0 && readResult.tagStart <= readResult.aiffSize) {
142-
// Depending, if a tag has been added or removed, the length needs to be adjusted
143-
if (!this._tag) {
144-
length -= 16;
145-
} else {
146-
length -= 8;
147-
}
148-
149-
this.insert(ByteVector.fromUInt(readResult.aiffSize + id3Chunk.length - length, true), 4, 4);
150-
}
140+
// Update the AIFF size
141+
const aiffSize = this.length - 8;
142+
this.insert(ByteVector.fromUInt(aiffSize, true), 4, 4);
151143

152144
// Update the tag types
153145
this._tagTypesOnDisk = this.tagTypes;
@@ -189,7 +181,7 @@ export default class AiffFile extends File {
189181
}
190182
}
191183

192-
private read(readTags: boolean, style: ReadStyle): {aiffSize: number, tagEnd: number, tagStart: number} {
184+
private read(readTags: boolean, style: ReadStyle): {fileSize: number, tagEnd: number, tagStart: number} {
193185
this.seek(0);
194186
if (!ByteVector.equal(this.readBlock(4), AiffFile.fileIdentifier)) {
195187
throw new CorruptFileError("File does not begin with AIFF identifier");
@@ -203,11 +195,10 @@ export default class AiffFile extends File {
203195
if (!ByteVector.equal(this.readBlock(4), AiffFile.aiffFormType)) {
204196
throw new CorruptFileError("File form type is not AIFF");
205197
}
206-
const formBlockChunkPosition = this.position;
207198

208199
// Read the properties of the file
209200
if (!this._headerBlock && style !== ReadStyle.None) {
210-
const commonChunkPos = this.findChunk(AiffFile.commIdentifier, formBlockChunkPosition);
201+
const commonChunkPos = this.findChunk(AiffFile.commIdentifier, this.position);
211202
if (commonChunkPos === -1) {
212203
throw new CorruptFileError("No common chunk available in this AIFF file");
213204
}
@@ -219,21 +210,16 @@ export default class AiffFile extends File {
219210
this._properties = new Properties(0, [header]);
220211
}
221212

222-
// Search for the ID3 chunk
223-
const id3ChunkPos = this.findChunk(AiffFile.id3Identifier, formBlockChunkPosition);
224-
225213
// Search for the sound chunk
226-
const soundChunkPos = this.findChunk(AiffFile.soundIdentifier, formBlockChunkPosition);
227-
228-
// Ensure there is a sound chunk for the file to be valid
214+
const soundChunkPos = this.findChunk(AiffFile.soundIdentifier, this.position);
229215
if (soundChunkPos === -1) {
230216
throw new CorruptFileError("No sound chunk available in this AIFF file");
231217
}
218+
this.seek(this.position + 4);
219+
const soundChunkLength = this.readBlock(4).toUInt();
232220

233-
// Get the length of the sound chunk and use this as a start value to look for ID3 chunk
234-
this.seek(soundChunkPos + 4);
235-
236-
// Read the ID3 chunk
221+
// Search for the ID3 chunk
222+
const id3ChunkPos = this.findChunk(AiffFile.id3Identifier, this.position + soundChunkLength);
237223
if (id3ChunkPos >= 0) {
238224
if (readTags && !this._tag) {
239225
this._tag = Id3v2Tag.fromFileStart(this, id3ChunkPos + 8, style);
@@ -248,7 +234,7 @@ export default class AiffFile extends File {
248234
}
249235

250236
return {
251-
aiffSize: aiffSize,
237+
fileSize: this.length,
252238
tagEnd: tagEnd,
253239
tagStart: tagStart
254240
};

src/aiff/aiffStreamHeader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class AiffStreamHeader implements ILosslessAudioCodec {
5555
// #region Properties
5656

5757
/** @inheritDoc */
58+
// @TODO: streamlength is total file data length, not sound length.
5859
public get audioBitrate(): number {
5960
return this.durationMilliseconds <= 0
6061
? 0

0 commit comments

Comments
 (0)