Enable Microphone using NAudio c# - c#

I have this code that uses NAudio to look for all microphones connected on laptop, andwhen I choose one it shows a meter of the sound in a progressbar.
when I gather all device, I collect them in a dropdwon list, my issue is that when I choose the one microphone from the list it will not be activated and display the sound on a meter unless I start WINDOWS SOUND RECORDER which seems to activate the mic.
how to enable or activate the microphone from the code without starting WINDOWS SOUND RECORDER?
here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.CoreAudioApi;
using System.Threading;
using NAudio.Wave;
using NAudio.Mixer;
namespace NaudioVisualizer
{
public partial class Form1 : Form
{
public int a = 1;
public Form1()
{
InitializeComponent();
MMDeviceEnumerator de = new MMDeviceEnumerator();
var device = de.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active);
comboBox1.Items.AddRange(device.ToArray());
}
private void timer1_Tick(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
var device = (MMDevice)comboBox1.SelectedItem;
progressBar1.Value = (int)Math.Round(device.AudioMeterInformation.MasterPeakValue * 100);
}
}
}
}

I had the same problem try to use the following code :
var waveIn = new WaveIn();
waveIn.StartRecording();

Related

Program doesn't respond if I send data to dial-up modem

I have the problem that when I send data (AT-Commands) to my Dial-Up Modem my program doesnt respond any more and crashes. I use System.IO.Ports to make this work. This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace Windows_XP_AOL
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
SerialPort serialPort = new SerialPort("COM1" , 9600);
serialPort.ReadTimeout = 10000;
serialPort.WriteTimeout = 10000;
serialPort.Open();
serialPort.DtrEnable = true;
serialPort.WriteLine("ATA");
serialPort.ReadLine();
serialPort.Close();
}
}
}
On the modem the Send Data Light lights up and the Read Data Light. But when I click on my button that sends the AT-Command (HAYES) (serialPort.WriteLine("ATA")) then my program just doesn't respond anymore and crashes. Because of my timeout function, it doesn't crash but my modem doesn't execute the command that I'm sending. Does someone know what I am making wrong? I can try to explain with pictures if someone doesn't understand it. Please I need this to work! Thanks for any help :)

ProgressBar not starting, not going in foreach loop until window not closed

I work on an export plugin to export revit models to database.
To do this, my main window is calling ExportEngine which is a Window:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using UserConnect;
using System.Collections.ObjectModel;
using System.IO;
namespace UserConnect
{
public partial class ExportEngine : Window
{
private List<Component> components = new List<Component>();
private static String zoneName = "REVIT";
private User user;
private Server neo4j = new Server("172.16.1.104", 8000);
public ExportEngine(User userp)
{
user = userp;
InitializeComponent();
}
public void addComponent(Component c)
{
components.Add(c);
}
public bool save()
{
this.ShowDialog();
float progress = components.Count / 100;
foreach (Component c in components)
{
//Here is my export request, working good
pbStatus.Value+=progress;
}
this.Close();
return result;
}
}
}
I call this in Main :
db = new ExportEngine(user);
foreach(String name in mats)
{
Component c = new Component(name, "m2",1);
db.addComponent(c);
}
db.save();
I don't understand why my save() starts only after I close the progressbar Window, and this progressbar doesn't move at all (no progress).
save doesnt run until windows is closed
ShowDialog does not return until the window you are showing is closed. That explains why it does not run until the windows closes.
Progress bar doesn't update
Try this.
pbStatus.MaxValue = components.Count; //make sure you set the max value
foreach (Component c in components)
{
pbStatus.Value+= 1; //increment by one item as you have processed on item.
}

removing image reference from picturebox (C#)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyProject
{
public partial class MyForm : Form
{
Process MyProcess;
public MyForm()
{
InitializeComponent();
}
private void MyForm_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case 'a':
this.MyPictureBox.Image = null;
this.MyProcess = new Process();
this.MyProcess.StartInfo =
new ProcessStartInfo("\"C:\\Program Files (x86)\\LilyPond\\usr\\bin\\lilypond.exe\"", "--png tmp.ly");
this.MyProcess.Start();
this.MyProcess.WaitForExit();
this.MyPictureBox.Image = new Bitmap("tmp.png");
break;
default:
break;
}
}
}
}
"C:\Program Files (x86)\LilyPond\usr\bin\lilypond.exe" --png tmp.ly command creates tmp.png. When I press a key first time, MyProcess returns 0, but next - returns 1 always. I think the problem is in overwritting file tmp.png, which is using by MyPictureBox, but I have no idea how to repair it. Could you help me?
As far as I know lilypond does not give an option for overwriting using a command line argument. If I'm right then you can include code to delete then png file (if exsists) before MyProcess starts.
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}

Record input from NAudio WaveIn, and output to NAudio WaveOut

I want to be able to get input from a microphone device via NAudio.WaveIn, and then output that exact input to an output device via NAudio.WaveOut.
How would I do this?
Here is the code that worked for me:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.CoreAudioApi;
namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
private BufferedWaveProvider bwp;
WaveIn wi;
WaveOut wo;
public Form4()
{
InitializeComponent();
wo = new WaveOut();
wi = new WaveIn();
wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
bwp = new BufferedWaveProvider(wi.WaveFormat);
bwp.DiscardOnBufferOverflow = true;
wo.Init(bwp);
wi.StartRecording();
wo.Play();
}
void wi_DataAvailable(object sender, WaveInEventArgs e)
{
bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);
}
}
}
The best way would be to use a BufferedWaveProvider as the input to WaveOut. Then in the DataAvailable callback of WaveIn, supply the data recorded to the BufferedWaveProvider
void DataAvailable(object sender, WaveInEventArgs args)
{
bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded);
}
You need to be aware that the default buffer sizes will result in a noticeable delay, so if you were hoping for low latency you might need to experiment a bit with buffer sizes to see how low you can get it.

DirectX InvalidCallException unhandled

I am getting an unhandled exception when trying to create a D3d device.
This is literally the whole code in C#. I am just testing and trying to learn directx.
I am getting the Unhandled exception on this line
: device = new Device(0, DeviceType.Hardware, this,CreateFlags.SoftwareVertexProcessing, presentParams);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Device device = null; // Create rendering device
PresentParameters presentParams = new PresentParameters();
device = new Device(0, DeviceType.Hardware, this,CreateFlags.SoftwareVertexProcessing, presentParams);
RenderToSurface render = new RenderToSurface(device, 300, 300, Format.D32, true, DepthFormat.D32);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Kind of blowing my mind because it looks like all the params are right..
needed presentParams.Windowed = true in order for this to work.

Categories

Resources