@@ -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 } ;
0 commit comments