# Speex stream to wav

**URL:** <https://community.wowza.com/t/speex-stream-to-wav/34339>\
**Category:** Wowza Streaming Engine\
**Created:** [February 23, 2011, 2:14am UTC](https://community.wowza.com/t/speex-stream-to-wav/34339 "2011-02-23T02:14:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris\_Y](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@Boris\_Y](https://community.wowza.com/u/Boris_Y)\
**Post date:** [February 23, 2011, 2:14am UTC](https://community.wowza.com/t/speex-stream-to-wav/34339/1 "2011-02-23T02:14:16Z")

</div>

Hi,

I’m publishing my microphone using Speex codec from Flash to Wowza, and I want to decode the audio on the server using JSpeex.

Actually I want to save Speex audio stream into wave file.

I see similar questions on Wowza forum, but unfortunately can’t get all this working.

Can anybody post the sample which really works (including 1-bit header concatenation, etc.) ?

I will be really thankful!

---

<div class="post-metadata">

**Author:** ![Charlie\_Good](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/charlie_good/32/383_2.png) [@Charlie\_Good](https://community.wowza.com/u/Charlie_Good)\
**Post date:** [February 23, 2011, 4:48am UTC](https://community.wowza.com/t/speex-stream-to-wav/34339/2 "2011-02-23T04:48:32Z")

</div>

WE (Wowza) do not have sample code for this.

Charlie

---

<div class="post-metadata">

**Author:** ![Marcel\_Rouwenhorst](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Marcel\_Rouwenhorst](https://community.wowza.com/u/Marcel_Rouwenhorst)\
**Post date:** [March 30, 2011, 1:16pm UTC](https://community.wowza.com/t/speex-stream-to-wav/34339/3 "2011-03-30T13:16:51Z")

</div>

hi borisbsu,

I’m trying to get your example of recording an wav to work. A wav file is created but when I play the wav file the recorded sound is really distorted. It’s hard to understand the recording.

Help would be highly appreciated!

Kind Regards,

Marcel

---

<div class="post-metadata">

**Author:** ![Boris\_Y](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@Boris\_Y](https://community.wowza.com/u/Boris_Y)\
**Post date:** [February 23, 2011, 5:09pm UTC](https://community.wowza.com/t/speex-stream-to-wav/34339/4 "2011-02-23T17:09:59Z")

</div>

Below is the example if anybody will look for the same question in the future:

(It’s based on the following package [https://www.wowza.com/downloads/forums/videopassthru/VideoPassThru.zip](https://www.wowza.com/downloads/forums/videopassthru/VideoPassThru.zip))

```auto
public class MediaStreamAudioRecord extends MediaStreamLive {
public class MediaStreamAudioRecord extends MediaStreamLive {
    private int audioBytes = 0;
    private int videoBytes = 0;
    private int dataBytes = 0;
    private ByteBuffer audioBuffer = null;
    private ByteBuffer dataBuffer = null;
    private ByteBuffer videoBuffer = null;
    private AudioFileWriter spxWriter;
    private AudioFileWriter wavWriter;
    public void startPublishing() {
        super.startPublishing();
        WMSLoggerFactory.getLogger(null).info("MediaStreamAudioRecord: startPublishing");
        try {
            // Speex output
            spxWriter = new OggSpeexWriter(1 /* Wideband */, 16000, 1, 1, false);
            spxWriter.open(getStreamFileForWrite(getName(), "spx", getExt()));
            spxWriter.writeHeader("Recorded with Wowza");
            // Wave output
            wavWriter = new PcmWaveWriter(16000, 1);
            wavWriter.open(getStreamFileForWrite(getName(), "wav", getExt()));
            wavWriter.writeHeader("Recorded with Wowza");
        } catch (IOException e) {
            WMSLoggerFactory.getLogger(null).info(e.getMessage(), e);
        }
    }
    public void stopPublishing() {
        super.stopPublishing();
        try {
            spxWriter.close();
            wavWriter.close();
        } catch (IOException e) {
            WMSLoggerFactory.getLogger(null).info(e.getMessage(), e);
        }
        WMSLoggerFactory.getLogger(null).info("MediaStreamAudioRecord: stopPublishing");
    }
    public void addAudioData(byte[] data, int offset, int size) {
        super.addAudioData(data, offset, size);
        if (this.audioBuffer == null) {
            this.audioBuffer = ByteBuffer.allocate(this.getAudioSize());
        }
        this.audioBytes += size;
        this.audioBuffer.put(data, offset, size);
        if (this.audioBytes == this.getAudioSize()) {
            try {
                // write Speex data
                spxWriter.writePacket(audioBuffer.array(), audioBuffer.arrayOffset() + 1, audioBytes - 1);
                // decode and write Wave data
                SpeexDecoder speexDecoder = new SpeexDecoder();
                speexDecoder.init(1, 16000, 1, false);
                speexDecoder.processData(audioBuffer.array(), audioBuffer.arrayOffset() + 1, audioBytes - 1);
                byte[] decoded = new byte[speexDecoder.getProcessedDataByteSize()];
                int decsize = speexDecoder.getProcessedData(decoded, 0);
                if (decsize > 0) {
                    wavWriter.writePacket(decoded, 0, decsize);
                }
            } catch (IOException e) {
                WMSLoggerFactory.getLogger(null).info(e.getMessage(), e);
            }
            this.audioBytes = 0;
            this.audioBuffer = null;
        }
    }
}

```
