How add properties at this object (BasicAudio library)

maoca

Member
Joined
Oct 30, 2023
Messages
5
Programming Experience
Beginner
hi everyone.
I'm a very beginner in object programming and I would like to record audio from my computer to save a wav file. I found this light library BasicAudio (GitHub - blakepell/BasicAudio: A simple class library that facilitates recording audio in Windows desktop applications.)
I would like to chage the properties of the object, but I don`t know what I doing bad:
C#:
using static BasicAudio.Recording;

namespace BasicAudio_1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // There are properties on this object to change the quality recording
          
            var audioRecorder = new BasicAudio.Recording();
           
            audioRecorder.Filename = @"c:\tmp\test.wav";
            audioRecorder.Channels = ChannelValue.Stereo;
            audioRecorder.BitsPerSample = BitsPerSampleValue.High;
            audioRecorder.SamplesPerSecond = SamplesPerSecValue.High;
            audioRecorder.StartRecording();
            Thread.Sleep(5000);
            // File is written out to disk when this is called.  The filename property must already be set.
            audioRecorder.StopRecording();

        }
    }
}
Could help me?
thanks
 
Last edited by a moderator:
Solution
I worked more with Naudio and I found it. It's works for me :)
C#:
using NAudio.Wave;
using System.Reflection.PortableExecutable;


WasapiLoopbackCapture CaptureInstance = null;
WaveFileWriter RecordedAudioWriter = null;

Console.WriteLine("NAudio Recrding.....");


void StartSoundRecord()
{

    string outputFilePath = @"C:\tmp\RecordedSound.wav";


    
    // Redefine the capturer instance with a new instance of the LoopbackCapture class
    CaptureInstance = new WasapiLoopbackCapture();
    // Redefine the audio writer instance with the given configuration
    int sampleRate = 16000; // 16 kHz
    int channels = 1; // mono
    int bits = 16;
    var recordingFormat = new WaveFormat(sampleRate, bits, channels)...
What problem are you running into when you try to change the properties? Which property or properties are changing? What error are you getting? Or if there is no error, what behavior are you seeing?
 
What problem are you running into when you try to change the properties? Which property or properties are changing? What error are you getting? Or if there is no error, what behavior are you seeing?

Thans for your reply.
I would like to get one wav file with this properties: Stereo, 16bits and 44100 hz, .
BitsPerSampleValue High = 16
ChannelValue = Stereo
High = 44100

But when I run the programm I get a mono, 11025 hz and 8 bits file. The program compile without problems in a Visual Studio 2022, but I can't change the properties using:

C#:
audioRecorder.Channels = ChannelValue.Stereo;
audioRecorder.BitsPerSample = BitsPerSampleValue.High;
audioRecorder.SamplesPerSecond = SamplesPerSecValue.High;
I see this: BasicAudio/src/BasicAudio/Recording.cs at master · blakepell/BasicAudio
How can I pass the properties correctly?
Thanks.
 
Last edited by a moderator:
Lines 212-216 seem to suggest that the MCI command string wants things in a particular order in Windows Vista. It's likely that the order has changed again in the current version of Windows. Have you tried contacting the author to see they can provide more insight?

Why not just use NAudio is you can't get any traction with this library?
 
Thanks again.
1. I can try it, I going to send a email, maybe I get a response.
2. I know Naudio, but they use buffers to record the file and I thought it is more complex for me... I used ffmpeg, OpenCV and VLC libraries but I'm a bit lost. In fact I managed to capture the desktop and the loopback audio but the image looked rotated 180 degrees!! I didn't know why :(
Regards.
 
I worked more with Naudio and I found it. It's works for me :)
C#:
using NAudio.Wave;
using System.Reflection.PortableExecutable;


WasapiLoopbackCapture CaptureInstance = null;
WaveFileWriter RecordedAudioWriter = null;

Console.WriteLine("NAudio Recrding.....");


void StartSoundRecord()
{

    string outputFilePath = @"C:\tmp\RecordedSound.wav";


    
    // Redefine the capturer instance with a new instance of the LoopbackCapture class
    CaptureInstance = new WasapiLoopbackCapture();
    // Redefine the audio writer instance with the given configuration
    int sampleRate = 16000; // 16 kHz
    int channels = 1; // mono
    int bits = 16;
    var recordingFormat = new WaveFormat(sampleRate, bits, channels);
    CaptureInstance.WaveFormat = recordingFormat;
     

    
    //RecordedAudioWriter = new WaveFileWriter(outputFilePath, CaptureInstance.WaveFormat);
    RecordedAudioWriter = new WaveFileWriter(outputFilePath, recordingFormat);
    // When the capturer receives audio, start writing the buffer into the mentioned file
    CaptureInstance.DataAvailable += (s, a) =>
    {
        RecordedAudioWriter.Flush();
        // Write buffer into the file of the writer instance
        RecordedAudioWriter.Write(a.Buffer, 0, a.BytesRecorded);
    };

    // When the Capturer Stops, dispose instances of the capturer and writer
    CaptureInstance.RecordingStopped += (s, a) =>
    {
        RecordedAudioWriter.Dispose();
        RecordedAudioWriter = null;
        CaptureInstance.Dispose();
    };

    // Start audio recording !
    CaptureInstance.StartRecording();

}

void StopSoundRecord()
{
    if (CaptureInstance != null)
    {
        CaptureInstance.StopRecording();
    }
}

StartSoundRecord();
Thread.Sleep(10000);
StopSoundRecord();
 
Last edited by a moderator:
Solution
When posting multiline code, please use CODE tags (</>) instead of ICODE tags (>_). The icons on the toolbar go against the StackOverflow conventions, so it's understable why you keep selecting the ICODE tags instead of the CODE tags.
 

Latest posts

Back
Top Bottom