forked from naudio/NAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters