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

Recording on WasapiCapture,a lot of noise. #473

Open BigQ517 opened 1 year ago

BigQ517 commented 1 year ago

I`m recording on WasapiCapture , sampleRate:16000, bits:16 and channels:1. There is a lot of noise when the sound is high. Is there a good solution?

BigQ517 commented 1 year ago
this.soundIn = new WasapiCapture();
            if (currentDevice != null)
            {
                this.soundIn.Device = currentDevice;
            }

            soundIn.Initialize();
            SoundInSource soundInSource = new SoundInSource(soundIn) { FillWithZeros = false };
            convertedSource = soundInSource
             .ChangeSampleRate(16000) // sample rate
             .ToSampleSource()
             .ToWaveSource(16); //bits per sample

            convertedSource =  convertedSource.ToMono() ;
            soundInSource.DataAvailable += OnDataAviailable;
            soundIn.Stopped += OnRecordingStopped;
            soundIn.Start();
eugeneYz commented 3 months ago

Me too. The recorded audio lasted a long time, but I didn't record it that long and it was all noise

private WasapiCapture capture;
private WaveWriter writer;

public void StartRecording(string fileName)
{
    capture = new WasapiCapture();
    capture.Initialize();

    // 设置录音格式
    var waveFormat = new WaveFormat(16000, 16, 1); // 16000Hz, 16bit, Mono

    writer = new WaveWriter(fileName, waveFormat);

    capture.DataAvailable += (s, e) =>
    {
        // 将捕获的音频数据写入文件
        writer.Write(e.Data, e.Offset, e.ByteCount);
    };

    capture.Start();
}

public void StopRecording()
{
    capture.Stop();
    capture.Dispose();
    writer.Dispose();
}
filoe commented 3 months ago

You are not supposed to define a wavformat yourself.

The WaveWriter will not convert the input to any desired output format. It will just write the raw data and place the necessary headers in order to write a valid .wav file.

Pass the capture.WaveFormat to the WaveWriter and it will be good or make sure to convert the captured data to the output format.

If the format is correct, the sound won’t be noisy and will also match in length.

Von: eugeneYz @.> Gesendet: Mittwoch, 6. März 2024 03:25 An: filoe/cscore @.> Cc: Subscribed @.***> Betreff: Re: [filoe/cscore] Recording on WasapiCapture,a lot of noise. (Issue #473)

Me too. The recorded audio lasted a long time, but I didn't record it that long and it was all noise

private WasapiCapture capture; private WaveWriter writer;

public void StartRecording(string fileName) { capture = new WasapiCapture(); capture.Initialize();

// 设置录音格式
var waveFormat = new WaveFormat(16000, 16, 1); // 16000Hz, 16bit, Mono

writer = new WaveWriter(fileName, waveFormat);

capture.DataAvailable += (s, e) =>
{
    // 将捕获的音频数据写入文件
    writer.Write(e.Data, e.Offset, e.ByteCount);
};

capture.Start();

}

public void StopRecording() { capture.Stop(); capture.Dispose(); writer.Dispose(); }

— Reply to this email directly, view it on GitHub https://github.com/filoe/cscore/issues/473#issuecomment-1979962180 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHXVYMXGYS6VTMMA4PVCDTYWZ463AVCNFSM57GWIFV2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJXHE4TMMRRHAYA . You are receiving this because you are subscribed to this thread. https://github.com/notifications/beacon/ABHXVYPTKQBMZXXBCSQEO3LYWZ463A5CNFSM57GWIFV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOOYB5GRA.gif Message ID: @. @.> >

eugeneYz commented 3 months ago

I have to define a Waveformat myself because the API that receives the audio specifies this format.Are there any other solutions?

赵铮 @.***

 

------------------ 原始邮件 ------------------ 发件人: "filoe/cscore" @.>; 发送时间: 2024年3月6日(星期三) 下午3:02 @.>; @.**@.>; 主题: Re: [filoe/cscore] Recording on WasapiCapture,a lot of noise. (Issue #473)

You are not supposed to define a wavformat yourself.

The WaveWriter will not convert the input to any desired output format. It will just write the raw data and place the necessary headers in order to write a valid .wav file.

Pass the capture.WaveFormat to the WaveWriter and it will be good or make sure to convert the captured data to the output format.

If the format is correct, the sound won’t be noisy and will also match in length.

Von: eugeneYz @.>
Gesendet: Mittwoch, 6. März 2024 03:25 An: filoe/cscore
@.> Cc: Subscribed @.***> Betreff: Re: [filoe/cscore] Recording on WasapiCapture,a lot of noise. (Issue #473)

Me too. The recorded audio lasted a long time, but I didn't record it that long and it was all noise

private WasapiCapture capture; private WaveWriter writer;

public void StartRecording(string fileName) { capture = new WasapiCapture(); capture.Initialize();

 // 设置录音格式 
 var waveFormat = new WaveFormat(16000, 16, 1); // 16000Hz, 16bit, Mono 

 writer = new WaveWriter(fileName, waveFormat); 

 capture.DataAvailable += (s, e) => 
 { 
     // 将捕获的音频数据写入文件 
     writer.Write(e.Data, e.Offset, e.ByteCount); 
 }; 

 capture.Start(); 

}

public void StopRecording() { capture.Stop(); capture.Dispose(); writer.Dispose(); }

— Reply to this email directly, view it on GitHub <https://github.com/filoe/cscore/issues/473#issuecomment-1979962180&gt; , or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABHXVYMXGYS6VTMMA4PVCDTYWZ463AVCNFSM57GWIFV2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOJXHE4TMMRRHAYA&gt; . You are receiving this because you are subscribed to this thread. <https://github.com/notifications/beacon/ABHXVYPTKQBMZXXBCSQEO3LYWZ463A5CNFSM57GWIFV2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOOYB5GRA.gif&gt; Message ID: @. @.> >

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>