Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IWavePlayer and IWaveIn cross-platform implementations via SDL2 #1153

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
63030de
NAudio SDL2 audio wrappers.
Apr 20, 2024
1f139a8
exceptions
Apr 22, 2024
ba92963
Refactoring sdl2 wrappers, demo
Apr 29, 2024
d4a0c0f
field style
Apr 30, 2024
75617ae
Update WaveOutSdl EnumerateOutputDevices.md
alextnull Apr 30, 2024
63a1a4e
sdl2 unit test
Apr 30, 2024
a4186ff
Merge branch 'sdl2' of https://github.com/alextnull/NAudio into sdl2
Apr 30, 2024
14deec4
authors
Apr 30, 2024
ade1020
Create Sdl2Recording.md
alextnull Apr 30, 2024
a8dcda2
Create Sdl2Playback.md
alextnull Apr 30, 2024
56f7e3b
Rename Sdl2Recording.md to Docs/Sdl2Recording.md
alextnull Apr 30, 2024
6768f9c
NAudioTests.csproj
Apr 30, 2024
127efb3
exclude assets
Apr 30, 2024
9aede34
Update Sdl2Playback.md
alextnull May 1, 2024
16fa38c
WaveOutSdl volume mixer
May 3, 2024
d51e9be
Merge branch 'sdl2' of https://github.com/alextnull/NAudio into sdl2
May 3, 2024
49f9ae3
Smarter audio format guess
May 13, 2024
6db5577
Compatibility for versions below SDL 2.0.16
May 13, 2024
f8ea9f9
Merge pull request #1 from alextnull/sdl2
alextnull May 13, 2024
52855cd
fix sdl2 version comparison
May 20, 2024
051c1c1
Merge pull request #2 from alextnull/sdl2
alextnull May 20, 2024
7d7e1dc
sdl2-2.2.3
May 20, 2024
04ec2a8
authors, description
May 20, 2024
fe716d1
AvaloniaDemo init
May 20, 2024
da640d7
WaveOutSdl Stop bugfix
May 21, 2024
5c3a5bd
WaveOutSdl Stop bugfix
May 21, 2024
69aa4e6
AvaloniaDemo AudioPlaybackDemo
May 21, 2024
bf1b8ca
WaveOutSdl fixes
May 23, 2024
c19e5b9
Audio conversion fix
May 23, 2024
6fe2e48
AudioConversion clarification
May 23, 2024
1b6e4c7
ActualWaveFormat, PeakLevel changes
May 24, 2024
b17ee48
sdl2-2.2.5
May 24, 2024
aacf8d6
Removed properties that complicated the code: AudioConversion, Actual…
Oct 19, 2024
562a050
Android partial progress
Nov 6, 2024
b299ab0
Remove AdjustLatencyPercent
Nov 10, 2024
b0a4724
Automatic initialization and termination. Change capabilities classes…
Nov 14, 2024
9700a45
AudioFileStreamReader
Nov 14, 2024
091f240
Changelog
Nov 15, 2024
e2193f1
Console demo lib update
Nov 15, 2024
23c646e
Changelog
Nov 15, 2024
a02ea96
Repo url
Nov 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Docs/EnumerateOutputDevices.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ You can then use the driver name to open the device:
new AsioOut(driverName);
```

# WaveOutSdl

Note that you must install the package with native libraries or distribute them manually (nuget NAudio.Sdl2.Library).

To discover the number of output devices and discover its capabilities you can use `WaveOutSdl.GetCapabilitiesList`.

Note that you can also pass an index of -1 which is the default audio device.

```c#
var capabilitiesList = WaveOutSdl.GetCapabilitiesList();
foreach (var caps in capabilitiesList)
{
Console.WriteLine(caps.ToString(Environment.NewLine));
Console.WriteLine();
}
```

Once you've selected the device you want, you can open it by creating an instance of `WaveOutSdl` and specifying it as the `DeviceId`:

```c#
var outputDevice = new WaveOutSdl() { DeviceId = DeviceId };
```

# Management Objects

Finally you can use Windows Management Objects to get hold of details of the sound devices installed. This doesn't map specifically to any of the NAudio output device types, but can be a source of useful information
Expand Down
42 changes: 42 additions & 0 deletions Docs/Sdl2Playback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Playback with SDL2

The `WaveOutSdl` class in NAudio allows you to playback audio using an SDL2. SDL2 is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.

To use SDL2 you will need native libraries. You can find these libraries on nuget or on official website.

## Opening an SDL2 device for playback

To discover the list of the accessible playback devices on your system you use `WaveOutSdl.GetCapabilitiesList()`.

We can use 'DeviceNumber' property from 'WaveOutSdlCapabilities' instance and set this number via 'DeviceId' property on the 'WaveOutSdl' instance.

```c#
var waveOutSdl = new WaveOutSdl()
{
DeviceId = waveOutSdlCaps.DeviceNumber
};
```

## Inititializing wave provider

Call `Init`, this lets us pass the `IWaveProvider` we want to play. Note that the sample rate of the `WaveFormat` of the input provider must be one supported by the SDL2.

```c#
waveOutSdl.Init(myWaveProvider);
```

## Stop Playback

We stop playing by calling Stop().

```c#
waveOutSdl.Stop(); // stop playing
```

As with other NAudio `IWavePlayer` implementations, we'll get a `PlaybackStopped` event firing when the playback stops.

And of course we should remember to `Dispose` our instance of `WaveOutSdl` when we're done with it.

```c#
waveOutSdl.Dispose();
```
63 changes: 63 additions & 0 deletions Docs/Sdl2Recording.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Recording with SDL2

The `WaveInSdl` class in NAudio allows you to record audio using an SDL2. SDL2 is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.

To use SDL2 you will need native libraries. You can find these libraries on nuget or on official website.

## Opening an SDL2 device for recording

To discover the list of the accessible recording devices on your system you use `WaveInSdl.GetCapabilitiesList()`.

We can use 'DeviceNumber' property from 'WaveInSdlCapabilities' instance and set this number via 'DeviceId' property on the 'WaveInSdl' instance.

We can use 'Frequency', 'Bits', and 'Channels' properies from 'WaveInSdlCapabilities' instance and set 'WaveFormat' property on the 'WaveInSdl' instance.

```c#
var waveInSdl = new WaveInSdl()
{
DeviceId = waveInSdlCaps.DeviceNumber,
WaveFormat = new WaveFormat(waveInSdlCaps.Frequency, waveInSdlCaps.Bits, waveInSdlCaps.Channels)
};
```

## Start Recording

We need to subscribe to the `DataAvailable` event in order to process audio received in the SDL2 buffer callback.

And we kick off recording by calling `StartRecording()`.

```c#
waveInSdl.DataAvailable += OnWaveInSdlDataAvailable;
waveInSdl.StartRecording(); // start recording
```

## Handle received audio

When we receive audio we get access to the byte array buffer in an `WaveInEventArgs` object.

Here's the simplest handler for `DataAvailable` that just gets the audio as byte array and writes them to a `WaveFileWriter` that we've set up in advance.

```c#
void OnWaveInSdlDataAvailable(object sender, WaveInEventArgs e)
{
waveFileWriter.Write(e.Buffer, 0, e.BytesRecorded);
}
```

For a real application, you'd probably want to write your own logic in here.

## Stop Recording

We stop recording by calling `StopRecording()`.

```c#
waveInSdl.StopRecording();
```

As with other NAudio `IWaveIn` implementations, we'll get a `RecordingStopped` event firing when the recording stops.

And of course we should remember to `Dispose` our instance of `WaveInSdl` when we're done with it.

```c#
waveInSdl.Dispose();
```
28 changes: 28 additions & 0 deletions NAudio.Sdl2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
### 2.2.6 (15 Nov 2024)
* BREAKING CHANGES
* Remove `AudioConversion`
* Remove `ActualWaveFormat`
* Remove `AdjustLatencyPercent`
* Change `WaveInSdlCapabilities`, `WaveOutSdlCapabilities` classes to structs
* Added automatic initialization and cleanup of SDL2 resources. `WaveInSdl`, `WaveOutSdl` constructors can throw exceptions
* Added experimental android support which requires the `NAudio.Sdl2.Library.Android` package. Use the `NAudioAvaloniaDemo.Android` project as an example
* Changes in NAudioAvaloniaDemo

### 2.2.5 (24 May 2024)
* Fix `WaveInSdl` bugs

### 2.2.4 (23 May 2024)
* Port NAudioWpfDemo to NAudioAvaloniaDemo
* Fix `WaveOutSdl` bugs
* Fix `AudioConversion` enum

### 2.2.3 (20 May 2024)
* Fix SDL version comparison

### 2.2.2 (13 May 2024)
* Added volume mixer for `WaveOutSdl`
* More reliable audio format recognition
* Added compatibility with versions lower than SDL-2.0.16

### 2.2.1 (30 Apr 2024)
* Initial version, `WaveInSdl` and `WaveOutSdl` implementations
Loading