filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.14k stars 450 forks source link

Appending recording onto another wav #441

Closed DonTheBradman closed 3 years ago

DonTheBradman commented 3 years ago

Hi there, so I have a recorder program - but the clients changed what they want. so now instead of recording the one file I need to start recording and after a minute create the first wav. Then every minute after that I need to append the new audio onto the original wav sort of like a rolling backup. I was hoping to know if you can do that with the library, or if theres an example I missed. Thanks very much.

Heres the code for capturing at the moment:

        _soundIn = new WasapiLoopbackCapture();
        _soundIn.Initialize();

        var soundInSource = new SoundInSource(_soundIn);
        var singleBlockNotificationStream = new SingleBlockNotificationStream(soundInSource.ToSampleSource());

        _finalSource = singleBlockNotificationStream.ToWaveSource(16);
        _finalSource.ChangeSampleRate(5512);
        _writer = new WaveWriter(file, _finalSource.WaveFormat);

        byte[] buffer = new byte[_finalSource.WaveFormat.BytesPerSecond / 2];

        soundInSource.DataAvailable += (s, e) =>
        {
            int read;
            while ((read = _finalSource.Read(buffer, 0, buffer.Length)) > 0)

                if (isPaused == false)
                {
                    _writer.Write(buffer, 0, read);

                }
        };

        singleBlockNotificationStream.SingleBlockRead += SingleBlockNotificationStreamOnSingleBlockRead;

        _soundIn.Start();

    }

    private void SingleBlockNotificationStreamOnSingleBlockRead(object sender, SingleBlockReadEventArgs e)
    {
        _graphVisualization.AddSamples(e.Left, e.Right);
    }
filoe commented 3 years ago

Of course you can do that. Just dispose the waveWriter and create a newcone within the while loop as soon as the length of the written data reaches one minute.