Save to output file corrupt

I am developing a UWP app. Within the app I try to save a effect to a wave file, but the file is not fully created until I close the debugger (Visual Studio 2017), and ends up being a 29 MB file when it was created from 79.5 KB file. Here is the code I am using to create the file.

        result = Factory.System_Create(out system);
        if (result == RESULT.OK)
            StorageFolder folder;
            StorageFile file;
            Channel channel;
            Sound sound;
            EffectInfoModel data;
            CREATESOUNDEXINFO info;
            Image img;
            string filename;

            img = sender as Image;
            data = (EffectInfoModel)img.Tag;

            filename = string.Format("{0}_{1}.wav", data.Name, DateTime.Now.ToString("yyyyMMddhhmmss"));
            folder = await ((App)Application.Current).GetRecordingsFolderAsync();
            file = await folder.CreateFileAsync(filename);
            result = system.setOutput(OUTPUTTYPE.WAVWRITER);
            result = system.init(2, INITFLAGS.NORMAL, Marshal.StringToHGlobalAnsi(file.Path));

            _effects = new EffectsMasterModel();
            _effects.EffectName = data.Name;
            _effects.CanProcessAudio = true;
            Factory.System_Create(out system);
            system.init(32, INITFLAGS.NORMAL, IntPtr.Zero);
            info = new CREATESOUNDEXINFO();
            system.createSound(_audioFile.Path, MODE.DEFAULT, ref info, out sound);
            system.playSound(sound, null, true, out channel);

            _effects.FmodSystem = system;
            _effects.FmodChannel = channel;
            _effects.FmodSound = sound;
            if (SetSoundParameters(_effects))
                ShowSaving = true;


    private bool SetSoundParameters(EffectsMasterModel effects)
        FMOD.System system;
        ChannelGroup masterGroup;
        Channel channel;
        Sound sound;

        system = effects.FmodSystem;
        system.getMasterChannelGroup(out masterGroup);
        channel = effects.FmodChannel;
        sound = effects.FmodSound;
        effects.CanProcessAudio = true;
        switch (effects.EffectName)
            case "cave":
                    REVERB_PROPERTIES props;
                    DSP dspEcho;

                    system.createDSPByType(DSP_TYPE.ECHO, out dspEcho);
                    masterGroup.addDSP(0, dspEcho);
                    props = PRESET.CAVE();
                    system.setReverbProperties(0, ref props);
                    dspEcho.setParameterFloat((int)DSP_ECHO.DELAY, 900);

           // More case statements that create different effects
        return (effects.CanProcessAudio);

    private void HandleSoundStop()
        if (ShowSaving)
            ShowSaving = false;

            _playImageControl.Source = _playImage;

        _pitchShift = 0;

    public void Release()
        if (FmodSound != null)
            FmodSound = null;

        if (FmodSystem != null)
            FmodSystem = null;

    public void RelaseDSP()
        if (FmodSystem != null)
            ChannelGroup group;

            FmodSystem.getMasterChannelGroup(out group);
            if (group != null)
                DSP dsp;
                int count;

                count = 0;
                group.getNumDSPs(out count);
                for (; count > 0; --count)
                    group.getDSP(count - 1, out dsp);

The HandleSoundStop() method is called from a 250ms watchdog timer that checks the isPlaying property of the Channel created from the playSound call.

Release() and ReleaseDSP() are methods in _effects.

Also, while the effect is being saved I can also hear it through the speakers. Is there any way to not route to speakers during file save?

you created 2 system objects, 1 never has a sound played through it, and doesnt get closed (which is what closes the output wavewriter file handle), and the 2nd is a normal system object which is default to your soundcard, and thats the one you actually call playsound.

Just delete the code that initializes the 2nd system object.

1 Like

That’s what happens when you copy and paste things. Thanks for the quick response.