Concatenating binary files?

Jimmie Houchin jhouchin at texoma.net
Mon Mar 31 22:15:00 UTC 2003


Hello Tim,

Thanks for the reply.

I appreciate the best practice information. I am slowly working through 
Beck's book. I'll take some time reading your information below so that 
I learn it well.

In the mean time I copied and pasted your code and it worked right out 
of the box. (after editing some email end of lines on the fileDir string)

It had the wave order backwords, but that is insignificant to the lesson.

Thanks again for the education. Now off to learn the lesson. :)

Jimmie Houchin

Tim Rowledge wrote:
>>"Concatenate 2 Wave Files"
>>| fileDir wave1 wave2 wave3 file1 file2 file3 |
>>fileDir := FileDirectory on: 'C:\Documents and Settings\Jimmie\My 
>>Documents\My Music\MP3\Alexander Scourby\waves'.
> 
> First problem; you should use
> FileDirectory default directoryNamed:'C:\Documents and
>  Settings\Jimmie\My
> Documents\My Music\MP3\Alexander Scourby\waves'.
> This will get you a proper file directroy for your platform rather than
> the generic one. Much more useful.
> 
>>file1 := fileDir fullName, '\Genesis 37a.wav'.
> 
> Second problem; using strings like this. It will work but it's really
> bad practice. After all, there is a fair bit of code already written to
> make sure filenames are acceptable. Why work around it?
> Use
> fileDir fileNamed: 'Genesis.wav'
>  etc. instead.
> 
>>file2 := fileDir fullName, '\Genesis 37b.wav'.
>>file3 := fileDir fullName, '\Genesis 37k.wav'.
>>  wave1 := (StandardFileStream fileNamed: file1) binary.
>>  wave1 := ReadStream on: wave1 contentsOfEntireFile.
> 
> Third problem; why are you makinga ReadStream on the contents of the
> file? The stuff is already accesible from the file stream and if it is
> a big file you will rapidly run out of memory.
> 
>>  wave2 := (StandardFileStream fileNamed: file2) binary.
>>  wave2 := ReadStream on: wave2 contentsOfEntireFile.
>>  wave3 := (StandardFileStream fileNamed: file3) binary.
>>  wave3 nextPutAll: wave1.
> 
> Fourth problem; you're trying to add a ReadStream to another Stream. You
> could get away with 'wave3 nextPutAll: wave1 contents' in most cases.
> 
>>  wave3 nextPutAll: wave2.
>>  wave3 flush.
>>  wave3 close.
>>  wave2 close.
>>  wave1 close.
> 
> Take a look at FileDirectory copyFileNamed:to: to see a similar process.
> 
> You might find this more successful:-
> | fileDir wave3 wave2 wave1 buffer|
> fileDir := FileDirectory default directoryNamed:'C:\Documents and
>  Settings\Jimmie\My
> Documents\My Music\MP3\Alexander Scourby\waves'.
> wave3 := fileDir fileNamed: 'Genesis 37k.wav'.
> wave2 := fileDir oldFileNamed: 'Genesis 37b.wav'.
> wave1 := fileDir oldFileNamed: 'Genesis 37a.wav'.
> buffer := String new:5000.
> wave3 setToEnd.
> [wave2 atEnd] whileFalse: [wave3 nextPutAll: (wave2 nextInto: buffer)].
> [wave1 atEnd] whileFalse: [wave3 nextPutAll: (wave1 nextInto: buffer)].
> wave1 close.
> wave2 close.
> wave3 close.
> 
> WARNING I didn't test this. Left as an exercise for the student.
> 
> tim




More information about the Squeak-dev mailing list