hello everyone and happy new year to one and all!!. hope everyone had a very wondeful holiday filled with joy,happiness and wishing more positive happenings in this year.
i worked on a small bit of code late last year which involved working with some audio data where i had to do some bit syntax and bit manipulation to get the various metadata and parts of an audio file either over the network being streamed or a file on disk
Erlang and erlang related languages(Elixir,Lfe,Luerl) have this short and nifty way of unpacking this data out and getting the various sections efficiently and also with a small bit of code it takes a little bit of getting used to but its very powerful none the less
get_audio_parts_wav(File_path)->
{ok, Audio_binary} = file:read_file(File_path),
<<Chunk_id:4/binary,ChunkSize:32/integer-little,Format:4/binary,
Subchunkid:4/binary,Subcsize:32/integer-little,Audio_format:16/integer-little,
Number_channels:16/integer-little,SamplesPerSecond:32/integer-little,
ByteRate:32/integer-little,BlockAlign:16/integer-little,
BitsPerSample:16/integer-little,
SubChunkTwo_id:4/binary,ChunkTwoSize:32/integer-little,
PcmData:ChunkTwoSize/binary,_/binary>> = Audio_binary,
TotalSeconds = floor(ChunkSize/ByteRate),
Minutes = floor(TotalSeconds/60),
Seconds = TotalSeconds rem 60,
[{chunk_id,Chunk_id},{chunk_size,ChunkSize},{format,Format},
{subchunkid,Subchunkid},{subchunksize,Subcsize},{audio_format,Audio_format},
{number_channels,Number_channels},{samplespersecond,SamplesPerSecond},
{byterate,ByteRate},{blockalign,BlockAlign},{bitspersample,BitsPerSample},
{subchunktwoid,SubChunkTwo_id},{chunktwosize,ChunkTwoSize},{pcm_data,PcmData},
{minutes,Minutes},{seconds,Seconds},{total_seconds,TotalSeconds}
].
this piece of code above will get all the metadata/pcm data about an mp3 file. the various sections can be integers,floats,text,binary which can be spliced out.
the endianess can be also put into the equation as seen above. I broke the code down for clarity sake but the part of the code for the dissassembly can be done in just 2 or 3 lines of code.pretty cool !!!. try putting this code in a file,compile and try it out.