Skip to content

Commit

Permalink
Concatenating audio documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
markheath committed Nov 10, 2017
1 parent 162dcc3 commit 2ad18c7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
44 changes: 44 additions & 0 deletions Docs/ConcatenatingAudio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Concatenating Audio

When you play audio or render audio to a file, you create a single `ISampleProvider` or `IWaveProvider` that represents the whole piece of audio to be played. So playback will continue until you reach the end, and then stop.

But what if you have two pieces of audio you want to play back to back? The `ConcatenatingSampleProvider` enables you to schedule one or more pieces of audio to play one after the other.

Here's a simple example where we have three audio files that are going to play back to back. Note that the three audio files must have exactly the same sample rate, channel count and bit depth, because it's not possible to change those during playback.

```c#
var first = new AudioFileReader("first.mp3");
var second = new AudioFileReader("second.mp3");
var third = new AudioFileReader("third.mp3");

var playlist = new ConcatenatingSampleProvider(new[] { first, second, third });)

// to play:
outputDevice.Init(playlist);
outputDevice.Play();

// ... OR ... to save to file
WaveFileWriter.CreateWaveFile16("playlist.wav", playlist);
```

Note that the `ConcatenatingSampleProvider` does not provide repositioning. If you want that, you can quite simply copy the code for `ConcatenatingSampleProvider` and adjust it to allow you to rewind, or jump to the beginning of one of the inputs, depending on your specific requirements.

# FollowedBy Extension Helpers

There are some helpful extension methods you can make use of to simplify concatenating. For example, to append one `ISampleProvider` onto the end of another, use `FollowedBy`. Under the hood this simply creates a `ConcatenatingSampleProvider`:

```c#
var first = new AudioFileReader("first.mp3");
var second = new AudioFileReader("second.mp3");
var playlist = first.FollowedBy(second);
```

You can also provide a duration of silence that you want after the first sound has finished and before the second begins:

```c#
var first = new AudioFileReader("first.mp3");
var second = new AudioFileReader("second.mp3");
var playlist = first.FollowedBy(TimeSpan.FromSeconds(1), second);
```

This makes use of an `OffsetSampleProvider` in conjunction with a `ConcatenatingSampleProvider`
1 change: 1 addition & 0 deletions NAudio.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{778D
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{BA7F6DBB-9FC4-49E6-92E4-621EFE4BBBBC}"
ProjectSection(SolutionItems) = preProject
Docs\ConcatenatingAudio.md = Docs\ConcatenatingAudio.md
Docs\ConvertBetweenStereoAndMono.md = Docs\ConvertBetweenStereoAndMono.md
Docs\ConvertMp3ToWav.md = Docs\ConvertMp3ToWav.md
Docs\EnumerateOutputDevices.md = Docs\EnumerateOutputDevices.md
Expand Down
2 changes: 2 additions & 0 deletions NAudio/NAudio.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeEditing/Localization/Localizable/@EntryValue">No</s:String></wpf:ResourceDictionary>
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ NAudio comes with several demo applications which are the quickest way to see ho
- [Enumerate and select Output Devices](Docs/EnumerateOutputDevices.md)
- [Convert an MP3 to WAV](Docs/ConvertMp3ToWav.md)
- [Convert between mono and stereo](Docs/ConvertBetweenStereoAndMono.md)
- [Concatenating Audio](Docs/ConcatenatingAudio.md)
- [Understand how to convert between any audio formats you have codecs for](http://www.codeproject.com/Articles/501521/How-to-convert-between-most-audio-formats-in-NET)
- [Encode to MP3 or other formats using MediaFoundationEncoder](http://markheath.net/post/naudio-mediafoundationencoder)
- [Implement "Fire and Forget" Playback (e.g. game sound effects)](http://mark-dot-net.blogspot.co.uk/2014/02/fire-and-forget-audio-playback-with.html)
Expand Down Expand Up @@ -100,7 +101,7 @@ To be successful developing applications that process digital audio, there are s

## How do I...?

The best way to learn how to use NAudio is to download the source code and look at the two demo applications - [NAudioDemo](https://github.com/naudio/NAudio/tree/master/NAudioDemo) and p[NAudioWpfDemo](https://github.com/naudio/NAudio/tree/master/NAudioWpfDemo). These demonstrate several of the key capabilities of the NAudio framework. They also have the advantage of being kept up to date, whilst some of the tutorials you will find on the internet refer to old versions of NAudio.
The best way to learn how to use NAudio is to download the source code and look at the two demo applications - [NAudioDemo](https://github.com/naudio/NAudio/tree/master/NAudioDemo) and [NAudioWpfDemo](https://github.com/naudio/NAudio/tree/master/NAudioWpfDemo). These demonstrate several of the key capabilities of the NAudio framework. They also have the advantage of being kept up to date, whilst some of the tutorials you will find on the internet refer to old versions of NAudio.

## FAQ

Expand Down

0 comments on commit 2ad18c7

Please sign in to comment.