2023-05-16 21:28:16 +02:00
|
|
|
function file_read_ASCII(file, amo = 1) {
|
|
|
|
var b = "";
|
2024-04-23 04:59:59 +02:00
|
|
|
repeat(amo) b += chr(buffer_read(file, buffer_u8));
|
2023-05-16 21:28:16 +02:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function file_read_wav(path) {
|
2023-05-29 20:36:31 +02:00
|
|
|
wav_file_reader = buffer_load(path);
|
2023-05-22 20:31:55 +02:00
|
|
|
wav_file_reading = true;
|
|
|
|
wav_file_prg = 0;
|
|
|
|
|
2023-05-29 20:36:31 +02:00
|
|
|
if(wav_file_reader == -1) {
|
|
|
|
noti_warning("File read error.");
|
|
|
|
return noone;
|
|
|
|
}
|
|
|
|
|
2023-05-29 18:35:33 +02:00
|
|
|
wav_file_load_time = current_time;
|
2024-04-03 09:40:37 +02:00
|
|
|
var _buffer_size = buffer_get_size(wav_file_reader);
|
2023-05-29 18:35:33 +02:00
|
|
|
|
2023-05-16 21:28:16 +02:00
|
|
|
//RIFF
|
2024-04-03 09:40:37 +02:00
|
|
|
var debug_str = $">> READING WAV [{path}] <<\n";
|
|
|
|
debug_str += $"Buffer size: {_buffer_size}\n\n";
|
|
|
|
|
2023-08-11 21:08:02 +02:00
|
|
|
debug_str += "-- RIFF --\n";
|
|
|
|
var b = file_read_ASCII(wav_file_reader, 4); debug_str += $"{b}\n";
|
|
|
|
var l = buffer_read(wav_file_reader, buffer_u32); debug_str += $"Packages: {l}\n";
|
|
|
|
var w = file_read_ASCII(wav_file_reader, 4); debug_str += $"{w}\n";
|
2023-05-22 20:31:55 +02:00
|
|
|
|
2024-04-03 09:40:37 +02:00
|
|
|
if(b != "RIFF" || w != "WAVE") {
|
|
|
|
printIf(global.FLAG.wav_import, debug_str);
|
|
|
|
noti_warning("Not a valid .wav file.");
|
|
|
|
return noone;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(buffer_get_size(wav_file_reader) != l + 8)
|
|
|
|
noti_warning(".wav file has different size than the package header. This may cause reading error.");
|
|
|
|
|
2023-05-16 21:28:16 +02:00
|
|
|
//FORMAT
|
2023-08-11 21:08:02 +02:00
|
|
|
debug_str += "-- FORMAT --\n";
|
|
|
|
var b = file_read_ASCII(wav_file_reader, 4); debug_str += $"{b}\n";
|
|
|
|
var l = buffer_read(wav_file_reader, buffer_u32); debug_str += $"Length: {l}\n";
|
2024-04-23 04:59:59 +02:00
|
|
|
var f = buffer_read(wav_file_reader, buffer_u16); debug_str += $"Linear quantize: {f}\n";
|
2023-05-28 20:00:51 +02:00
|
|
|
|
2024-04-23 04:59:59 +02:00
|
|
|
if(l != 16 || f != 1) {
|
2024-04-03 09:40:37 +02:00
|
|
|
printIf(global.FLAG.wav_import, debug_str);
|
2024-04-23 04:59:59 +02:00
|
|
|
noti_warning("File format not supported, the audio file need to be 8, 16, 32 bit uncompressed PCM wav with no extension.");
|
2023-05-29 18:35:33 +02:00
|
|
|
return noone;
|
2023-05-28 20:00:51 +02:00
|
|
|
}
|
|
|
|
|
2023-08-11 21:08:02 +02:00
|
|
|
var ch = buffer_read(wav_file_reader, buffer_u16); debug_str += $"Channels: {ch}\n";
|
|
|
|
var sm = buffer_read(wav_file_reader, buffer_u32); debug_str += $"Sample: {sm}\n";
|
2024-04-23 04:59:59 +02:00
|
|
|
var l = buffer_read(wav_file_reader, buffer_u32); debug_str += $"BPS: {l}\n";
|
2024-04-03 09:40:37 +02:00
|
|
|
var br = buffer_read(wav_file_reader, buffer_u16); debug_str += $"Byterate: {br}\n";
|
2023-08-11 21:08:02 +02:00
|
|
|
var l = buffer_read(wav_file_reader, buffer_u16); debug_str += $"Bit/Sam: {l}\n";
|
2023-05-22 20:31:55 +02:00
|
|
|
|
2023-05-16 21:28:16 +02:00
|
|
|
//DATA
|
2023-08-11 21:08:02 +02:00
|
|
|
debug_str += "-- DATA --\n";
|
|
|
|
var b = file_read_ASCII(wav_file_reader, 4); debug_str += $"{b}\n";
|
|
|
|
var l = buffer_read(wav_file_reader, buffer_u32); debug_str += $"Length: {l}\n";
|
2023-05-22 20:31:55 +02:00
|
|
|
|
2023-05-16 21:28:16 +02:00
|
|
|
var bpc = br / ch;
|
|
|
|
var bits = l / br;
|
2023-10-03 04:29:58 +02:00
|
|
|
var data = array_create(ch);
|
|
|
|
var dataF = [ array_create(bits) ];
|
2023-05-16 21:28:16 +02:00
|
|
|
|
2023-08-11 21:08:02 +02:00
|
|
|
debug_str += "-- READ --\n";
|
|
|
|
debug_str += $"Channels: {ch}\n";
|
|
|
|
debug_str += $"BPC: {bpc * 8}\n";
|
|
|
|
debug_str += $"bits: {bits}\n";
|
|
|
|
debug_str += $"samples: {sm}\n";
|
2024-04-03 09:40:37 +02:00
|
|
|
debug_str += $"duration: {real(bits) / real(sm)}s\n";
|
2023-05-22 20:31:55 +02:00
|
|
|
|
2023-10-03 04:29:58 +02:00
|
|
|
for( var j = 0; j < ch; j++ )
|
|
|
|
data[j] = array_create(bits);
|
2023-05-16 21:28:16 +02:00
|
|
|
|
2023-05-22 20:31:55 +02:00
|
|
|
wav_file_range = [0, 0];
|
2023-10-03 04:29:58 +02:00
|
|
|
|
2024-04-03 09:40:37 +02:00
|
|
|
var _buffer_left = _buffer_size - buffer_tell(wav_file_reader);
|
|
|
|
if(_buffer_left < l) {
|
|
|
|
noti_warning($"The file is smaller than the definded length. ({_buffer_left} < {l})");
|
|
|
|
bits = floor(_buffer_left / br);
|
|
|
|
}
|
|
|
|
|
2023-10-03 04:29:58 +02:00
|
|
|
content = new audioObject(sm, ch);
|
|
|
|
content.sound = data;
|
|
|
|
content.soundF = dataF;
|
|
|
|
content.bit_depth = bpc * 8;
|
|
|
|
content.duration = real(bits) / real(sm);
|
|
|
|
content.packet = bits;
|
2023-05-22 20:31:55 +02:00
|
|
|
|
2023-08-11 21:08:02 +02:00
|
|
|
printIf(global.FLAG.wav_import, debug_str);
|
|
|
|
|
2023-05-22 20:31:55 +02:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function file_read_wav_step() {
|
|
|
|
if(!wav_file_reading) return false;
|
2024-04-03 09:40:37 +02:00
|
|
|
if(!content) return false;
|
2023-05-22 20:31:55 +02:00
|
|
|
|
|
|
|
var t = current_time;
|
2023-05-29 18:35:33 +02:00
|
|
|
var bf_type, lim;
|
|
|
|
if(content.bit_depth == 8) { bf_type = buffer_u8; lim = 255; }
|
|
|
|
else if(content.bit_depth == 16) { bf_type = buffer_s16; lim = 32_768; }
|
|
|
|
else if(content.bit_depth == 32) { bf_type = buffer_s32; lim = 2_147_483_648; }
|
2023-05-22 20:31:55 +02:00
|
|
|
|
|
|
|
for(; wav_file_prg < content.packet; wav_file_prg++ ) {
|
2024-04-03 09:40:37 +02:00
|
|
|
var ch = 0;
|
|
|
|
var cha = content.channels;
|
|
|
|
var j = 0;
|
|
|
|
|
|
|
|
repeat( cha ) {
|
2023-10-03 04:29:58 +02:00
|
|
|
var b = buffer_read(wav_file_reader, bf_type) / lim;
|
|
|
|
ch += b;
|
|
|
|
content.sound[j][wav_file_prg] = b;
|
2024-04-03 09:40:37 +02:00
|
|
|
j++;
|
2023-10-03 04:29:58 +02:00
|
|
|
}
|
2023-05-22 20:31:55 +02:00
|
|
|
|
2023-10-03 04:29:58 +02:00
|
|
|
content.soundF[0][wav_file_prg] = ch / content.channels;
|
2023-05-22 20:31:55 +02:00
|
|
|
|
|
|
|
if(current_time - t > 1000 / 30) return false;
|
|
|
|
}
|
|
|
|
|
2023-05-29 18:35:33 +02:00
|
|
|
//printIf(global.FLAG.wav_import, $"Wav range: {wav_file_range}");
|
|
|
|
printIf(global.FLAG.wav_import, $"Load file complete in: {(current_time - wav_file_load_time) / 1000} s.");
|
2023-05-22 20:31:55 +02:00
|
|
|
|
|
|
|
wav_file_reading = false;
|
2023-05-29 18:35:33 +02:00
|
|
|
buffer_delete(wav_file_reader);
|
2023-05-22 20:31:55 +02:00
|
|
|
return true;
|
2023-05-16 21:28:16 +02:00
|
|
|
}
|