# Exported audio wav file is distorted

**URL:** https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115
**Category:** General
**Created:** [14 March 2020 19:28 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115 "2020-03-14T19:28:05Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![albydnc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/albydnc/32/8878_2.png) [@albydnc](https://discourse.nodered.org/u/albydnc)
#### Post date: [14 March 2020 19:28 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115/1 "2020-03-14T19:28:06Z")

</div>

I have a PCM buffer that has to be saved as a file. The raw audio is not distorted and by playing it with the **node-red-contrib-play-audio** works as expected.  
When I try to save it using the **file** node, the file saved is really distorted.  
I use **node-red-contrib-wav** to add the wav headers.

Here a mini flow to demonstrate the issue:

```auto
[{"id":"3f882ab9.642dd6","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"5d269671.055368","type":"wav-headers","z":"3f882ab9.642dd6","name":"","action":"add","channels":1,"samplerate":"11025","bitwidth":"8","x":670,"y":120,"wires":[["dba3db72.3f0a18","5af79fed.b692b"],[]]},{"id":"dba3db72.3f0a18","type":"play audio","z":"3f882ab9.642dd6","name":"","voice":"9","x":890,"y":80,"wires":[]},{"id":"5af79fed.b692b","type":"file","z":"3f882ab9.642dd6","name":"","filename":"/tmp/test.wav","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"binary","x":890,"y":120,"wires":[[]]},{"id":"c7bdab08.cc0088","type":"inject","z":"3f882ab9.642dd6","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":120,"wires":[["8abe4665.7cb898"]]},{"id":"8abe4665.7cb898","type":"http request","z":"3f882ab9.642dd6","name":"","method":"GET","ret":"bin","paytoqs":false,"url":"http://mauvecloud.net/sounds/pcm0811m.wav","tls":"","persist":false,"proxy":"","authType":"","x":270,"y":120,"wires":[["7597b016.b33b3"]]},{"id":"7597b016.b33b3","type":"wav-headers","z":"3f882ab9.642dd6","name":"","action":"del","channels":1,"samplerate":22050,"bitwidth":16,"x":460,"y":120,"wires":[["5d269671.055368"],[]]}]

```

---

<div class="post-metadata">

### Author: ![albydnc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/albydnc/32/8878_2.png) [@albydnc](https://discourse.nodered.org/u/albydnc)
#### Post date: [14 March 2020 23:04 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115/2 "2020-03-14T23:04:37Z")

</div>

@BartButenaers I'm tagging you since maybe you know how to help!

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [15 March 2020 07:28 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115/3 "2020-03-15T07:28:48Z")

</div>

Hey Alberto (@albydnc),

In those cases, always try to simplify your flow as much as possible to find the root case.  
Seems not to be related to my **node-red-contrib-wav** node, because it also fails with this simple flow:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6eeb327786253ea8397f2a1bb7e7c1a1ad42f197.png)

```auto
[{"id":"4feed947.cded28","type":"file in","z":"b3db206e.b7139","name":"","filename":"/tmp/test.wav","format":"","chunk":false,"sendError":false,"encoding":"none","x":410,"y":620,"wires":[["5c99ef16.93d88"]]},{"id":"57de7ec8.184eb","type":"inject","z":"b3db206e.b7139","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":620,"wires":[["4feed947.cded28"]]},{"id":"5c99ef16.93d88","type":"play audio","z":"b3db206e.b7139","name":"","voice":"9","x":590,"y":620,"wires":[]},{"id":"a833df3d.c5369","type":"file","z":"b3db206e.b7139","name":"","filename":"/tmp/test.wav","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"binary","x":590,"y":520,"wires":[[]]},{"id":"359ebe7c.62df62","type":"inject","z":"b3db206e.b7139","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":520,"wires":[["85018a22.774868"]]},{"id":"85018a22.774868","type":"http request","z":"b3db206e.b7139","name":"","method":"GET","ret":"bin","paytoqs":false,"url":"http://mauvecloud.net/sounds/pcm0811m.wav","tls":"","persist":false,"proxy":"","authType":"","x":410,"y":520,"wires":[["a833df3d.c5369"]]},{"id":"3bc85905.fa6366","type":"comment","z":"b3db206e.b7139","name":"Step 1 : download the WAV file and store it in a file","info":"","x":350,"y":480,"wires":[]},{"id":"8231499.8dfecb8","type":"comment","z":"b3db206e.b7139","name":"Step 2 : play the downloaded WAV file","info":"","x":310,"y":580,"wires":[]}]

```

The downloaded WAV is saved as _ **binary** _ which should be fine... The WAV contains both _binary data_ (i.e. the audio samples themselves) and _headers_ (to explain to the player how the data has been recorded). And from this [discussion](https://stackoverflow.com/a/38067764) I assume that both need to be stored as binary! No clue now why this audio becomes distorted ...

But at the moment I am recovering from a surgery, so not in the mood for an in depth analysis 🥺 Since this _ **problem is about storing data in a file** _, perhaps other community members can help you now. Otherwise I will have a look in a week or so... But then you should mention me again to get my attention!

Good luck with it!  
Bart

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [15 March 2020 07:45 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115/4 "2020-03-15T07:45:58Z")

</div>

When you play the stored file with a third-party player (e.g. VLC) then it becomes clear that _ **the problem is somewhere in the File Write node** _ (not in the File Read node)! You can see e.g. [here](https://stackoverflow.com/a/12761716) that the file needs to be in _ **Little Endian** _ format. I have no idea whether that is the problem, or how it works or how you can solve it ...

---

<div class="post-metadata">

### Author: ![albydnc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/albydnc/32/8878_2.png) [@albydnc](https://discourse.nodered.org/u/albydnc)
#### Post date: [15 March 2020 10:48 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115/5 "2020-03-15T10:48:02Z")

</div>

Thanks for the help!  
I opened a new [discussion](https://discourse.nodered.org/t/writing-buffer-to-file-not-writing-correctly/23132) to take care of the issue.

Hope you get well soon!

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [15 March 2020 11:50 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115/6 "2020-03-15T11:50:02Z")

</div>

Works ok if you use `default` encoding.  
Any of the others use iconv.encode - not sure why encode of (binary) buffer to binary changes it but it does ☹

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [14 May 2020 11:50 UTC](https://discourse.nodered.org/t/exported-audio-wav-file-is-distorted/23115/7 "2020-05-14T11:50:12Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
