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.15k stars 451 forks source link

I am trying to write samples of audio recorded by wasapi loopback capture by different wave format #363

Open mithunvs opened 5 years ago

mithunvs commented 5 years ago

I tried to write samples of different wave format .When i am using WaveWriter.Write, I got clear audio but WaveWriter.Write does not take float values ,So I had to use the WaveWriter.WriteSamples but this time i got a different audio . I have to do this because I want to reduce the size of the audio captured by the wasapi loopback capture. Is there any way by which I can write samples with different format Here is my code : ` var soundIn = new WasapiLoopbackCapture(); soundIn.Initialize(); var soundInSource = new SoundInSource(soundIn); IWaveSource convertedSource = soundInSource .ChangeSampleRate(32000) // sample rate .ToSampleSource() .ToWaveSource(16); var notificationSource = new NotificationSource(soundInSource.ToSampleSource());

        notificationSource.Interval = 5000;
        notificationSource.BlockRead += (s, e) =>
        {

            string directory = System.IO.Directory.GetCurrentDirectory();
            string filename = DateTime.Now.Ticks.ToString() /*"sample" */+ ".wav";
            directory = directory + "\\Cache\\";
            string fileName = System.IO.Path.Combine(directory + filename);

            using (var intercepts = new WaveWriter(fileName, convertedSource.WaveFormat))
            {

                Debug.Assert(e.Data.Length == e.Length);
                intercepts.WriteSamples(e.Data, 0, e.Length);
            }
            int length = TimeConverterFactory.Instance.GetTimeConverterForSource(notificationSource)
              .ToTimeSpan(convertedSource.WaveFormat, e.Length).Seconds;

        };

        var waveSource = notificationSource.ToWaveSource();
        var writer = new WaveWriter("out.wav", waveSource.WaveFormat);
        byte[] buffer = new byte[waveSource.WaveFormat.BytesPerSecond / 2];

        soundInSource.DataAvailable += (s, e) =>
        {
            int read;
            while ((read = waveSource.Read(buffer, 0, buffer.Length)) > 0)
                writer.Write(e.Data, 0, read);
        };

        soundIn.Start();`
mithunvs commented 5 years ago

Please help

JohnsonGao commented 5 years ago

Yes, you can , there is an example in Samples named as RecordWithSepcificFormat , demoed to flexible format settings. But you know , WASAPI Loopback Recording only support the format alreay internal use, so if you really need to set format different to the system internal using , you have to convert it by yourself. Otherwise If you did that arbitrarily, you would get the bad audio quality results.