forked from go-audio/wav
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start work on the smpl metadata codec
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package wav | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/mattetti/audio/riff" | ||
) | ||
|
||
// smpl chunk is documented here: | ||
// https://sites.google.com/site/musicgapi/technical-documents/wav-file-format#smpl | ||
|
||
// DecodeSamplerChunk decodes a smpl chunk and put the data in Decoder.Metadata.SamplerInfo | ||
func DecodeSamplerChunk(d *Decoder, ch *riff.Chunk) error { | ||
if ch == nil { | ||
return fmt.Errorf("can't decode a nil chunk") | ||
} | ||
if d == nil { | ||
return fmt.Errorf("nil decoder") | ||
} | ||
if ch.ID == CIDSmpl { | ||
// read the entire chunk in memory | ||
buf := make([]byte, ch.Size) | ||
var err error | ||
if _, err = ch.Read(buf); err != nil { | ||
return fmt.Errorf("failed to read the smpl chunk - %v", err) | ||
} | ||
if d.Metadata == nil { | ||
d.Metadata = &Metadata{} | ||
} | ||
|
||
d.Metadata.SamplerInfo = &SamplerInfo{} | ||
|
||
r := bytes.NewReader(buf) | ||
|
||
scratch := make([]byte, 4) | ||
if _, err = r.Read(scratch); err != nil { | ||
return fmt.Errorf("failed to read the smpl Manufacturer") | ||
} | ||
copy(d.Metadata.SamplerInfo.Manufacturer[:], scratch[:4]) | ||
if _, err = r.Read(scratch); err != nil { | ||
return fmt.Errorf("failed to read the smpl Product") | ||
} | ||
copy(d.Metadata.SamplerInfo.Product[:], scratch[:4]) | ||
|
||
if err := binary.Read(r, binary.BigEndian, &d.Metadata.SamplerInfo.SamplePeriod); err != nil { | ||
return err | ||
} | ||
if err := binary.Read(r, binary.BigEndian, &d.Metadata.SamplerInfo.MIDIUnityNote); err != nil { | ||
return err | ||
} | ||
if err := binary.Read(r, binary.BigEndian, &d.Metadata.SamplerInfo.MIDIPitchFraction); err != nil { | ||
return err | ||
} | ||
if err := binary.Read(r, binary.BigEndian, &d.Metadata.SamplerInfo.SMPTEFormat); err != nil { | ||
return err | ||
} | ||
if err := binary.Read(r, binary.BigEndian, &d.Metadata.SamplerInfo.SMPTEOffset); err != nil { | ||
return err | ||
} | ||
if err := binary.Read(r, binary.BigEndian, &d.Metadata.SamplerInfo.NumSampleLoops); err != nil { | ||
return err | ||
} | ||
//TODO: loops | ||
} | ||
ch.Drain() | ||
return nil | ||
} |