Skip to content

Instantly share code, notes, and snippets.

@voluntas
Last active May 26, 2017 05:57
Show Gist options
  • Save voluntas/b30b3e5c1e23ea2b1a689dd19498482c to your computer and use it in GitHub Desktop.
Save voluntas/b30b3e5c1e23ea2b1a689dd19498482c to your computer and use it in GitHub Desktop.
wav ファイルのパース、音源は G.711 の PCMU
-module(wav).
-export([parse/0, parse/1]).
-include_lib("eunit/include/eunit.hrl").
%% http://www.graffiti.jp/pc/p030506a.htm
%% http://soundfile.sapp.org/doc/WaveFormat/
%% http://bb.watch.impress.co.jp/cda/parts/image_for_link/58934-16386-3-1.html
%% Erlang/OTP 19 [erts-8.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] [sharing-preserving]
%%
%% Eshell V8.0.2 (abort with ^G)
%% 1> c(wav).
%% {ok,wav}
%% 2> wav:parse().
%% wav.erl:44:<0.57.0>: ChunkSize = 203969
%% wav.erl:45:<0.57.0>: Format = <<"WAVE">>
%% wav.erl:46:<0.57.0>: Subchunk1Id = <<"fmt ">>
%% wav.erl:47:<0.57.0>: Subchunk1IdSize = 18
%% wav.erl:48:<0.57.0>: AudioFormat = 7
%% wav.erl:49:<0.57.0>: NumChannels = 1
%% wav.erl:50:<0.57.0>: SampleRate = 8000
%% wav.erl:51:<0.57.0>: ByteRate = 8000
%% wav.erl:52:<0.57.0>: BlockAlign = 1
%% wav.erl:53:<0.57.0>: BitsPerSample = 8
%% wav.erl:54:<0.57.0>: ExtraParamSize = 0
%% wav.erl:55:<0.57.0>: Subchunk2Id = <<"data">>
%% wav.erl:56:<0.57.0>: Subchunk2IdSize = 203931
%% wav.erl:57:<0.57.0>: byte_size ( _Rest ) = 203931
%% ok
parse() ->
%% load sample file
parse("g711-ulaw-25s.wav").
parse(Filename) ->
{ok, Binary} = file:read_file(Filename),
parse0(Binary).
parse0(<<"RIFF", ChunkSize:32/little, Format:32/bits,
Subchunk1Id:32/bits, Subchunk1IdSize:32/little,
AudioFormat:16/little, NumChannels:16/little,
SampleRate:32/little, ByteRate:32/little,
BlockAlign:16/little, BitsPerSample:16/little,
ExtraParamSize:16/little,
Subchunk2Id:32/bits, Subchunk2IdSize:32/little,
_Rest/binary>>) ->
?debugVal(ChunkSize),
?debugVal(Format),
?debugVal(Subchunk1Id),
?debugVal(Subchunk1IdSize),
?debugVal(AudioFormat),
?debugVal(NumChannels),
?debugVal(SampleRate),
?debugVal(ByteRate),
?debugVal(BlockAlign),
?debugVal(BitsPerSample),
?debugVal(ExtraParamSize),
?debugVal(Subchunk2Id),
?debugVal(Subchunk2IdSize),
?debugVal(byte_size(_Rest)),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment