Created
August 2, 2015 09:39
-
-
Save naoya/fcca01ab75f88c08f7f2 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
defmodule FormChunk do | |
defstruct [:id, :size, :type, :common, :sound_data] | |
def new( | |
<< | |
id :: bitstring-size(32), | |
size :: unsigned-integer-size(32), | |
type :: bitstring-size(32), | |
chunks :: binary, | |
>>) do | |
## Common Chunk のメタデータを抽出 | |
<< | |
common_id :: unsigned-integer-size(32), | |
common_chunk_size :: unsigned-integer-size(32), | |
remain :: binary | |
>> = chunks | |
## 抽出された値で Common Chunk と Sound Data Chunk を分ける | |
<< | |
common_data :: binary-size(common_chunk_size), | |
sound_data_chunk :: binary | |
>> = remain | |
%FormChunk{ | |
id: id, | |
size: size, | |
type: type, | |
common: CommonChunk.new(common_id, common_chunk_size, common_data), | |
sound_data: SoundDataChunk.new(sound_data_chunk) | |
} | |
end | |
end | |
defmodule CommonChunk do | |
defstruct [ | |
:id, | |
:size, | |
:num_channels, | |
:num_sample_frames, | |
:sample_size, | |
:sample_rate, | |
] | |
def new( | |
id, | |
size, | |
<< | |
num_channels :: unsigned-integer-size(16), | |
num_sample_frames :: unsigned-integer-size(32), | |
sample_size :: unsigned-integer-size(16), | |
# FIXME: Sound Rate よく分からん | |
_x :: unsigned-integer-size(16), # ? | |
sample_rate :: unsigned-integer-size(16), # hmm... | |
_y :: binary | |
>>) do | |
%CommonChunk{ | |
id: id, | |
size: size, | |
num_channels: num_channels, | |
num_sample_frames: num_sample_frames, | |
sample_size: sample_size, | |
sample_rate: sample_rate, | |
} | |
end | |
end | |
defmodule SoundDataChunk do | |
defstruct [ | |
:id, | |
:size, | |
:offset, | |
:block_size, | |
:sound_data, | |
] | |
def new( | |
<< | |
id :: bitstring-size(32), | |
size :: unsigned-integer-size(32), | |
offset :: unsigned-integer-size(32), | |
block_size :: unsigned-integer-size(32), | |
sound_data :: binary | |
>>) do | |
%SoundDataChunk{ | |
id: id, | |
size: size, | |
offset: offset, | |
block_size: block_size, | |
sound_data: sound_data, | |
} | |
end | |
end | |
[path] = System.argv | |
{:ok, file} = File.open path, [:read] | |
IO.inspect FormChunk.new IO.binread(file, :all) | |
File.close file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment