Xeno-rat
Loading...
Searching...
No Matches
NAudio.Wave.DirectSoundOut Class Reference

NativeDirectSoundOut using DirectSound COM interop. Contact author: Alexandre Mutel - alexandre_mutel at yahoo.fr Modified by: Graham "Gee" Plumb. More...

Inheritance diagram for NAudio.Wave.DirectSoundOut:
Collaboration diagram for NAudio.Wave.DirectSoundOut:

Classes

class  BufferCaps
 
class  BufferDescription
 
struct  DirectSoundBufferPositionNotify
 
interface  IDirectSound
 IDirectSound interface.
 
interface  IDirectSoundBuffer
 IDirectSoundBuffer interface.
 
interface  IDirectSoundNotify
 IDirectSoundNotify interface.
 

Public Member Functions

 DirectSoundOut ()
 Initializes a new instance of the DirectSoundOut class.
 
 DirectSoundOut (Guid device)
 Initializes a new instance of the DirectSoundOut class.
 
 DirectSoundOut (int latency)
 Initializes a new instance of the DirectSoundOut class.
 
 DirectSoundOut (Guid device, int latency)
 Initializes a new instance of the DirectSoundOut class. (40ms seems to work under Vista).
 
void Play ()
 Starts playing the audio. If the playback state is stopped, it creates a new thread to process samples and starts playing.
 
void Stop ()
 Stops the playback.
 
void Pause ()
 Pauses the playback.
 
long GetPosition ()
 Retrieves the current playback position in bytes.
 
void Init (IWaveProvider waveProvider)
 Initializes the wave stream and wave format using the provided waveProvider .
 
void Dispose ()
 Stops the object and releases resources, and suppresses the finalization of the object.
 

Static Public Attributes

static readonly Guid DSDEVID_DefaultPlayback = new Guid("DEF00000-9C6D-47ED-AAF1-4DDA8F2B5C03")
 DirectSound default playback device GUID.
 
static readonly Guid DSDEVID_DefaultCapture = new Guid("DEF00001-9C6D-47ED-AAF1-4DDA8F2B5C03")
 DirectSound default capture device GUID.
 
static readonly Guid DSDEVID_DefaultVoicePlayback = new Guid("DEF00002-9C6D-47ED-AAF1-4DDA8F2B5C03")
 DirectSound default device for voice playback.
 
static readonly Guid DSDEVID_DefaultVoiceCapture = new Guid("DEF00003-9C6D-47ED-AAF1-4DDA8F2B5C03")
 DirectSound default device for voice capture.
 

Properties

static IEnumerable< DirectSoundDeviceInfoDevices [get]
 Gets the DirectSound output devices in the system.
 
TimeSpan PlaybackPosition [get]
 Gets the current position from the wave output device.
 
PlaybackState PlaybackState [get]
 Current playback state.
 
float Volume [get, set]
 The volume 1.0 is full scale.
 
WaveFormat OutputWaveFormat [get]
 The WaveFormat this device is using for playback.
 
- Properties inherited from NAudio.Wave.IWavePlayer

Events

EventHandler< StoppedEventArgsPlaybackStopped
 Playback Stopped.
 
- Events inherited from NAudio.Wave.IWavePlayer
EventHandler< StoppedEventArgsPlaybackStopped
 Indicates that playback has gone into a stopped state due to reaching the end of the input stream or an error has been encountered during playback.
 

Private Member Functions

 ~DirectSoundOut ()
 Releases unmanaged resources and performs other cleanup operations before the DirectSoundOut is reclaimed by garbage collection.
 
void InitializeDirectSound ()
 Initializes the DirectSound and creates primary and secondary sound buffers for audio playback.
 
bool IsBufferLost ()
 Checks if the secondary buffer is lost and returns a boolean value indicating the result.
 
int MsToBytes (int ms)
 Converts milliseconds to bytes based on the average bytes per second of the wave format.
 
void PlaybackThreadFunc ()
 This method represents the playback thread function. It initializes the DirectSound, feeds the audio samples, and handles the playback state.
 
void RaisePlaybackStopped (Exception e)
 Raises the PlaybackStopped event with the specified exception.
 
void StopPlayback ()
 Stops the playback of the sound.
 
void CleanUpSecondaryBuffer ()
 Cleans up the secondary buffer by filling it with silence data.
 
