make sure you subscribe to PlaybackStopped and check the Exception property
↧
New Post: No sound while playing an mp3 file from disk using WaveOut
↧
New Post: Modify current audio output stream (add effect)
I've been experementing with the WasapiLoopbackCapture and WasapiOut. All I think I need to do, is something like
Would appreciate that :)
loop
{
record for 100ms
play recorded input
}
Can you help me out with the object model of NAudio? It's complicated and I don't see through...Would appreciate that :)
↧
↧
New Post: Modify current audio output stream (add effect)
This is what I have so far, but it's only causing some sort of noise. I also don't see a way to control how often "DataAvailable" is called. I would rather like to record like 100ms and then play it at half volume to create the echo effect.
WasapiLoopbackCapture waveIn = new WasapiLoopbackCapture();
waveIn.DataAvailable += delegate(object sender, WaveInEventArgs e)
{
try
{
byte[] bytes;
using (MemoryStream mem = new MemoryStream())
{
using (WaveFileWriter file = new WaveFileWriter(mem, new WaveFormat(44000, 2)))
{
file.Write(e.Buffer, 0, e.BytesRecorded);
}
bytes = mem.ToArray();
}
WaveOut waveOut = new WaveOut();
waveOut.Init(new WaveFileReader(new MemoryStream(bytes)));
waveOut.Play();
}
catch (Exception ex)
{
}
};
waveIn.StartRecording();
Application.Run();
↧
New Post: Effect not working and behaving differently on different sound cards
I'm using the WasapiLoopbackCapture in combination with WaveOut in order to create an echo effect that is global, i.e. affects the current audio output.
However, my implementation, which seems pretty poor to me, doesn't work well at all. On every machine it sounds different, sometimes it even crashes.
This is the code I came up with. It records the audio output and every 75ms it plays the recorded stream using WaveOut.
What am I doing wrong here, how can this be improved?
However, my implementation, which seems pretty poor to me, doesn't work well at all. On every machine it sounds different, sometimes it even crashes.
This is the code I came up with. It records the audio output and every 75ms it plays the recorded stream using WaveOut.
What am I doing wrong here, how can this be improved?
private static void CreateReverb()
{
MemoryStream loopbackWave = new MemoryStream();
DateTime lastEcho = DateTime.Now;
WasapiLoopbackCapture waveIn = new WasapiLoopbackCapture();
waveIn.StartRecording();
waveIn.DataAvailable += delegate(object sender, WaveInEventArgs e)
{
loopbackWave.Write(e.Buffer, 0, e.BytesRecorded);
if ((DateTime.Now - lastEcho).TotalMilliseconds > 75)
{
lastEcho = DateTime.Now;
byte[] loopbackBytes = loopbackWave.ToArray();
loopbackWave.Dispose();
loopbackWave = new MemoryStream();
using (MemoryStream waveStream = new MemoryStream())
{
using (WaveFileWriter waveFileWriter = new WaveFileWriter(waveStream, waveIn.WaveFormat))
{
waveFileWriter.Write(loopbackBytes, 0, loopbackBytes.Length);
}
loopbackBytes = waveStream.ToArray();
}
for (int i = 0; i < 3; i++)
{
ThreadPool.QueueUserWorkItem(delegate
{
try
{
WaveOut waveOut = new WaveOut();
waveOut.Init(new WaveFileReader(new MemoryStream(loopbackBytes)));
waveOut.Volume = ReverbIntensity * .5f;
waveOut.Play();
Thread.Sleep(200);
waveOut.Dispose();
}
catch { }
});
}
}
};
}
↧
New Post: Spectrogram operations
Hello everyone,
I'm new around here and I'm trying to dig my way up to NAudio. I have a project were I need to find a file's spectrogram peaks and I'm a little bit lost. I thought about splitting my chore into two phases:
1) Making a spectrogram visualizer
2) Reading somehow information from a .wav file to find the peaks
Can you help me with this chores? Maybe if you could just lead me on how to read data from the .wav file in order to find the magnitude, frequency and time I could understand better how to accomplish my goal.
Thanks in advance!
I'm new around here and I'm trying to dig my way up to NAudio. I have a project were I need to find a file's spectrogram peaks and I'm a little bit lost. I thought about splitting my chore into two phases:
1) Making a spectrogram visualizer
2) Reading somehow information from a .wav file to find the peaks
Can you help me with this chores? Maybe if you could just lead me on how to read data from the .wav file in order to find the magnitude, frequency and time I could understand better how to accomplish my goal.
Thanks in advance!
↧
↧
Created Unassigned: MemoryAllocationError calling waveInOpen [16472]
Hello,
I use NAudio for professional project to communicate with radios. Randomly, the DLL crashes at start of the Micro capture.
Someone would have an idea?
Thank you in advance.
Here is my code:
Imports NAudio.Wave
Imports System.Runtime.InteropServices
Public Class NaudioMicro
Implements IDisposable
Protected Disposed As Boolean = False
#Region "Déclarations"
'Autorisation d'exporter le flux Audio
Private Transmit As Integer = 0
'Indique que le recorder est en mode capture
Private IsCapturing As Integer = 0
'Recorder Naudio
Private WithEvents _WaveInDevice As WaveIn
#End Region
#Region "Constructor"
''' <summary>
''' Simple New without Data
''' </summary>
''' <remarks>The selection of Peiker Microphone is automatic !</remarks>
Public Sub New()
Dim DeviceInfoI As WaveInCapabilities
Try
'1. Détection automatique du Micro Peiker
Dim DeviceNumber As Integer = -1
For I As Integer = 0 To WaveIn.DeviceCount - 1
DeviceInfoI = WaveIn.GetCapabilities(I)
If Val("&H" & Strings.Left(DeviceInfoI.ProductGuid.ToString, 2)) > 0 Then
If InStr(DeviceInfoI.ProductName, "PTC USB", CompareMethod.Text) > 0 Then
DeviceNumber = I
Exit For
End If
End If
Next
'2. Configuration du Recorder avec Micro Peiker
If DeviceNumber >= 0 Then
_WaveInDevice = New WaveIn()
_WaveInDevice.DeviceNumber = DeviceNumber
_WaveInDevice.WaveFormat = New NAudio.Wave.WaveFormat(8000, 16, 1)
_WaveInDevice.BufferMilliseconds = 60
End If
Catch Ex As Exception
Finally
IsCapturing = 0
End Try
End Sub
#End Region
#Region "Event"
Friend Event OnDeliverySpeechData(ByVal SpeechData() As Byte)
#End Region
#Region "Commandes"
''' <summary>
''' Start Recording
''' </summary>
''' <remarks></remarks>
Private Sub CaptureStart()
If _WaveInDevice Is Nothing Then
Return
End If
If IsCapturing = 0 Then
IsCapturing = 1
_WaveInDevice.StartRecording()
End If
End Sub
''' <summary>
''' Stop Recording
''' </summary>
''' <remarks></remarks>
Private Sub CaptureStop()
If _WaveInDevice Is Nothing Then
Return
End If
If IsCapturing = 1 Then
IsCapturing = 0
_WaveInDevice.StopRecording()
Transmit = 0
End If
End Sub
''' <summary>
''' Start transmission of audio stream
''' </summary>
''' <remarks></remarks>
Friend Sub StartCapture()
Transmit = 1
If IsCapturing = 0 Then
CaptureStart()
End If
End Sub
''' <summary>
''' Stop transmission of audio stream
''' </summary>
''' <remarks></remarks>
Friend Sub StopCapture()
Transmit = 0
If IsCapturing = 1 Then
CaptureStop()
End If
End Sub
#End Region
#Region "Processus_internes"
''' <summary>
''' Fonction CallBack de l'enregistreur
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub _WaveInDevice_DataAvailable(ByVal sender As Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles _WaveInDevice.DataAvailable
If Transmit = 1 Then
If e.BytesRecorded > 0 Then
If My.Settings.SendSpeechDirect = 1 Then
Dim AIOPCMFrame As New _D25AIOPCMFRAME
AIOPCMFrame.sz = e.Buffer.Length
AIOPCMFrame.m_aFrame = Marshal.AllocHGlobal(e.Buffer.Length)
Marshal.Copy(e.Buffer, 0, AIOPCMFrame.m_aFrame, e.Buffer.Length)
Call Appels.DiffusionMicro(AIOPCMFrame)
If LoopBack = 1 Then
If IHM.PlayBackJingleInProgress = 0 Then
If My.Settings.AudioEmbeded = 0 Then
Players.Player(0).PlayPacket(e.Buffer)
Else
LocalPlayer.PlayPacket(e.Buffer)
End If
End If
End If
Else
RaiseEvent OnDeliverySpeechData(e.Buffer)
End If
End If
End If
End Sub
#End Region
#Region "IDisposable Support"
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If Not Me.Disposed Then
If disposing Then
If _WaveInDevice IsNot Nothing Then
_WaveInDevice.StopRecording()
_WaveInDevice.Dispose()
_WaveInDevice = Nothing
End If
End If
End If
Me.Disposed = True
End Sub
' Do not change or add Overridable to thse methods.
' Put cleanup code in Dispose(Byval disposing as boolean).
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
End Class
↧
Reviewed: NAudio 1.7 (Mrz 10, 2015)
Rated 5 Stars (out of 5) - Great and well designed Audio library, which is meant to stream Audio through various ways. V1.7 supports the media foundation Decoder and can directly stream from Radio or direct http link! Although Video files can be read as Audio stream. Overall structure makes it possible to write own Readers for unsupported file types. Please keep it up Mark and one day it´s a masterpiece!!
↧
New Post: Supported formats for playing
I´d also recommend to use These well working Decoders with NAudio:
OGG Decoder: https://nvorbis.codeplex.com/
FLAC Decoder: http://code.google.com/p/naudio-flac/
OGG Decoder: https://nvorbis.codeplex.com/
FLAC Decoder: http://code.google.com/p/naudio-flac/
↧
New Post: Bug report: WaveFileReader - Argument out of range exception
No, I used the WaveFileWriter class provided at your site.
↧
↧
New Post: "StarRecording"
I developed around DLL NAudio a professional application and randomly, for some reason, when I run the command 'StartRecording', there is no data that arrives in a routine 'DataAvailable'! Someone would have an idea?
Thank you in advance
Thank you in advance
↧
New Post: "StarRecording"
what class are you using to record? WaveIn? WasapiIn?
↧
New Post: AsioOut ret ComException
Hello all, I am also having this problem with the ASIO4ALL v2 driver. Any help would be appreciated!
↧
New Post: AsioOut ret ComException
WorshipCookies, try to mark Main function as STAThread:
[STAThread]
static void Main(string[] args){
...
}
[STAThread]
static void Main(string[] args){
...
}
↧
↧
New Post: AsioOut ret ComException
Thanks Denchik! Solved the problem :).
↧
New Post: Different lengths of the same file (in .wav and .mp3)
Hi,
I need to make an operation on a file and its copy which requires the same lengths of these files.
I spotted, that when i save a file (using WaveFileWriter) to .wav format and then i create a copy of it in .mp3 format (i tried two third party converters) - .wav and .mp3 files have different lengths. I load and compare them using Length field of AudioFileReader.
Why is it different? How can i assure the length is the same (i.e. they are "the same" minus, of course, compression)?
Thanks in advance :)
I need to make an operation on a file and its copy which requires the same lengths of these files.
I spotted, that when i save a file (using WaveFileWriter) to .wav format and then i create a copy of it in .mp3 format (i tried two third party converters) - .wav and .mp3 files have different lengths. I load and compare them using Length field of AudioFileReader.
Why is it different? How can i assure the length is the same (i.e. they are "the same" minus, of course, compression)?
Thanks in advance :)
↧
New Post: "StarRecording"
I used WaveIn Without explore WasapiIn because WaveIn fits perfectly with what I do.
Here is my code :
Imports NAudio.Wave
Imports System.Runtime.InteropServices
Friend Class NaudioMicro
...
End Class
Here is my code :
Imports NAudio.Wave
Imports System.Runtime.InteropServices
Friend Class NaudioMicro
...
Region "Déclarations"
'Autorisation d'exporter le flux Audio
Private Transmit As Integer = 0
'Indique que le recorder est en mode capture
Private IsCapturing As Integer = 0
'Recorder Naudio
Private WithEvents _WaveInDevice As NAudio.Wave.WaveIn
End Region
Region "Constructor"
Public Sub New() Dim DeviceInfoI As WaveInCapabilities
Try
'1. Détection automatique du Micro Peiker
Dim DeviceNumber As Integer = -1
For I As Integer = 0 To WaveIn.DeviceCount - 1
DeviceInfoI = WaveIn.GetCapabilities(I)
If Val("&H" & Strings.Left(DeviceInfoI.ProductGuid.ToString, 2)) > 0 Then
If InStr(DeviceInfoI.ProductName, "PTC USB", CompareMethod.Text) > 0 Then
DeviceNumber = I
Exit For
End If
End If
Next
'2. Configuration du Recorder avec Micro Peiker If DeviceNumber >= 0 Then
_WaveInDevice = New WaveIn()
_WaveInDevice.DeviceNumber = DeviceNumber
_WaveInDevice.WaveFormat = New WaveFormat(8000, 16, 1)
_WaveInDevice.NumberOfBuffers = 2
_WaveInDevice.BufferMilliseconds = 60
End If
Catch Ex As Exception
End Try
End Sub
End Region
Region "Event"
Friend Event OnError(ByVal Source As String, ByVal Message As String)
Friend Event OnDeliverySpeechData(ByVal SpeechData() As Byte)
End Region
Region "Commandes"
Private Sub CaptureStart()
Try
If _WaveInDevice Is Nothing Then
Return
End If
If IsCapturing = 0 Then
IsCapturing = 1
_WaveInDevice.StartRecording()
End If
Catch Ex As Exception
RaiseEvent OnError("CaptureStart", Ex.Message)
End Try
End Sub
Private Sub CaptureStop()
If _WaveInDevice Is Nothing Then
Return
End If
If IsCapturing = 1 Then
IsCapturing = 0
_WaveInDevice.StopRecording()
Transmit = 0
End If
End Sub
Friend Sub StartCapture()
Transmit = 1
CaptureStart()
End Sub
Friend Sub StopCapture()
Transmit = 0
CaptureStop()
End Sub
End Region
Region "Processus_internes"
Private Sub DataAvailable(ByVal sender As Object, ByVal e As NAudio.Wave.WaveInEventArgs) Handles _WaveInDevice.DataAvailable
Try
If My.Settings.SendSpeechDirect = 1 Then
Dim AIOPCMFrame As New _D25AIOPCMFRAME
AIOPCMFrame.sz = e.Buffer.Length
AIOPCMFrame.m_aFrame = Marshal.AllocHGlobal(e.Buffer.Length)
Marshal.Copy(e.Buffer, 0, AIOPCMFrame.m_aFrame, e.Buffer.Length)
Call IHM.Appels.DiffusionMicro(AIOPCMFrame)
If IHM.LoopBack = 1 Then
If IHM.PlayBackJingleInProgress = 0 Then
If My.Settings.AudioEmbeded = 0 Then
IHM.Players.Player(0).PlayPacket(e.Buffer)
Else
IHM.LocalPlayer.PlayPacket(e.Buffer)
End If
End If
End If
Else
RaiseEvent OnDeliverySpeechData(e.Buffer)
End If
Catch Ex As Exception
End Try
End Sub
End Region
...End Class
↧
New Post: "StarRecording"
What sort of app are you using? WaveIn can be used with WinForms or WPF, but if it is a console app you'll need to go with WaveInEvent
↧
↧
New Post: Device Name Truncation with WaveOut.GetCapabilities
Hi, I want to share my solution in c# to match waveout device and MMDevice, but for Windows 7.
Use GetWaveOutEndpointId() to get Guid of the audio endpoint.
Use this guid to get MMDevice
mmdevice = (new MMDeviceEnumerator()).GetDevice(guid);
Regards.
Use GetWaveOutEndpointId() to get Guid of the audio endpoint.
Use this guid to get MMDevice
mmdevice = (new MMDeviceEnumerator()).GetDevice(guid);
Regards.
struct WaveOutDevice
{
public int WaveOutDeviceId;
public string FullName;
public string EndpointGuid;
}
int DRV_QUERYFUNCTIONINSTANCEID = (0x0800 + 17);
int DRV_QUERYFUNCTIONINSTANCEIDSIZE = (0x0800 + 18);
[DllImport("winmm.dll")]
static extern Int32 waveOutGetNumDevs();
[DllImport("winmm.dll")]
static extern Int32 waveOutMessage(IntPtr hWaveOut, int uMsg, out int dwParam1, IntPtr dwParam2);
[DllImport("winmm.dll")]
static extern Int32 waveOutMessage(IntPtr hWaveOut, int uMsg, IntPtr dwParam1, int dwParam2);
string GetWaveOutEndpointId(int devNumber)
{
int cbEndpointId;
string result = string.Empty;
waveOutMessage((IntPtr)devNumber, DRV_QUERYFUNCTIONINSTANCEIDSIZE, out cbEndpointId, IntPtr.Zero);
IntPtr strPtr = Marshal.AllocHGlobal(cbEndpointId);
waveOutMessage((IntPtr)devNumber, DRV_QUERYFUNCTIONINSTANCEID, strPtr, cbEndpointId);
result = Marshal.PtrToStringAuto(strPtr);
Marshal.FreeHGlobal(strPtr);
return result;
}
List<WaveOutDevice> GetWaveOutDevices()
{
List<WaveOutDevice> retVal = new List<WaveOutDevice>();
foreach (MMDevice d in (new NAudio.CoreAudioApi.MMDeviceEnumerator()).EnumerateAudioEndPoints(DataFlow.Render, DeviceState.All))
{
if (d.State != DeviceState.Active) continue;
WaveOutDevice di = new WaveOutDevice()
{
EndpointGuid = d.ID,
FullName = d.FriendlyName,
WaveOutDeviceId = -1
};
for (int waveOutIdx = 0; waveOutIdx < waveOutGetNumDevs(); waveOutIdx++)
{
string guid = GetWaveOutEndpointId(waveOutIdx);
if (guid == di.EndpointGuid)
{
di.WaveOutDeviceId = waveOutIdx;
break;
}
}
retVal.Add(di);
}
return retVal;
}
↧
New Post: Device Name Truncation with WaveOut.GetCapabilities
nice! thanks for sharing
↧
New Post: Device Name Truncation with WaveOut.GetCapabilities
You are welcome Mark! It would be nice to have GetWaveOutEndpointId in WaveOut class in NAudio :) It should be also similar for WaveIn
↧