Skip to content

Commit

Permalink
More description in the code . Removed debug prints
Browse files Browse the repository at this point in the history
  • Loading branch information
csiwek committed Jul 30, 2019
1 parent fe750ea commit 640c1b7
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions opus.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,16 @@ func (i *OpusReader) getPage() ([]byte, error) {
if err := binary.Read(i.stream, binary.LittleEndian, &headerType); err != err {
return payload, err
}
fmt.Printf("headerType: %v\n", headerType)
var granulePosition uint64
if err := binary.Read(i.stream, binary.LittleEndian, &granulePosition); err != err {
return payload, err
}
fmt.Printf("i.granulePosition: %v\n", granulePosition)
if err := binary.Read(i.stream, binary.LittleEndian, &i.serial); err != err {
return payload, err
}
fmt.Printf("i.serial: %v\n", i.serial)
if err := binary.Read(i.stream, binary.LittleEndian, &i.pageIndex); err != err {
return payload, err
}
fmt.Printf("i.pageIndexl: %v\n", i.pageIndex)
//skipping checksum
io.CopyN(ioutil.Discard, i.stream, 4)

Expand All @@ -186,42 +182,34 @@ func (i *OpusReader) getPage() ([]byte, error) {
i.previousGranulePosition = granulePosition

var payloadLen uint32
// Iterating through all segments calculate the complete packet size
for x := 1; x <= int(segments); x++ {
var segSize uint8
if err := binary.Read(i.stream, binary.LittleEndian, &segSize); err != err {
return payload, err
}
fmt.Printf("Seg %d size: %v\n", x, segSize)
payloadLen = payloadLen + uint32(segSize)
}

fmt.Printf("payloadLen 0: %v\n", payloadLen)
fmt.Printf("segments: %v\n", segments)

if i.pageIndex == 0 {

_, err := i.readOpusHead()
if err != nil {
fmt.Printf("Read Headers Error : %v\n", err)
return payload, err
}
} else if i.pageIndex == 1 {
plen, err := i.readOpusTags()
if err != nil {
fmt.Printf("ReadTags Error : %v\n", err)
return payload, err
}
// we are not interested in tags (metadata?)
io.CopyN(ioutil.Discard, i.stream, int64(payloadLen-plen))

} else {
tmpPacket := make([]byte, payloadLen)
binary.Read(i.stream, binary.LittleEndian, &tmpPacket)
fmt.Printf("an audio frame\n")
//Reading the TOC byte - we need to know the frame duration.
fmt.Printf("============= TOC : % 08b \n", tmpPacket[0])
return tmpPacket, nil
}


return payload, nil
}

Expand Down Expand Up @@ -265,6 +253,7 @@ func (i *OpusReader) getPageSingle() ([]byte, error) {
i.previousGranulePosition = granulePosition

var x uint8
// building a map of all segments
for x = 1; x <= i.segments; x++ {
var segSize uint8
if err := binary.Read(i.stream, binary.LittleEndian, &segSize); err != err {
Expand All @@ -276,36 +265,28 @@ func (i *OpusReader) getPageSingle() ([]byte, error) {
i.currentSegment = 1
}
var currentPacketSize uint32
// Iteraring throug all segments to check if there are lacing packets. If a segment is 255 bytes long, it means that there must be a following segment for the same packet (which may be again 255 bytes long)
for i.segmentMap[i.currentSegment] == 255 {
fmt.Printf("This packet has more segments: %v %v\n", i.currentSegment, i.segmentMap[i.currentSegment])
currentPacketSize += 255
i.currentSegment += 1

}

// Adding either the last segments of lacing ones or a packet that fits only in one segment
currentPacketSize += uint32(i.segmentMap[i.currentSegment])
fmt.Printf("Adding packet from segment %v size: %v \n", i.currentSegment, i.segmentMap[i.currentSegment])
if i.currentSegment < i.segments {
i.currentSegment += 1
} else {
i.currentSegment = 0
}

fmt.Printf("Current packet size: %v \n", currentPacketSize)

tmpPacket := make([]byte, currentPacketSize)

binary.Read(i.stream, binary.LittleEndian, &tmpPacket)
//Reading the TOC byte - we need to know the frame duration.
if len(tmpPacket) > 0 {
//shift 3 bits right to get a value of 5 leading bits. See https://tools.ietf.org/html/rfc6716
toc := tmpPacket[0] >> 3
// fmt.Printf("============= TOC : % 08b \n", tmpPacket[0])
// fmt.Printf("============= TOC Dec value : %d \n", toc)
// fmt.Printf("============= frameSize : %f \n", getFrameSize(uint8(toc)))
i.currentSampleLen = getFrameSize(uint8(toc))
}
fmt.Printf("Current segment %v \n", i.currentSegment)
return tmpPacket, nil
}

Expand Down Expand Up @@ -351,7 +332,6 @@ func (i *OpusReader) GetSingleSample() ([]byte, error) {

func (i *OpusReader) calculateSampleDuration(deltaGranulePosition uint32) (uint32, error) {
i.currentSamples = uint32(deltaGranulePosition)
fmt.Printf("calculating sample duration for packets: %v\n", i.currentSamples)
if i.sampleRate == 0 {
return 0, errors.New("Wrong samplerate")
}
Expand All @@ -364,7 +344,6 @@ func (i *OpusReader) calculateSampleDuration(deltaGranulePosition uint32) (uint3
}

func (i *OpusReader) GetCurrentSamples() uint32 {
fmt.Printf("current samples: %v\n", i.currentSamples)
return i.currentSamples
}

Expand Down

0 comments on commit 640c1b7

Please sign in to comment.