# Writing WAV (not bwav)

**URL:** <https://forum.juce.com/t/writing-wav-not-bwav/1284>\
**Category:** General JUCE discussion\
**Created:** [December 11, 2006, 1:28pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284 "2006-12-11T13:28:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![haydxn](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@haydxn](https://forum.juce.com/u/haydxn)\
**Post date:** [December 11, 2006, 1:28pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/1 "2006-12-11T13:28:26Z")

</div>

This is for another user on KVR (jupiter8) who’s slow to register here!

He’s having trouble writing a WAV file that can be read by audacity, as it always writes a BWAV. He doesn’t know how to make it write a standard wave file. Here’s his post:

> [@jupiter8](#):
>
> I got my audio nice and tidy in an AudioSampleBuffer. I’d like to save the contents of that file to disk.
> 
> I created a FileWriter but when i call the write function it expects an array of arrays (WTF?). So if someone could walk me thru this i’d be one happy camper fer sure.
> 
> …
> 
> Halleluja,i actually managed to write a wav file to disk! Funny that,i tried for 24 hours and 2 minutes after i posted here i figured it out. BUT!!!
> 
> If i try to open it in Audacity it crashes! Oh crap.  
> Look some further i discover it is a Broadcast wav (i think). Why god? Why? Hmmmm Cubase can read BWAVs.  
> Works fine in Cubase! That’s weird.
> 
> I can import the wav raw in Audacity and it plays fine except for some garbage in the beginning. And when i check the ID tag on the WAV file it reports a length of 0:00 (m:ss) when it in fact is around 2 sec.
> 
> However i see no way of just making a regular wav as the function WavAudioFormat::createWriterFor must have a StringPairArray with bwav metadata as a parameter.
> 
> I tried with just passing 0 as an argument and it did’nt complain and there was less garbage in the wav file.There still was some and it still reports a length of 0:00 m:ss.  
> And it the wav still crashes Audacity.
> 
> I think i’ll just screw Juce AudioFormatWriter and make my own Format chunk. I onlyh want to export 32 bit float wavs anyways so how hard can that be. Very Happy Maybe i even learn something in the process.
> 
> While you’re at it,i can’t even make a new directory.  
> How sad is that ? Just create one single new directory on my harddrive should’nt be that hard but i’m struggling.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 11, 2006, 2:58pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/2 "2006-12-11T14:58:42Z")

</div>

That’s pathetic if audacity can’t parse a WAV properly. A BWAV is just a WAV with extra stuff in the header, so any parser that correctly reads RIFF files should just ignore the extra bits and skip straight to the audio, without even needing to understand the BWAV format at all.

Anyway, looking at my WavAudioFormatWriter code, it does write an ordinary wav file if you pass it an empty StringPairArray. It’s easy enough to step through that code in the debugger and see what’s happening, so he should try that and let me know if there’s a real bug here, or if it’s just him making newbie mistakes.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 11, 2006, 3:00pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/3 "2006-12-11T15:00:32Z")

</div>

[quote]While you’re at it,i can’t even make a new directory.  
How sad is that ? Just create one single new directory on my harddrive should’nt be that hard but i’m struggling. [/quote]

…erm, File::createDirectory() isn’t too hard to understand, is it??

---

<div class="post-metadata">

