# Reading int values from audio input

**URL:** <https://discourse.processing.org/t/reading-int-values-from-audio-input/6476>\
**Category:** Libraries\
**Created:** [December 10, 2018, 2:19pm UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476 "2018-12-10T14:19:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fastfourier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fastfourier/32/2764_2.png) [@fastfourier](https://discourse.processing.org/u/fastfourier)\
**Post date:** [December 10, 2018, 2:19pm UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476/1 "2018-12-10T14:19:42Z")

</div>

hi all,

Is there any library which allows me to open an audio input (either a file or a real one) and read it as integers?  
All the ways I’ve tried so far present the audio data as floats from -1.0 to +1.0.

thanks!

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [December 10, 2018, 9:28pm UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476/2 "2018-12-10T21:28:25Z")

</div>

> [@fastfourier](#):
>
> as floats from -1.0 to +1.0.

Here could be a solution :

You can use the function `map()` ([map() / Reference / Processing.org](https://processing.org/reference/map_.html)) to map your audio data between -1.0 to 1.0 to an int range (for example 0 to 100):

```auto
int mapped_value = (int) map(0.5,-1.0,1.0,0,100);
//mapped_value = 75

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 10, 2018, 11:02pm UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476/3 "2018-12-10T23:02:10Z")

</div>

> [@josephh](#):
>
> You can use the function [**map()**](https://processing.org/reference/map_.html)…

Or alternatively add `1` to _value_, multiply it by `50` and then [**round()**](https://processing.org/reference/round_.html) it: 🤓

```java
final float value = .5;
final int mapped = round((value + 1.0) * 50.0);
println(mapped); // 75
exit();

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [December 10, 2018, 11:46pm UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476/4 "2018-12-10T23:46:20Z")

</div>

Use JavaSound directly and you’ll get integers. Well, raw bytes that are probably shorts. If you want integer values far better to get the raw value from the device/file than convert to float and back again.

But, why do you want integers out of interest? Normalised floats are in general far more useful, hence why most libraries at a higher level give you them.

---

<div class="post-metadata">

**Author:** ![fastfourier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fastfourier/32/2764_2.png) [@fastfourier](https://discourse.processing.org/u/fastfourier)\
**Post date:** [December 11, 2018, 12:50am UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476/5 "2018-12-11T00:50:22Z")

</div>

Thanks everyone. I managed to get what I wanted by doing

```auto
map (in.left.get(x), -1.0, 1.0, -32768, 32768));

```

I’m decoding non-audio data being sent over SPDIF, which is why the floats aren’t as useful. Maybe I’ll try the JavaSound approach.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [December 11, 2018, 9:16am UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476/6 "2018-12-11T09:16:55Z")

</div>

I assume you have some margin of error in the values, but note strictly speaking the range of a 16-bit signed sample is -32768 to 32767. There’s a good chance the float conversion maps -1.0:1.0 to -32767:32767.

---

<div class="post-metadata">

**Author:** ![fastfourier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fastfourier/32/2764_2.png) [@fastfourier](https://discourse.processing.org/u/fastfourier)\
**Post date:** [December 11, 2018, 1:55pm UTC](https://discourse.processing.org/t/reading-int-values-from-audio-input/6476/7 "2018-12-11T13:55:38Z")

</div>

Oh yes, thank you. I should have realised that. I can feed it a self-generated file and see what numbers map to what.

At the moment it’s working fine for this exercise. The audio contains multiple streams of data that are one bit wide and I’m only looking at one of them at the moment, so I only need to detect one bit flipping back and forth.
