The code works, but I am clearly doing something wrong because the loop through the "LIST" chunk messes up in between each cue, apparently reading each one twice. You can see this when debugging the unique IDs and txt labels.
Code: Select all
Structure RIFFStructure
Riff.a[4]
Length.l
Wave.a[4]
EndStructure
Structure chunkStructure
Signature.a[4]
Length.l
EndStructure
Structure CuePointStructure
unique_id.l
label.s
sample_offset.l
EndStructure
Procedure.b GetWavCuePoints(fn.s,List cue.CuePointStructure())
Define *RiffPtr.RIFFStructure
Define *chunkPtr.chunkStructure
f.i = ReadFile(#PB_Any, fn)
If Not f
ProcedureReturn #False
EndIf
loaf.i = Lof(f)
*Buffer = AllocateMemory(loaf)
If *Buffer
If ReadData(f, *Buffer, loaf) = loaf
*RiffPtr = *Buffer
If PeekS(@*RiffPtr\Riff, 4, #PB_Ascii) = "RIFF" And *RiffPtr\Length = loaf - 8 And PeekS(@*RiffPtr\Wave, 4, #PB_Ascii) = "WAVE"
Debug "Header Ok"
*chunkPtr = *Buffer + $0C
Repeat
ChunkName.s = PeekS(@*chunkPtr\Signature, 4, #PB_Ascii)
Debug ""
Debug "CHUNK: " + ChunkName
Select ChunkName
Case "cue "
*cuePtr = *chunkPtr
*cuePtr + 8
NumCuePoints.w = PeekW(*cuePtr)
*cuePtr + 4
Debug "cue points: "+Str(NumCuePoints)
For cp = 1 To NumCuePoints
AddElement(cue())
cue()\unique_id = PeekL(*cuePtr)
Debug "unique_id: "+Str(cue()\unique_id)
*cuePtr+4
play_order_position.i = PeekL(*cuePtr)
*cuePtr+4
data_chunk_id.s = PeekS(*cuePtr,4,#PB_Ascii)
Debug "data_chunk_id: "+data_chunk_id
*cuePtr+4
chunk_start.l = PeekL(*cuePtr)
Debug "chunk_start: "+Str(chunk_start)
*cuePtr+4
block_start.i = PeekL(*cuePtr)
*cuePtr+4
cue()\sample_offset = PeekL(*cuePtr)
Debug "sample_offset: "+Str(cue()\sample_offset)
*cuePtr+4
Next cp
Case "LIST"
chklngth.i = *chunkPtr\Length
Debug "list chunk size: "+Str(chklngth)
list_type_id.s = PeekS(*chunkPtr+12,4,#PB_Ascii)
;Debug "list type ID: "+list_type_id
If list_type_id = "labl"
*lablPtr = *chunkPtr+12
lngth.l = PeekL(*lablPtr+4)
Debug "LNGTH: "+Str(lngth)
Debug "--------"
accum_lngth.l = 8
chklngth-4
While accum_lngth<chklngth
uid.l = PeekL(*lablPtr+accum_lngth)
Debug "UID: "+uid
accum_lngth+4
txt.s = ""
Repeat
this_char.s = PeekS(*lablPtr+accum_lngth,1,#PB_Ascii)
accum_lngth+1
If Asc(this_char)=0
Break
Else
txt+this_char
EndIf
ForEver
ForEach cue()
If cue()\unique_id=uid
cue()\label = txt
Break
EndIf
Next
Debug "TXT: "+txt
accum_lngth+2
If Len(txt) % 2
Debug Str(Len(txt))+" is odd"
accum_lngth-1
EndIf
Debug "--------"
Wend
EndIf
EndSelect
*chunkPtr + 8 + *chunkPtr\Length
Until *chunkPtr >= *Buffer + *RiffPtr\Length
EndIf
EndIf
FreeMemory(*Buffer)
EndIf
CloseFile(f)
ProcedureReturn #True
EndProcedure
Filename$ = "D:\regions_mono.wav"
NewList cue.CuePointStructure()
GetWavCuePoints(Filename$,cue())
Debug "" : Debug "" : Debug "----------------"
c.i = 0
ForEach cue()
c+1
Debug "#"+Str(c)+": "+cue()\label+" @ "+Str(cue()\sample_offset)
Next
I think the mistake is to do with the building up of the "accum_lngth" variable as it works its way through the chunk.
I am sure the mistake is some elementary thing that will turn out to be quite embarrassing, but if anyone could help out, I'd appreciate it.