fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
3d38215
v2.1.1
1local ffi =require ( "ffi" ) 2 3local ffistring =ffi . string 4local cdef =ffi . cdef 5 6cdef [[ 7typedef uint8_t SAMPLE_8BIT ; 8typedef int16_t SAMPLE_16BIT ; 9 10typedef struct { 11unsigned char riff [ 4 ]; // RIFF string 12unsigned int data_size ; // overall size of file in bytes - 8 13unsigned char wave [ 4 ]; // WAVE string 14} WAV_HEADER ; 15 16typedef struct { 17unsigned char chunk_id [ 4 ]; // RIFF string 18unsigned int chunk_size ; // overall size of chunk in bytes 19} WAV_CHUNK ; 20 21typedef struct { 22unsigned int id ; 23unsigned int position ; 24unsigned char data [ 4 ]; 25unsigned int chunk_start ; 26unsigned int block_start ; 27unsigned int sample_offset ; 28} CUE_POINT ; 29 30typedef struct { 31unsigned int num_points ; 32CUE_POINT points []; 33} CUE_CHUNK ; 34 35typedef struct { 36unsigned int id ; 37unsigned int type ; 38unsigned int sample_start ; 39unsigned int sample_end ; 40unsigned int fraction ; 41unsigned int play_count ; 42} SAMPLE_LOOP ; 43 44typedef struct { 45unsigned int manufacturer ; 46unsigned int product ; 47unsigned int sample_period ; 48unsigned int midi_unity_note ; 49unsigned int midi_pitch_fraction ; 50unsigned int smpte_format ; 51unsigned int smpte_offset ; 52unsigned int sample_loops ; 53unsigned int sample_data ; 54SAMPLE_LOOP loops []; 55} SAMPLE_CHUNK ; 56]] 57 58local wav ={} 59 60local function newChunkCue ( f , chunk ) 61local data =f : read ( chunk . chunk_size ) 62local points ={} 63local cue =ffi . cast ( "CUE_CHUNK*" , data ) 64local np =cue . num_points - 1 65for i =0 , np do 66local point =cue . points [ i ] 67table . insert ( points , { 68id =tonumber ( point . id ), 69position =tonumber ( point . position ), 70chunk_start =tonumber ( point . chunk_start ), 71block_start =tonumber ( point . block_start ), 72sample_offset =tonumber ( point . sample_offset ) 73} ) 74end 75return points 76end 77 78local function newChunkSmpl ( f , chunk ) 79local data =f : read ( chunk . chunk_size ) 80local loops ={} 81local smpl =ffi . cast ( "SAMPLE_CHUNK*" , data ) 82local ns =smpl . sample_loops - 1 83for i =0 , ns do 84local loop =smpl . loops [ i ] 85table . insert ( loops , { 86id =tonumber ( loop . id ), 87type =tonumber ( loop . type ), 88sample_start =tonumber ( loop . sample_start ), 89sample_end =tonumber ( loop . sample_end ), 90fraction =tonumber ( loop . fraction ), 91play_count =tonumber ( loop . play_count ) 92} ) 93end 94return loops 95end 96 97function wav . parse ( file ) 98local f =assert ( love . filesystem . newFile ( file , "r" )) 99 100local parsed ={ 101points ={} , 102loops ={} 103} 104 105local header =ffi . cast ( "WAV_HEADER*" , f : read ( ffi . sizeof ( "WAV_HEADER" ))) 106 107while true do 108local chunk =ffi . cast ( "WAV_CHUNK*" , f : read ( ffi . sizeof ( "WAV_CHUNK" ))) 109 110local name =ffistring ( chunk . chunk_id , 4 ): lower () 111local size =chunk . chunk_size 112 113if size <= 0 then break end -- EOF 114 115if name == "cue " then 116parsed . points =newChunkCue ( f , chunk ) 117elseif name == "smpl" then 118parsed . loops =newChunkSmpl ( f , chunk ) 119else 120-- Seek without reading data.. 121local pos =f : tell () + chunk . chunk_size 122if pos >= f : getSize () then break end 123f : seek ( pos ) 124end 125end 126 127f : close () 128return parsed 129end 130 131return wav