Im new to using Wasapi in Naudio and Im having n issue with the sound quality. About 1/10 times the audio will sound perfect when I record and the other 9 times it will be fuzzy. I was wondering if there is any reason for this.
Here is my code i'm using to record the audio:
public void CaptureAudio(String Name)
{
capture = new WasapiLoopbackCapture();
capture.Initialize();
w = new WaveWriter(Name, capture.WaveFormat);
capture.DataAvailable += (s, capData) =>
{
w.Write(capData.Data, capData.Offset, capData.ByteCount);
};
capture.Start();
}
public void StartRecording(String Name)
{
new Thread(delegate(){CaptureAudio(Name); }).Start();
}
public void StopCapture()
{
capture.Stop();
capture.Dispose();
w.Dispose();
}
First of all. As Mark already said, your code does not look like NAudio. It looks like CSCore. If you are using CSCore please create a new console application and copy paste the following code (I've modified your code). I just tried out that code on three different systems without any bugs and all 20 files were ok without beeing fuzzy.
private static void Main(string[] args)
{
for (int i = 0; i < 20; i++)
{
Console.WriteLine(i);
Capture(i);
}
}
private static void Capture(int index)
{
string Name = String.Format("dump-{0}.wav", index);
using (WasapiCapture capture = new WasapiLoopbackCapture())
{
capture.Initialize();
using (var w = new WaveWriter(Name, capture.WaveFormat))
{
capture.DataAvailable += (s, capData) => w.Write(capData.Data, capData.Offset, capData.ByteCount);
capture.Start();
Thread.Sleep(10000);
capture.Stop();
}
}
}
The problem turned out to be xbox music or windows media player running in the background, apparently they hog all the sound cards resources.
A few comments on your code:
First, have you modified WasapiLoopbackCapture in some way? The WaveInEventArgs on DataAvailable does not have the properties shown in your code. I'd expect you have some kind of block alignment error going on, so that your fuzzy sound is not reading on exact sample boundaries. Also NAudio does not have a class called WaveWriter - it's WaveFileWriter. Are you sure you are using NAudio?
Second, there is no need to start a new thread in StartRecording. WasapiLoopbackCapture will be using a background thread already.
Related
Problem:
As a part of school project, I attempt to build an application that provides a guitar AMP using the NAudio library.
When i plug in the guitar it recognizes it, and everything is working properly, but there is a huge delay between the input and the output from the speakers.
Here is my source code:
private void button2_Click(object sender, EventArgs e)
{
if (sourceList.SelectedItems.Count == 0) return;
int deviceNumber = sourceList.SelectedItems[0].Index;
sourceStream = new WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
sourceStream.StartRecording();
WaveInProvider waveIn = new WaveInProvider(sourceStream);
waveOut = new DirectSoundOut();
waveOut.Init(waveIn);
waveOut.Play();
}
in this code I catch an event of a button click that uses the selected input (microphone/guitar) and converts the sound it recieves to output.
The delay between the input and the output is around ~1sec and it's a deal breaker.
How do I improve the delay, to make the application more responsive?
DirectSoundOut and WaveIn are not particularly low-latency audio APIs. For something like this, ASIO is preferable. AsioOut is unfortunately a bit more complicated to work with, but it should allow you to get much lower latencies.
I've been working with a bit of speech recognition for a few days with various test programs and it's all worked fine. However i've tried implementing it into my OpenGL project and the function 'Recognized' is now not being called.
Up in the Windows Speech Recognition thing (the thing that says "try saying 'Start Listening'" an awful lot), words that are loaded appear when i say them, so I am assuming that it is correctly detecting words, it's just for some reason not triggering the event.
Here's the code i've been using. All you really need to know (besides what is shown in the code), is that AddCommands is called somewhere else, to add in a few words that i've been testing with and that 'Initiate' is called upon the loading of the form.
public class SpeechControls
{
public static SpeechRecognizer sRecognizer;
private static Dictionary<string, IVoiceControlable> controllers = new Dictionary<string, IVoiceControlable>();
public static void Initiate()
{
sRecognizer = new SpeechRecognizer();
sRecognizer.Enabled = true;
sRecognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Recognized);
}
private static void Recognized(object obj, SpeechRecognizedEventArgs args)
{
controllers[args.Result.Text].TriggerCommand(args.Result.Text);
}
public static void AddCommands(string[] commands, IVoiceControlable control)
{
foreach (string str in commands)
{
controllers.Add(str, control);
}
sRecognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(commands))));
}
}
Does anyone know why 'Recognized' would not be triggered?
Thanks for any help, much appreciated.
Because OpenGL runs of game loops rather than event listening, the thread is completely taken up by the loop. To start listening for commands a second thread is needed.
I am trying to read weight from digital scale in c# app, found this question
this is exactly what I am trying to do
but for me below function never runs.
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}
I have checked the scale in device manager, its location is set to Port_#0004.Hub_#0003 and appear to be working fine
I was not sure about port number of the scale so I did
var test = SerialPort.GetPortNames();
and only COM1 gets returned
Edit 1: When I do int a = port.ReadByte(); my application hangs and execution never moves forward from this statement.
I faced a problem like this and I solved it changing a COM configuration (Configuration > Comunication > Conections) to SICS in the device. I don't know your scale model but maybe my code can help. [Reading data from Mettler Toledo (IND560) scale device using C#]
Could you try polling instead of using the DataReceived event? Also, I believe the DataReceived event has a threshold before it fires, you might want to look into that too.
Are you able to get the serial number from the balance? This should be the first thing you do when connecting. It will let you verify the connection is established. If you are having trouble connecting through a C# interface try using HyperTerminal first. You can vary a lot of setting really quickly and dial in on the right ones to use. Although the balance should be able to use a wide variety of baud rates and stop bits and such. They are usually pretty adaptable. But do try HyperTerminal.
I'm looking for the pdf but there is a very long list of available commands (depending on your model). The pdf is like 130 pages long. Have you read this?
Please see this post, I used Mike library to connect.
using System;
using System.Linq;
using System.Text;
using HidLibrary;
namespace MagtekCardReader
{
class Program
{
private const int VendorId = 0x0801;
private const int ProductId = 0x0002;
private static HidDevice _device;
static void Main()
{
_device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();
if (_device != null)
{
_device.OpenDevice();
_device.Inserted += DeviceAttachedHandler;
_device.Removed += DeviceRemovedHandler;
_device.MonitorDeviceEvents = true;
_device.ReadReport(OnReport);
Console.WriteLine("Reader found, press any key to exit.");
Console.ReadKey();
_device.CloseDevice();
}
else
{
Console.WriteLine("Could not find reader.");
Console.ReadKey();
}
}
private static void OnReport(HidReport report)
{
if (!_device.IsConnected) { return; }
var cardData = new Data(report.Data);
Console.WriteLine(!cardData.Error ? Encoding.ASCII.GetString(cardData.CardData) : cardData.ErrorMessage);
_device.ReadReport(OnReport);
}
private static void DeviceAttachedHandler()
{
Console.WriteLine("Device attached.");
_device.ReadReport(OnReport);
}
private static void DeviceRemovedHandler()
{
Console.WriteLine("Device removed.");
}
}
}
In my (Silverlight) weather app I am downloading up to 6 seperate weather radar images (each one taken about 20 mins apart) from a web site and what I need to do is display each image for a second then at the end of the loop, pause 2 seconds then start the loop again. (This means the loop of images will play until the user clicks the back or home button which is what I want.)
So, I have a RadarImage class as follows, and each image is getting downloaded (via WebClient) and then loaded into a instance of RadarImage which is then added to a collection (ie: List<RadarImage>)...
//Following code is in my radar.xaml.cs to download the images....
int imagesToDownload = 6;
int imagesDownloaded = 0;
RadarImage rdr = new RadarImage(<image url>); //this happens in a loop of image URLs
rdr.FileCompleteEvent += ImageDownloadedEventHandler;
//This code in a class library.
public class RadarImage
{
public int ImageIndex;
public string ImageURL;
public DateTime ImageTime;
public Boolean Downloaded;
public BitmapImage Bitmap;
private WebClient client;
public delegate void FileCompleteHandler(object sender);
public event FileCompleteHandler FileCompleteEvent;
public RadarImage(int index, string imageURL)
{
this.ImageIndex = index;
this.ImageURL = imageURL;
//...other code here to load in datetime properties etc...
client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
client.OpenReadAsync(new Uri(this.ImageURL, UriKind.Absolute));
}
private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
StreamResourceInfo sri = new StreamResourceInfo(e.Result as Stream, null);
this.Bitmap = new BitmapImage();
this.Bitmap.SetSource(sri.Stream);
this.Downloaded = true;
FileCompleteEvent(this); //Fire the event to let the app page know to add it to it's List<RadarImage> collection
}
}
}
As you can see, in the class above I have exposed an event handler to let my app page know when each image has downloaded. When they have all downloaded I then run the following code in my xaml page - but only the last image ever shows up and I can't work out why!
private void ImageDownloadedEventHandler(object sender)
{
imagesDownloaded++;
if (imagesDownloaded == imagesToDownload)
{
AllImagesDownloaded = true;
DisplayRadarImages();
}
}
private void DisplayRadarImages()
{
TimerSingleton.Timer.Stop();
foreach (RadarImage img in radarImages)
{
imgRadar.Source = img.Bitmap;
Thread.Sleep(1000);
}
TimerSingleton.Timer.Start(); //Tick poroperty is set to 2000 milliseconds
}
private void SingleTimer_Tick(object sender, EventArgs e)
{
DisplayRadarImages();
}
So you can see that I have a static instance of a timer class which is stopped (if running), then the loop should show each image for a second. When all 6 have been displayed then it pauses, the timer starts and after two seconds DisplayRadarImages() gets called again.
But as I said before, I can only ever get the last image to show for some reason and I can't seem to get this working properly.
I'm fairly new to WP7 development (though not to .Net) so just wondering how best to do this - I was thinking of trying this with a web browser control but surely there must be a more elegant way to loop through a bunch of images!
Sorry this is so long but any help or suggestions would be really appreciated.
Mike
You can use a background thread with either a Timer or Sleep to periodically update your image control.
Phạm Tiểu Giao - Threads in WP7
You'll need to dispatch updates to the UI with
Dispatcher.BeginInvoke( () => { /* your UI code */ } );
Why don't you add the last image twice to radarImages, set the Timer to 1000 and display just one image on each tick?
I'm trying to record audio data from a microphone (or line-in), and then replay it again, using C#.
Any suggestions on how I can achieve this?
See Console and multithreaded recording and playback
class Program
{
static void Main(string[] args)
{
rex.Data += new RecorderEx.DataEventHandler(rex_Data);
rex.Open += new EventHandler(rex_Open);
rex.Close += new EventHandler(rex_Close);
rex.Format = pcmFormat;
rex.StartRecord();
Console.WriteLine("Please press enter to exit!");
Console.ReadLine();
rex.StopRecord();
}
static RecorderEx rex = new RecorderEx(true);
static PlayerEx play = new PlayerEx(true);
static IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 44100);
static void rex_Open(object sender, EventArgs e)
{
play.OpenPlayer(pcmFormat);
play.StartPlay();
}
static void rex_Close(object sender, EventArgs e)
{
play.ClosePlayer();
}
static void rex_Data(object sender, DataEventArgs e)
{
byte[] data = e.Data;
play.AddData(data);
}
}
A live link of NAudio.
https://github.com/naudio/NAudio
It's available as a NuGet Package
Information about Output devices:
https://github.com/naudio/NAudio/blob/master/Docs/OutputDeviceTypes.md
Keep in mind for performance the following from the FAQ:
"Is .NET Performance Good Enough for Audio?
While .NET cannot compete with unmanaged languages for very low latency audio work, it still performs better than many people would expect. On a fairly modest PC, you can quite easily mix multiple WAV files together, including pass them through various effects and codecs, play back glitch free with a latency of around 50ms."