1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixir その177

Last updated at Posted at 2023-01-17

概要

paiza.ioでelixirやってみた。
aiffファイルを解析してみた。

参考にしたページ

サンプルコード


:inets.start
:ssl.start

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),
			_x :: unsigned-integer-size(16),
			sample_rate :: unsigned-integer-size(16),
			_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
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),
			common_id :: unsigned-integer-size(32), 
			common_chunk_size :: unsigned-integer-size(32),
			common_data :: binary-size(common_chunk_size),
			sound_data_chunk :: binary
		>>) do
		%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 Main do
	def main() do
		{:ok, {_status, _headers, body}} = :httpc.request('https://resolvenet.org/modules/contrib/webform/tests/files/sample.aiff')
		body = IO.iodata_to_binary(body)
	    IO.inspect FormChunk.new body
	end
end
Main.main


実行結果

%FormChunk{
  common: %CommonChunk{
    id: 1129270605,
    num_channels: 1,
    num_sample_frames: 37478,
    sample_rate: 44100,
    sample_size: 16,
    size: 18
  },
  id: "FORM",
  size: 75002,
  sound_data: %SoundDataChunk{
    block_size: 0,
    id: "SSND",
    offset: 0,
    size: 74964,
    sound_data: <<255, 0, 0, 0, 0, 0, 255, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255,
      0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0,
      0, ...>>
  },
  type: "AIFF"
}

成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?