**Author:** ![jupiter8](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jupiter8](https://forum.juce.com/u/jupiter8)\
**Post date:** [December 11, 2006, 4:00pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/4 "2006-12-11T16:00:18Z")

</div>

Hi everybody. For some strange reason the wav works just fine now.  
No crashes in Audacity. I can even play it in Windows Media Player which came as a surprise as it is a 32 bit float and i can’t play 24 bit files in media player. It still registers length as zero seconds and minutes though but it’s no biggie for me for the moment.

And the directory thing works as a charm as well. Thank you very much.  
I’m not even going to say what i did wrong but it is one of the noobiest noobie mistakes you can make. 😃

---

<div class="post-metadata">

**Author:** ![jupiter8](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jupiter8](https://forum.juce.com/u/jupiter8)\
**Post date:** [December 11, 2006, 4:10pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/5 "2006-12-11T16:10:56Z")

</div>

I just discovered something strange right now.  
When i create a new wav file in my program and open it in Audacity or Media Player the file is empty. So i opened it in Cubase and it works fine there. When i shut down Cubase for some strange reason it works again in media player and Audacity.

---

<div class="post-metadata">

**Author:** ![Rock\_Hardbuns](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@Rock\_Hardbuns](https://forum.juce.com/u/Rock_Hardbuns)\
**Post date:** [December 11, 2006, 4:22pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/6 "2006-12-11T16:22:34Z")

</div>

Could cubase be tampering with the file at all?

(And welcome to juce land)

---

<div class="post-metadata">

**Author:** ![jupiter8](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jupiter8](https://forum.juce.com/u/jupiter8)\
**Post date:** [December 11, 2006, 4:34pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/7 "2006-12-11T16:34:28Z")

</div>

Cubase should leave the file alone but apparently it does’nt. Have some new dicoveries. It crashed the Audacity 1.3 beta. It opens as an empty file in Audacity 1.2. When i tried to open the file in QT it complained that the movie (sic) had the wrong duration. So there is definitly something funky with it reporting 0:00 sec length.

Anyways here’s what i’ve done so far, codewise:[code]  
test = new WavAudioFormat();  
theDate = new Time(2004, 12, 21, 10, 12, 0, 0);

```
	directory = new File("c:\\Wav Export");
	if (!directory->exists()){
		directory->createDirectory();
	}
	outputFile = new File("c:\\Wav Export\\test.wav");
	outputTo = outputFile->createOutputStream(); 
	StringPairArray stringPair(true); 
	stringPair = test->createBWAVMetadata("wave file", "test1", "test2", *theDate, 44100, "test3"); 
	writer = test->createWriterFor(outputTo, 44100, 1, 32,stringPair, 0); 
	buffer2->writeToAudioWriter(writer,0,buffer2->getNumSamples());[/code]
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 11, 2006, 5:28pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/8 "2006-12-11T17:28:15Z")

</div>

Looking at your code, my first thought is “have you closed the file properly?” Obviously you have to delete the audiofilewriter object or it won’t flush its data, which would leave a half-written file on disk.

---

<div class="post-metadata">

**Author:** ![jupiter8](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jupiter8](https://forum.juce.com/u/jupiter8)\
**Post date:** [December 11, 2006, 5:42pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/9 "2006-12-11T17:42:58Z")

</div>

> [@jules](#):
>
> Looking at your code, my first thought is “have you closed the file properly?” Obviously you have to delete the audiofilewriter object or it won’t flush its data, which would leave a half-written file on disk.

That’s it,thanks.  
I’ve been programming Java for 18 months now so i kinda forgot you have to clean up after yourself in C++. :oops:

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 11, 2006, 5:53pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/10 "2006-12-11T17:53:48Z")

</div>

Always check the program’s output for the leaked object report when your app quits - you should _never_ have anything in there.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://avatars.discourse-cdn.com/v4/letter/o/a8b319/32.png) [@OvermindDL1](https://forum.juce.com/u/OvermindDL1)\
**Post date:** [December 13, 2006, 6:43pm UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/11 "2006-12-13T18:43:06Z")

</div>

I am guessing that “directory” is a pointer (obviously)? To make it so you do not have to cleanup, don’t use a pointer for such things…

[code]  
theDate = Time(2004, 12, 21, 10, 12, 0, 0);

```
  directory = File("c:\\Wav Export");
  if (!directory.exists()){
     directory.createDirectory();
  }
  outputFile = File("c:\\Wav Export\\test.wav");
  outputTo = outputFile.createOutputStream();[/code]

```

Etc… He put operator=()'s in his classes for a reason. 🙂  
Not to mention you will get a speed boost thanks to not needing to dereference pointers on use and all such as well.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 9:58am UTC](https://forum.juce.com/t/writing-wav-not-bwav/1284/12 "2017-05-12T09:58:31Z")

</div>

Closed on May 12, 2017, 9:58am UTC