int Feed (int bytesToCopy)
 Reads data from the wave stream and copies it to the secondary buffer. If the buffer is lost, it is restored. If in Paused state, the bufferSamples are cleared.
 
static void DirectSoundCreate (ref Guid GUID, [Out, MarshalAs(UnmanagedType.Interface)] out IDirectSound directSound, IntPtr pUnkOuter)
 Creates a DirectSound object that represents the default sound device.
 
delegate bool DSEnumCallback (IntPtr lpGuid, IntPtr lpcstrDescription, IntPtr lpcstrModule, IntPtr lpContext)
 The DSEnumCallback function is an application-defined callback function that enumerates the DirectSound drivers. The system calls this function in response to the application's call to the DirectSoundEnumerate or DirectSoundCaptureEnumerate function.
 
static void DirectSoundEnumerate (DSEnumCallback lpDSEnumCallback, IntPtr lpContext)
 Enumerates the DirectSound drivers installed on the system and calls the specified callback function for each driver found.
 
static IntPtr GetDesktopWindow ()
 Retrieves a handle to the desktop window. The desktop window covers the entire screen. The desktop window is the area on top of which other windows are painted.
 

Static Private Member Functions

static bool EnumCallback (IntPtr lpGuid, IntPtr lpcstrDescription, IntPtr lpcstrModule, IntPtr lpContext)
 Callback function for enumerating DirectSound devices.
 

Private Attributes

PlaybackState playbackState
 
WaveFormat waveFormat
 
int samplesTotalSize
 
int samplesFrameSize
 
int nextSamplesWriteIndex
 
int desiredLatency
 
Guid device
 
byte[] samples
 
IWaveProvider waveStream = null
 
IDirectSound directSound = null
 
IDirectSoundBuffer primarySoundBuffer = null
 
IDirectSoundBuffer secondaryBuffer = null
 
EventWaitHandle frameEventWaitHandle1
 
EventWaitHandle frameEventWaitHandle2
 
EventWaitHandle endEventWaitHandle
 
Thread notifyThread
 
SynchronizationContext syncContext
 
long bytesPlayed
 
Object m_LockObject = new Object()
 

Static Private Attributes

static List< DirectSoundDeviceInfodevices
 

Detailed Description

NativeDirectSoundOut using DirectSound COM interop. Contact author: Alexandre Mutel - alexandre_mutel at yahoo.fr Modified by: Graham "Gee" Plumb.

Constructor & Destructor Documentation

◆ DirectSoundOut() [1/4]

NAudio.Wave.DirectSoundOut.DirectSoundOut ( )
inline

Initializes a new instance of the DirectSoundOut class.

◆ DirectSoundOut() [2/4]

NAudio.Wave.DirectSoundOut.DirectSoundOut ( Guid device)
inline

Initializes a new instance of the DirectSoundOut class.

◆ DirectSoundOut() [3/4]

NAudio.Wave.DirectSoundOut.DirectSoundOut ( int latency)
inline

Initializes a new instance of the DirectSoundOut class.

◆ DirectSoundOut() [4/4]

NAudio.Wave.DirectSoundOut.DirectSoundOut ( Guid device,
int latency )
inline

Initializes a new instance of the DirectSoundOut class. (40ms seems to work under Vista).

Parameters
latencyThe latency.
deviceSelected device

◆ ~DirectSoundOut()

NAudio.Wave.DirectSoundOut.~DirectSoundOut ( )
inlineprivate

Releases unmanaged resources and performs other cleanup operations before the DirectSoundOut is reclaimed by garbage collection.

Here is the call graph for this function:

Member Function Documentation

◆ CleanUpSecondaryBuffer()

void NAudio.Wave.DirectSoundOut.CleanUpSecondaryBuffer ( )
inlineprivate

Cleans up the secondary buffer by filling it with silence data.

If the secondary buffer is not null, this method locks the buffer, fills it with silence data, and then unlocks it. The silence data is created as an array of bytes with a size equal to the total samples size of the buffer. The method first locks the secondary buffer to access its memory, then copies the silence data into the buffer, and finally unlocks the buffer.

Here is the caller graph for this function:

◆ DirectSoundCreate()

static void NAudio.Wave.DirectSoundOut.DirectSoundCreate ( ref Guid GUID,
[Out, MarshalAs(UnmanagedType.Interface)] out IDirectSound directSound,
IntPtr pUnkOuter )
private

Creates a DirectSound object that represents the default sound device.

Parameters
GUIDThe GUID of the sound device.
directSoundWhen this method returns, contains the IDirectSound interface for the created sound device.
pUnkOuterReserved for future use. Must be null.
Exceptions
ArgumentExceptionThrown when the GUID is not a valid GUID.
ExternalExceptionThrown when an error occurred while creating the DirectSound object.
Here is the caller graph for this function:

◆ DirectSoundEnumerate()

static void NAudio.Wave.DirectSoundOut.DirectSoundEnumerate ( DSEnumCallback lpDSEnumCallback,
IntPtr lpContext )
private

Enumerates the DirectSound drivers installed on the system and calls the specified callback function for each driver found.

Parameters
lpDSEnumCallbackA pointer to the callback function that will be called for each DirectSound driver found.
lpContextAn application-defined value that will be passed to the callback function.

This method calls the specified callback function lpDSEnumCallback for each DirectSound driver found on the system. The lpContext parameter is an application-defined value that will be passed to the callback function.

◆ Dispose()

void NAudio.Wave.DirectSoundOut.Dispose ( )
inline

Stops the object and releases resources, and suppresses the finalization of the object.

This method stops the object and releases any resources it is using. It also suppresses the finalization of the object, preventing the finalizer from being called.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DSEnumCallback()

delegate bool NAudio.Wave.DirectSoundOut.DSEnumCallback ( IntPtr lpGuid,
IntPtr lpcstrDescription,
IntPtr lpcstrModule,
IntPtr lpContext )
private

The DSEnumCallback function is an application-defined callback function that enumerates the DirectSound drivers. The system calls this function in response to the application's call to the DirectSoundEnumerate or DirectSoundCaptureEnumerate function.

Parameters
lpGuidAddress of the GUID that identifies the device being enumerated, or NULL for the primary device. This value can be passed to the DirectSoundCreate8 or DirectSoundCaptureCreate8 function to create a device object for that driver.
lpcstrDescriptionAddress of a null-terminated string that provides a textual description of the DirectSound device.
lpcstrModuleAddress of a null-terminated string that specifies the module name of the DirectSound driver corresponding to this device.
lpContextAddress of application-defined data. This is the pointer passed to DirectSoundEnumerate or DirectSoundCaptureEnumerate as the lpContext parameter.
Returns
Returns TRUE to continue enumerating drivers, or FALSE to stop.

◆ EnumCallback()

static bool NAudio.Wave.DirectSoundOut.EnumCallback ( IntPtr lpGuid,
IntPtr lpcstrDescription,
IntPtr lpcstrModule,
IntPtr lpContext )
inlinestaticprivate

Callback function for enumerating DirectSound devices.

Parameters
lpGuidA pointer to the GUID of the enumerated device.
lpcstrDescriptionA pointer to the description of the enumerated device.
lpcstrModuleA pointer to the module name of the enumerated device, or null if not available.
lpContextA pointer to application-defined data.
Returns
True if the enumeration should continue; otherwise, false.

This callback function is used for enumerating DirectSound devices. It creates a new DirectSoundDeviceInfo object, populates it with the provided information, and adds it to the devices collection. If the GUID pointer is IntPtr.Zero, the device's GUID is set to Guid.Empty; otherwise, it is constructed from the provided bytes. The description and module name are obtained from the provided pointers and assigned to the device object. The function returns true to continue the enumeration.

◆ Feed()

int NAudio.Wave.DirectSoundOut.Feed ( int bytesToCopy)
inlineprivate

Reads data from the wave stream and copies it to the secondary buffer. If the buffer is lost, it is restored. If in Paused state, the bufferSamples are cleared.

Parameters
bytesToCopyThe number of bytes to copy from the wave stream to the secondary buffer.
Returns
The number of bytes actually read from the wave stream and copied to the secondary buffer.

This method first checks if the buffer is lost and restores it if necessary. If in Paused state, it clears the bufferSamples. It then reads data from the wave stream and copies it to the secondary buffer. The method locks a portion of the SecondaryBuffer and copies the data back to it. Finally, it unlocks the SecondaryBuffer and returns the number of bytes read from the wave stream.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDesktopWindow()

static IntPtr NAudio.Wave.DirectSoundOut.GetDesktopWindow ( )
private

Retrieves a handle to the desktop window. The desktop window covers the entire screen. The desktop window is the area on top of which other windows are painted.

Returns
A handle to the desktop window.
Here is the caller graph for this function:

◆ GetPosition()

long NAudio.Wave.DirectSoundOut.GetPosition ( )
inline

Retrieves the current playback position in bytes.

Returns
The current playback position in bytes. Returns 0 if the playback state is stopped or if the secondary buffer is null.

This method retrieves the current play cursor and write cursor from the secondary buffer and calculates the current playback position by adding the bytes played to the play cursor. If the playback state is stopped or if the secondary buffer is null, the method returns 0.

◆ Init()

void NAudio.Wave.DirectSoundOut.Init ( IWaveProvider waveProvider)
inline

Initializes the wave stream and wave format using the provided waveProvider .

Parameters
waveProviderThe wave provider to be initialized.

This method initializes the wave stream and wave format using the provided waveProvider .

Implements NAudio.Wave.IWavePlayer.

◆ InitializeDirectSound()

void NAudio.Wave.DirectSoundOut.InitializeDirectSound ( )
inlineprivate

Initializes the DirectSound and creates primary and secondary sound buffers for audio playback.

This method initializes the DirectSound by creating primary and secondary sound buffers for audio playback. It sets the cooperative level to PRIORITY and creates the primary sound buffer for looping playback. It also creates the secondary sound buffer with double buffering notification for audio playback.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsBufferLost()

bool NAudio.Wave.DirectSoundOut.IsBufferLost ( )
inlineprivate

Checks if the secondary buffer is lost and returns a boolean value indicating the result.

Returns
True if the secondary buffer is lost; otherwise, false.
Here is the caller graph for this function:

◆ MsToBytes()

int NAudio.Wave.DirectSoundOut.MsToBytes ( int ms)
inlineprivate

Converts milliseconds to bytes based on the average bytes per second of the wave format.

Parameters
msThe time in milliseconds to be converted to bytes.
Returns
The equivalent number of bytes for the given time in milliseconds, based on the wave format's average bytes per second.
Here is the caller graph for this function:

◆ Pause()

void NAudio.Wave.DirectSoundOut.Pause ( )
inline

Pauses the playback.

This method pauses the playback by setting the playbackState to PlaybackState.Paused.

Implements NAudio.Wave.IWavePlayer.

◆ Play()

void NAudio.Wave.DirectSoundOut.Play ( )
inline

Starts playing the audio. If the playback state is stopped, it creates a new thread to process samples and starts playing.

Implements NAudio.Wave.IWavePlayer.

Here is the call graph for this function:

◆ PlaybackThreadFunc()

void NAudio.Wave.DirectSoundOut.PlaybackThreadFunc ( )
inlineprivate

This method represents the playback thread function. It initializes the DirectSound, feeds the audio samples, and handles the playback state.

Exceptions
ExceptionThrown when there is an error during the playback process, such as a DirectSound buffer timeout.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RaisePlaybackStopped()

void NAudio.Wave.DirectSoundOut.RaisePlaybackStopped ( Exception e)
inlineprivate

Raises the PlaybackStopped event with the specified exception.

Parameters
eThe exception that caused the playback to stop.

This method raises the PlaybackStopped event with the specified exception. If a synchronization context is available, the event is raised on the synchronization context; otherwise, it is raised on the current thread.

Exceptions
ArgumentNullExceptionThrown when the specified exception is null.
Here is the caller graph for this function:

◆ Stop()

void NAudio.Wave.DirectSoundOut.Stop ( )
inline

Stops the playback.

This method attempts to stop the playback by acquiring a lock on the m_LockObject for 50 milliseconds using Monitor.TryEnter(object, int). If successful, it sets the playbackState to PlaybackState.Stopped and releases the lock. If unsuccessful, it aborts the notifyThread if it exists.

Implements NAudio.Wave.IWavePlayer.

Here is the caller graph for this function:

◆ StopPlayback()

void NAudio.Wave.DirectSoundOut.StopPlayback ( )
inlineprivate

Stops the playback of the sound.

This method stops the playback of the sound by releasing the secondary buffer, stopping the secondary buffer, releasing the primary sound buffer, and releasing the DirectSound object.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ bytesPlayed

long NAudio.Wave.DirectSoundOut.bytesPlayed
private

◆ desiredLatency

