Skip to content
This repository has been archived by the owner on Feb 19, 2021. It is now read-only.

Commit

Permalink
Custom Audio Output Device
Browse files Browse the repository at this point in the history
Added support for a custom output device.
  • Loading branch information
NeilSeligmann authored and Sammy Sammon committed Dec 21, 2017
1 parent f3c0e8d commit 51b4f78
Show file tree
Hide file tree
Showing 6 changed files with 707 additions and 478 deletions.
148 changes: 142 additions & 6 deletions Audio/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,32 @@

namespace ListenMoeClient
{
class AudioPlayer : IDisposable

/// <summary>
/// Audio Output Device Object
/// </summary>
public class AudioDevice
{
public int ID;
public string Name;

public AudioDevice(int _id, string _name)
{
this.ID = _id;
this.Name = _name;
}

public override string ToString()
{
return this.Name;
}

}

public class AudioPlayer : IDisposable
{
BufferedWaveProvider provider;
WaveOut waveOut = new WaveOut();
WaveOut waveOut;

Queue<short[]> samplesToPlay = new Queue<short[]>();

Expand All @@ -19,9 +41,36 @@ public AudioPlayer()
BufferDuration = TimeSpan.FromSeconds(5),
DiscardOnBufferOverflow = true
};
waveOut.Init(provider);
waveOut.Volume = Settings.Get<float>("Volume");
}

Initialize();
}

/// <summary>
/// Intialize the WaveOut Device and set Volume
/// </summary>
/// <param name="reinitialize">Whether to re-initialize the player.</param>
public void Initialize(bool reinitialize = false)
{
PlaybackState prevState = PlaybackState.Stopped;

if (reinitialize)
{
prevState = waveOut.PlaybackState;
Dispose();
}


waveOut = new WaveOut();
SetAudioOutputDevice(Settings.Get<int>("OutputDeviceNumer"));
waveOut.Init(provider);
waveOut.Volume = Settings.Get<float>("Volume");


if (reinitialize)
if(prevState == PlaybackState.Playing)
Play();

}

public void Play()
{
Expand Down Expand Up @@ -67,5 +116,92 @@ public void SetVolume(float vol)
{
waveOut.Volume = BoundVolume(vol);
}
}


/// <summary>
/// Get an array of the available audio output devices.
/// <para>Because of a limitation of WaveOut, device's names will be cut if they are too long.</para>
/// </summary>
public AudioDevice[] GetAudioOutputDevices()
{
AudioDevice[] devices = new AudioDevice[WaveOut.DeviceCount + 1];

for (int n = -1; n < WaveOut.DeviceCount; n++)
{
var caps = WaveOut.GetCapabilities(n);
devices[n+1] = new AudioDevice(n, caps.ProductName);
}

return devices;
}

/// <summary>
/// Get current audio output device.
/// </summary>
public AudioDevice GetCurrentAudioOutputDevice()
{
WaveOutCapabilities dev = WaveOut.GetCapabilities(waveOut.DeviceNumber);
return new AudioDevice(waveOut.DeviceNumber, dev.ProductName);
}

/// <summary>
/// Get the index of an Audio Device by providing the ID
/// </summary>
/// <param name="ID">Audio Device ID (From -1 to the amount of devices available)</param>
/// <returns></returns>
public int GetAudioDeviceIndex(int ID)
{
AudioDevice[] devices = GetAudioOutputDevices();

int desired = -1;

for (int i = 0; i < devices.Length; i++)
{
if (devices[i].ID == ID)
{
desired = i;
break;
}
}

return desired;
}

/// <summary>
/// Set the audio output device (if available); Returns current audio device (desired if valid).
/// <para><strong>Important:</strong> Requires re-initialization of WaveOut to apply changes.</para>
/// </summary>
/// <param name="device">Device ID</param>
/// <param name="reinitialize">Whether to re-initialize the player.</param>
/// <returns></returns>
public int SetAudioOutputDevice(int device, bool reinitialize = false)
{
int currentDevice = GetCurrentAudioOutputDevice().ID;

if (device == currentDevice)
return currentDevice;

int validDevice = (device < WaveOut.DeviceCount) ? device : -1; //If invalid set the default device(-1)
waveOut.DeviceNumber = validDevice;
Settings.Set<int>("OutputDeviceNumer", validDevice);
Settings.WriteSettings();

if (reinitialize)
Initialize(true);

return currentDevice;
}

/// <summary>
/// Set the audio output device (if available); Returns current audio device (desired if valid).
/// <para><strong>Important:</strong> Requires re-initialization of WaveOut to apply changes.</para>
/// </summary>
/// <param name="device">Audio Device Object</param>
/// <param name="reinitialize">Whether to re-initialize the player.</param>
/// <returns></returns>
public int SetAudioOutputDevice(AudioDevice device, bool reinitialize = false)
{
return SetAudioOutputDevice(device.ID, reinitialize);
}
}
}
9 changes: 7 additions & 2 deletions Audio/WebStreamPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ListenMoeClient
{
class WebStreamPlayer
public class WebStreamPlayer
{
AudioPlayer audioPlayer = new AudioPlayer();

Expand Down Expand Up @@ -112,5 +112,10 @@ public bool IsPlaying()
{
return playing;
}
}

public AudioPlayer GetAudioPlayer()
{
return audioPlayer;
}
}
}
Loading

0 comments on commit 51b4f78

Please sign in to comment.