fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosMore attempts at fixing random garbage collection crashes..3d38215

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