Created
October 12, 2019 17:15
-
-
Save evanmcc/c1f7626fc13510b812939f8f34be4b26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(bb_wav). | |
-export([ | |
load_sounds/0, load_sounds/1 | |
]). | |
-define(default_path, "sound_data/"). | |
load_sounds() -> | |
load_sounds(?default_path). | |
load_sounds(Path) -> | |
_ = filelib:fold_files( | |
Path, ".*wav", true, | |
fun(F, _) -> | |
{ok, Bin} = file:read_file(F), | |
{Channels, Data, Duration} = load_wav(Bin), | |
Base = filename:rootname(filename:basename(F)), | |
ok = bbsynth:load_wav(Base, Channels, Duration, Data), | |
ignored | |
end, | |
ignored). | |
load_wav(BinFile) -> | |
TotalSize = size(BinFile), | |
<<"RIFF", RIFFSize:32/little-unsigned-integer, Rest/binary>> = | |
BinFile, | |
TotalSize = RIFFSize + 8, | |
<<"WAVE", "fmt ", Sub1Size:32/little-unsigned-integer, Rest1/binary>> = | |
Rest, | |
Sub1Size = 16, % we assume PCM for now | |
<<Format:16/little-unsigned-integer, | |
Channels:16/little-unsigned-integer, | |
SampleRate:32/little-unsigned-integer, | |
_ByteRate:32/little-unsigned-integer, | |
_BlockAlign:16/little-unsigned-integer, | |
SampleBits:16/little-unsigned-integer, | |
Rest2/binary>> = Rest1, | |
%% some assertions | |
Format = 1, | |
SampleRate = 44100, | |
SampleBits = 16, | |
true = Channels == 1 orelse Channels == 2, | |
%% now extract the actual data | |
<<"data", DataSize:32/little-unsigned-integer, | |
Data:DataSize/bytes>> = Rest2, | |
%% this would be more complicated if we allowed different sample | |
%% rates or bit counts. | |
Duration = DataSize div 2, | |
{Channels, Data, Duration}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment