RudolfVonKrugstein

RudolfVonKrugstein

How to convert binary data with primitive data types

Dear Community,

I have a binary of float32 representing audio data with floats in the range of [-1,1].
I want this audio data converted into a binary of signed 16 bit integers.
What is an efficient way of doing this in elexir? I tried:

  def to_linear16(<<>>) do
    <<>>
  end

  def to_linear16(<<v::float-size(32), r::binary>>) do
    Logger.debug("#{inspect(v)}")
    f =
      if v < 0 do
        <<trunc(v * 0x8000)::size(16)>>
      else
        <<trunc(v * 0x7FFF)::size(16)>>
      end

    f <> to_linear16(r)
  end

But there is something wrong:

  • I get an no function clause matching in to_linear16/1 also I am only giving it data, that is definitly multples of 4 bytes long.
  • I check the intermediate results via logging of the float32 extracted form the binary and they are not what I expect. In fact, if I put the same binary into Nx.from_tensor(b,:f32) I get different results.

So I am very puzzled here.

  • What is an efficient way of doing this in elixir or am I already on the right track here?
  • What am I doing wrong in the pattern matching? I am always taking a 32bit float from the beginning of the binary.So how could the matching fail?

Thank you!

Marked As Solved

dimitarvp

dimitarvp

Have you checked for endianness? Erlang assumes big-endian-arranged bytes when extracting floats the way you are doing it.

Demo:

bin = <<:math.pi()::float-32>> <> <<0, 0, 0>>
<<64, 73, 15, 219, 0, 0, 0>>

<<x::float-32, rest::binary>> = bin
<<64, 73, 15, 219, 0, 0, 0>>

x
3.1415927410125732

<<x::big-float-32, rest::binary>> = bin
<<64, 73, 15, 219, 0, 0, 0>>

x
3.1415927410125732

<<x::little-float-32, rest::binary>> = bin
<<64, 73, 15, 219, 0, 0, 0>>

x
-4.03314608963584e16

As you can see, by default big endian is used. So if your values are little-endian-arranged, you have to specify that explicitly.

In your example, try changing float-size(32) to little-float-size(32) and see if it works.

Also Liked

RudolfVonKrugstein

RudolfVonKrugstein

Well, I solved it exactly as you said:

  def to_linear16(<<>>) do
    <<>>
  end

  # use littel-float-size here, as it seems our floats are littel endian
  def to_linear16(<<v::little-float-size(32), r::binary>>) do
    Logger.debug("#{inspect(v)}")
    f =
      if v < 0 do
        <<trunc(v * 0x8000)::size(16)>>
      else
        <<trunc(v * 0x7FFF)::size(16)>>
      end

    f <> to_linear16(r)
  end

I am still not sure, if this is an efficient way, but it works.

I might switch to the membrance framework, as it seems to have functionality like this build in.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement