# Newbie trying to play with audio

**URL:** <https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598>\
**Category:** General JUCE discussion\
**Created:** [February 23, 2008, 1:56pm UTC](https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598 "2008-02-23T13:56:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![portscan](https://avatars.discourse-cdn.com/v4/letter/p/c67d28/32.png) [@portscan](https://forum.juce.com/u/portscan)\
**Post date:** [February 23, 2008, 1:56pm UTC](https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598/1 "2008-02-23T13:56:18Z")

</div>

Hello all.

I’m new to this forum.  
I played a little with juce during the last year.  
But was only on midi.  
So first : big congrats for this lib as it’s what helped me started c++.

Now i’m starting to play with the audio.  
I’m just trying to open a wav file, fill an AudioSampleBufferfrom a reader, and then write a new wave File.  
Of course the idea will be to play with the samples in the buffer.

Anyway, the code below is what i did.  
the file “seems” to be opened fine and written, but i can’t open it in any audio editor. Except in vlc.  
I believe i don’t use the metadata correctly.  
Plus the sizes are different.

Anyone can tell me what’s wrong in it ?

Thanks a lot.

h

[code]#include “juce.h”

class CoreEngine : public Thread  
{  
public :  
CoreEngine ();  
~CoreEngine ();

public:

```
TextEditor* text;
ComboBox *projet;

String outFileName;
FileInputStream *fileInputStream;
FileOutputStream *fileOutputStream;
WavAudioFormat *format;
AudioFormatReader *reader;
AudioFormatWriter *writer;
AudioSampleBuffer *buffer;

void run();

juce_UseDebuggingNewOperator

```

};[/code]

cpp

[code]  
void CoreEngine::run()  
{  
outFileName = “outwave.wav”;  
text-\>setText(T(“Opening File”));  
wait(1000);  
fileInputStream = new FileInputStream(projet-\>getText());  
fileOutputStream = new FileOutputStream(outFileName);  
format= new WavAudioFormat;

```
text->setText(T("Creating Input"));
wait(1000);
reader = format->createReaderFor(fileInputStream,false);	

text->setText(T("Creating Output"));
wait(1000);
writer = format->createWriterFor(fileOutputStream,reader->sampleRate,reader->numChannels,reader->bitsPerSample,&reader->metadataValues,0);

text->setText(T("Creating Processor"));
wait(1000);
buffer = new AudioSampleBuffer(reader->numChannels,reader->lengthInSamples);

text->setText(T("Processing File"));
wait(1000);
buffer->readFromAudioReader(reader,0,reader->lengthInSamples,0,true,true);	

text->setText(T("Writing to Output"));
wait(1000);
buffer->writeToAudioWriter(writer,0,buffer->getNumSamples());

text->setText(T("Finished !!!"));
wait(1000);

```

}[/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:** [February 25, 2008, 4:19pm UTC](https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598/2 "2008-02-25T16:19:13Z")

</div>

Nowadays, as soon as I see the phrases “newbie” and “files not being written properly” there’s no need to read any further to know the problem - delete your output stream when you’re finished with it!!

---

<div class="post-metadata">

**Author:** ![portscan](https://avatars.discourse-cdn.com/v4/letter/p/c67d28/32.png) [@portscan](https://forum.juce.com/u/portscan)\
**Post date:** [February 25, 2008, 6:18pm UTC](https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598/3 "2008-02-25T18:18:27Z")

</div>

Sorry but I didn’t write the file correctly.  
i’m sure I just didn’t do it properly. That’s why i’m here.  
Thanks for your answer.  
I can open it actually, but as a raw wave, not as a .wav.  
So I’ll try what you’re telling me and play more with it.

---

<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:** [February 25, 2008, 8:12pm UTC](https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598/4 "2008-02-25T20:12:01Z")

</div>

[quote=“portscan”]Sorry but I didn’t write the file correctly.  
i’m sure I just didn’t do it properly. That’s why i’m here.  
Thanks for your answer.  
I can open it actually, but as a raw wave, not as a .wav.  
So I’ll try what you’re telling me and play more with it.[/quote]

Er, you sound a bit pissed-off there? Sorry, my answer was supposed to be friendly and jokey, not to be taken as a criticism. Loads of people seem to keep making this same mistake, but I can’t think of a good way to trigger an assertion or something to let them know about it.

---

<div class="post-metadata">

**Author:** ![portscan](https://avatars.discourse-cdn.com/v4/letter/p/c67d28/32.png) [@portscan](https://forum.juce.com/u/portscan)\
**Post date:** [February 25, 2008, 8:21pm UTC](https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598/5 "2008-02-25T20:21:51Z")

</div>

hum ok. 😉 just had a hard day.  
i did what you told me to.  
but i still can’t open it as a .wav  
can you tell me where my mistake is ?  
thanks.

---

<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:** [February 25, 2008, 9:21pm UTC](https://forum.juce.com/t/newbie-trying-to-play-with-audio/2598/6 "2008-02-25T21:21:31Z")

</div>

Just remember to delete the output stream object (and all your other memory leaks too, of course).
