Skip to content

Commit

Permalink
start work on the smpl metadata codec
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Jan 24, 2018
1 parent 5a351b0 commit 7cafa6e
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions smpl_chunk.go
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
}

0 comments on commit 7cafa6e

Please sign in to comment.