int NAudio.Wave.DirectSoundOut.desiredLatency
private

◆ device

Guid NAudio.Wave.DirectSoundOut.device
private

◆ devices

List<DirectSoundDeviceInfo> NAudio.Wave.DirectSoundOut.devices
staticprivate

◆ directSound

IDirectSound NAudio.Wave.DirectSoundOut.directSound = null
private

◆ DSDEVID_DefaultCapture

readonly Guid NAudio.Wave.DirectSoundOut.DSDEVID_DefaultCapture = new Guid("DEF00001-9C6D-47ED-AAF1-4DDA8F2B5C03")
static

DirectSound default capture device GUID.

◆ DSDEVID_DefaultPlayback

readonly Guid NAudio.Wave.DirectSoundOut.DSDEVID_DefaultPlayback = new Guid("DEF00000-9C6D-47ED-AAF1-4DDA8F2B5C03")
static

DirectSound default playback device GUID.

◆ DSDEVID_DefaultVoiceCapture

readonly Guid NAudio.Wave.DirectSoundOut.DSDEVID_DefaultVoiceCapture = new Guid("DEF00003-9C6D-47ED-AAF1-4DDA8F2B5C03")
static

DirectSound default device for voice capture.

◆ DSDEVID_DefaultVoicePlayback

readonly Guid NAudio.Wave.DirectSoundOut.DSDEVID_DefaultVoicePlayback = new Guid("DEF00002-9C6D-47ED-AAF1-4DDA8F2B5C03")
static

DirectSound default device for voice playback.

◆ endEventWaitHandle

EventWaitHandle NAudio.Wave.DirectSoundOut.endEventWaitHandle
private

◆ frameEventWaitHandle1

EventWaitHandle NAudio.Wave.DirectSoundOut.frameEventWaitHandle1
private

◆ frameEventWaitHandle2

EventWaitHandle NAudio.Wave.DirectSoundOut.frameEventWaitHandle2
private

◆ m_LockObject

Object NAudio.Wave.DirectSoundOut.m_LockObject = new Object()
private

◆ nextSamplesWriteIndex

int NAudio.Wave.DirectSoundOut.nextSamplesWriteIndex
private

◆ notifyThread

Thread NAudio.Wave.DirectSoundOut.notifyThread
private

◆ playbackState

PlaybackState NAudio.Wave.DirectSoundOut.playbackState
private

◆ primarySoundBuffer

IDirectSoundBuffer NAudio.Wave.DirectSoundOut.primarySoundBuffer = null
private

◆ samples

byte [] NAudio.Wave.DirectSoundOut.samples
private

◆ samplesFrameSize

int NAudio.Wave.DirectSoundOut.samplesFrameSize
private

◆ samplesTotalSize

int NAudio.Wave.DirectSoundOut.samplesTotalSize
private

◆ secondaryBuffer

IDirectSoundBuffer NAudio.Wave.DirectSoundOut.secondaryBuffer = null
private

◆ syncContext

SynchronizationContext NAudio.Wave.DirectSoundOut.syncContext
private

◆ waveFormat

WaveFormat NAudio.Wave.DirectSoundOut.waveFormat
private

◆ waveStream

IWaveProvider NAudio.Wave.DirectSoundOut.waveStream = null
private

Property Documentation

◆ Devices

IEnumerable<DirectSoundDeviceInfo> NAudio.Wave.DirectSoundOut.Devices
staticget

Gets the DirectSound output devices in the system.

◆ OutputWaveFormat

WaveFormat NAudio.Wave.DirectSoundOut.OutputWaveFormat
get

The WaveFormat this device is using for playback.

Implements NAudio.Wave.IWavePlayer.

◆ PlaybackPosition

TimeSpan NAudio.Wave.DirectSoundOut.PlaybackPosition
get

Gets the current position from the wave output device.

◆ PlaybackState

PlaybackState NAudio.Wave.DirectSoundOut.PlaybackState
get

Current playback state.

Implements NAudio.Wave.IWavePlayer.

◆ Volume

float NAudio.Wave.DirectSoundOut.Volume
getset

The volume 1.0 is full scale.

Implements NAudio.Wave.IWavePlayer.

Event Documentation

◆ PlaybackStopped

EventHandler<StoppedEventArgs> NAudio.Wave.DirectSoundOut.PlaybackStopped

Playback Stopped.


The documentation for this class was generated from the following file: