RIFFReader.st
author Claus Gittinger <cg@exept.de>
Thu, 23 Nov 2000 18:08:13 +0100
changeset 1434 9675c5ea14df
parent 541 9b654ff94202
child 1437 b4cc563c0895
permissions -rw-r--r--
refactored RIFF- and AVIReader for general use RIFFReader is no longer tied to the ImageReader hierarchy.

"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

"{ Package: 'stx:libview2' }"

Object subclass:#RIFFReader
	instanceVariableNames:'inStream chunkSize streamTypes client'
	classVariableNames:'UnsupportedFormatErrorSignal'
	poolDictionaries:''
	category:'System-Support-FileFormats'
!

!RIFFReader class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Abstract helper class to read RIFF files. See concrete subclasses for details.
    RIFF is the abstract base-format used for a whole bunch of concrete file formats:
        WAV, AVI, AIF etc.
    Concrete formats must be parsed by concrete readers.

    Use:
        define your own concrete reader, with methods getChunk_FOO:,
        for each chunk type FOO you want to parse there.
        Define your reader as a client to this reader, and let it processChunks.
        See AVIReader for a concrete example.

    This is in an experimental state and not yet finished.
    The protocol may change.
"
! !

!RIFFReader class methodsFor:'class initialization'!

initialize
    UnsupportedFormatErrorSignal isNil ifTrue:[
        UnsupportedFormatErrorSignal := ErrorSignal newSignalMayProceed:true.
        UnsupportedFormatErrorSignal nameClass:self message:#unsupportedFormatErrorSignal.
    ].

    "
     RIFFReader initialize
    "

    "Created: 4.4.1997 / 22:35:52 / cg"
    "Modified: 5.4.1997 / 16:12:16 / cg"
! !

!RIFFReader class methodsFor:'testing'!

isRIFFFile:aFileName
    "return true, if aFileName contains RIFF-encoded data"

    |data1 inStream|

    inStream := self streamReadingFile:aFileName.
    inStream isNil ifTrue:[^ false].
    inStream binary.

    data1 := String new:4.
    inStream nextBytes:4 into:data1.
    inStream close.

    ^ (data1 = 'RIFF')

    "
     RIFFReader isRIFFFile:'bitmaps/magtape.xpm'    
     RIFFReader isRIFFFile:'/home2/pd_stuff/movies/avi/drlair.avi'      
     RIFFReader isRIFFFile: '/usr/share/sounds/alsa/test.wav'      
    "

    "Created: 4.4.1997 / 22:35:52 / cg"
    "Modified: 5.4.1997 / 16:12:16 / cg"
! !

!RIFFReader methodsFor:'accessing'!

addStream:type
    streamTypes add:type
!

client
    "return the value of the instance variable 'client' (automatically generated)"

    ^ client
!

client:something
    "set the value of the instance variable 'client' (automatically generated)"

    client := something.
!

streamTypes:something
    "set the value of the instance variable 'streamTypes' (automatically generated)"

    streamTypes := something.
! !

!RIFFReader methodsFor:'reading from stream'!

fromStream:aStream
    "read RIFF chunks from aStream. Process chunks in getChunkXXX methods
     (usually redefined in concrete reader classes."

    client := self.
    self processStream:aStream

    "
     RIFFReader fromFile:'/phys/exept/home/pd_stuff/movies/avi/hangldm.avi'      
     RIFFReader fromFile:'/phys/exept/opt/office52/share/gallery/sounds/gong.wav'      

     AVIReader fromFile:'/phys/exept/home/pd_stuff/movies/avi/hangldm.avi'      
     WAVFileReader fromFile:'/phys/exept/opt/office52/share/gallery/sounds/gong.wav'      
     AIFFFileReader fromFile:'/phys/exept/opt/office52/share/gallery/sounds/gong.wav'      
    "

    "Modified: 17.4.1997 / 03:25:08 / cg"
!

getChunk
    "get a single chunk"

    |id sel sTyp|

    'getChunk -> ' infoPrint.

    id := '    '.
    inStream nextBytes:4 into:id startingAt:1.
    chunkSize := inStream nextLongMSB:false.

    (id at:1) == $0 ifTrue:[
        sTyp := '_Unknown'.
        streamTypes notNil ifTrue:[
            sTyp := streamTypes at:((id at:2) digitValue + 1) ifAbsent:nil.
        ].
        sel := 'getChunk_' , sTyp , '_' , (id copyFrom:3), ':'.

"/id infoPrint. ' -> ' infoPrint. sel infoPrintCR.
        sel := sel asSymbolIfInterned.
        (sel isNil or:[(self respondsTo:sel) not]) ifTrue:[
            sel := 'getChunk_' , sTyp , '_Unknown:'.
"/'  ' infoPrint. id infoPrint. ' -> ' infoPrint. sel infoPrintCR.
            sel := sel asSymbolIfInterned.
            (sel isNil or:[(self respondsTo:sel) not]) ifTrue:[
                '[' infoPrint. ('getChunk_' , sTyp , '_' , (id copyFrom:3)) infoPrint. '] ' infoPrint.
                sel := #'getChunk_Unknown:'.    
"/'    ' infoPrint. id infoPrint. ' -> ' infoPrint. sel infoPrintCR.
            ].
        ]
    ] ifFalse:[
        (id at:4) == Character space ifTrue:[
            id := id copyTo:3
        ].
        sel := ('getChunk_' , id , ':') asSymbolIfInterned.
        (sel isNil or:[(self respondsTo:sel) not]) ifTrue:[
            '[' infoPrint. ('getChunk_' , id) infoPrint. '] ' infoPrint.
            sel := #'getChunk_Unknown:'    
        ].
"/id infoPrint. ' -> ' infoPrint. sel infoPrintCR.
    ].

    client perform:sel with:chunkSize ifNotUnderstood:[self perform:sel with:chunkSize].
    '' infoPrintCR.

    "
     RIFFReader fromFile:'/phys/exept/home/pd_stuff/movies/avi/hangldm.avi'      
     AVIReader fromFile:'/phys/exept/home/pd_stuff/movies/avi/hangldm.avi'      
    "

    "Created: 4.4.1997 / 22:50:13 / cg"
    "Modified: 5.4.1997 / 15:47:48 / cg"
!

getChunk_DISP:chunkSize
    "process (ignore) a DISP chunk"

    'getChunk_DISP -> ' infoPrint.

    self skipChunk:chunkSize

    "Created: 5.4.1997 / 15:19:10 / cg"
!

getChunk_JUNK:chunkSize
    "ignore JUNK chunk"

    'getChunk_JUNK -> ' infoPrint.

    self skipChunk:chunkSize
!

getChunk_LIST:chunkSize
    "process a LIST chunk"

    'getChunk_LIST' infoPrint.

    inStream skip:4.

    "
     AVIReader fromFile:'/home2/pd_stuff/movies/avi/hangldm.avi'      
     RIFFReader fromFile:'/home2/pd_stuff/movies/avi/hangldm.avi'      
     RIFFReader fromFile:'/home2/cg/AFsp-V2R0/test/audiofiles/jg00b1ss.wav'      
    "

    "Created: 4.4.1997 / 23:18:33 / cg"
    "Modified: 17.4.1997 / 03:22:15 / cg"
!

getChunk_RIFF:chunkSize
    "process a RIFF chunk"

    'getChunk_RIFF' infoPrint.

    inStream skip:4.
!

getChunk_Unknown:chunkSize
    "ignore any other chunk"

    'getChunk_Unknown -> ' infoPrint.

    self skipChunk:chunkSize
!

processStream:aStream
    "read RIFF chunks from aStream. Process chunks in getChunkXXX methods
     (usually redefined in concrete reader classes."

    inStream := aStream.
    inStream binary.

    [inStream atEnd] whileFalse:[
        client perform:#getChunk ifNotUnderstood:[self getChunk]. 
    ].
    ^ nil

    "
     RIFFReader fromFile:'/home2/pd_stuff/movies/avi/hangldm.avi'      
     RIFFReader fromFile:'/home2/cg/AFsp-V2R0/test/audiofiles/jg00b1ss.wav'      
    "

    "Modified: 17.4.1997 / 03:25:08 / cg"
!

skipChunk:chunkSize
    "skip a chunk"

    |sz|

    'skipChunk' infoPrint.

    sz := chunkSize.
    (sz bitTest:1) ifTrue:[
        sz := sz + 1
    ].

    inStream skip:sz.
! !

!RIFFReader class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/RIFFReader.st,v 1.4 2000-11-23 17:08:13 cg Exp $'
! !
RIFFReader initialize!