receive data microphone #19
|
// Initialize OwnaudioNET (engine must be running) // Create input source // Add to mixer and start Hello, I want to receive data packets from a specified microphone length. I didn't understand how to do this from the example I found. I'm a newbie, please help. |
Replies: 3 comments 1 reply
|
Hi! Of course, I'll help you get started! var config = OwnaudioNet.CreateDefaultConfig();
config.EnableInput = true;
//You can also change other settings here!
OwnaudioNet.Initialize(config);
InputSource? inputSource = null;
AudioMixer? mixer = null;
//To rule out errors, check that the API has been initialized correctly and that the engine has been started!
if (OwnaudioNet.IsInitialized)
{
// Start the audio engine
OwnaudioNet.Start();
if (OwnaudioNet.IsRunning)
{
//Create the mixer that handles sources and many other things, then add the source to it and start everything!
var Engine = OwnaudioNet.Engine!.UnderlyingEngine;
//We create an InputSource to record the microphone/line input.
var inputSource = new InputSource(OwnaudioNet.Engine, bufferSizeInFrames: 8192);
var mixer = new AudioMixer(Engine);
mixer.AddSource( inputSource);
mixer.Start();
// Start playback (begin capturing and processing)
inputSource.Play();
//Process received data with the ReadSamples(Span<float> buffer, int frameCount) method.
}
}
//Clean up when finished
inputSource?.Stop();
if (inputSource != null) mixer?.RemoveSource(inputSource);
inputSource?.Dispose();
mixer?.Dispose(); |
|
must be used ReadSamples(buffer, frameCount) ? |
|
Hi! Own processing thread (if you don't need playback, just processing) var config = OwnaudioNet.CreateDefaultConfig();
config.EnableInput = true;
OwnaudioNet.Initialize(config);
InputSource? inputSource = null;
CancellationTokenSource? cts = null;
Task? processingTask = null;
if (OwnaudioNet.IsInitialized)
{
OwnaudioNet.Start();
if (OwnaudioNet.IsRunning)
{
inputSource = new InputSource(OwnaudioNet.Engine!, bufferSizeInFrames: 8192);
// Watch for data loss
inputSource.BufferUnderrun += (s, e) =>
Console.WriteLine($"WARNING: {e.DroppedFrames} frame lost!");
inputSource.Play();
// Calculate the frame size from the engine settings
int channels = OwnaudioNet.Engine!.Config.Channels;
int frameSize = 512; // Number of frames in one processing cycle
float[] readBuffer = new float[frameSize * channels];
cts = new CancellationTokenSource();
processingTask = Task.Factory.StartNew(() =>
{
while (!cts.Token.IsCancellationRequested)
{
int framesRead = inputSource.ReadSamples(readBuffer, frameSize);
if (framesRead > 0)
{
// Process the data — e.g. save to file, analyze, etc.
ProcessAudio(readBuffer.AsSpan(0, framesRead * channels));
}
else
{
// If there is no data, do not spin the CPU unnecessarily
Thread.Sleep(1);
}
}
}, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
}
// Cleanup
cts?.Cancel();
processingTask?.Wait(TimeSpan.FromSeconds(3));
inputSource?.Stop();
inputSource?.Dispose();
cts?.Dispose(); |
Hi!
Own processing thread (if you don't need playback, just processing)
Don't add the inputSource to the mixer — call ReadSamples() in a dedicated thread: