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

Changed error details for MmException #1113

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions Docs/FadeInOutSampleProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ var fade = new FadeInOutSampleProvider(audio, true);
fade.BeginFadeIn(2000);
```

After fade in is complete, `FadeInComplete` event is triggered:
```c#
fade.FadeInComplete += (sender, e) => {
Console.WriteLine("Fade in was done!");
};
fade.BeginFadeIn(2000);
```

Now we can pass our `FadeInOutSampleProvider` to an output device and start playing. We'll hear the audio fading in over the first two seconds.

```c#
Expand All @@ -28,6 +36,15 @@ At some point in the future, we might want to fade out, and we can trigger that
fade.BeginFadeOut(2000);
```

It also triggers `FadeOutComplete` event, when fade out is complete

```c#
fade.FadeOutComplete += (sender, e) => {
Console.WriteLine("Fade out was done!");
};
fade.BeginFadeOut(2000);
```

Once the audio has faded out, the `FadeInOutSampleProvider` continues to read from its source but emits silence until it reaches its end, or until you call `BeginFadeIn` again.

### Taking it further
Expand Down
3 changes: 3 additions & 0 deletions NAudio.Asio/ASIODriverExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class AsioDriverExt
private int bufferSize;
private int outputChannelOffset;
private int inputChannelOffset;
/// <summary>
/// Reset Request Callback
/// </summary>
public Action ResetRequestCallback;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion NAudio.Asio/NAudio.Asio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>2.2.1</Version>
<Version>2.2.1-SA</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>Mark Heath</Authors>
Expand Down
12 changes: 10 additions & 2 deletions NAudio.Core/FileFormats/Wav/WaveFileChunkReader.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using NAudio.Utils;
using NAudio.Wave;
using System.Diagnostics;

namespace NAudio.FileFormats.Wav
{
/// <summary>
/// Reader of RIFF chunks from a WAV file
/// </summary>
public class WaveFileChunkReader
{
private WaveFormat waveFormat;
Expand All @@ -19,12 +21,18 @@ public class WaveFileChunkReader
private readonly bool storeAllChunks;
private long riffSize;

/// <summary>
/// Creates a new WaveFileChunkReader
/// </summary>
public WaveFileChunkReader()
{
storeAllChunks = true;
strictMode = false;
}

/// <summary>
/// Read the WAV header
/// </summary>
public void ReadWaveHeader(Stream stream)
{
this.dataChunkPosition = -1;
Expand Down
2 changes: 1 addition & 1 deletion NAudio.Core/MmException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public MmException(MmResult result, string function)

private static string ErrorMessage(MmResult result, string function)
{
return $"{result} calling {function}";
return $"Windows MMAPI returned \"{result}\" after call to \"{function}\"";
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion NAudio.Core/NAudio.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>Mark Heath</Authors>
<Version>2.2.1</Version>
<Version>2.2.1-SA</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<SignAssembly>true</SignAssembly>
Expand Down
18 changes: 17 additions & 1 deletion NAudio.Core/Wave/SampleProviders/FadeInOutSampleProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NAudio.Wave.SampleProviders
using System;

namespace NAudio.Wave.SampleProviders
{
/// <summary>
/// Sample Provider to allow fading in and out
Expand All @@ -13,6 +15,16 @@ enum FadeState
FadingOut,
}

/// <summary>
/// Raised when scheduled fade-in is done
/// </summary>
public event EventHandler FadeInComplete;

/// <summary>
/// Raised when scheduled fade-out is done
/// </summary>
public event EventHandler FadeOutComplete;

private readonly object lockObject = new object();
private readonly ISampleProvider source;
private int fadeSamplePosition;
Expand Down Expand Up @@ -108,6 +120,8 @@ private void FadeOut(float[] buffer, int offset, int sourceSamplesRead)
if (fadeSamplePosition > fadeSampleCount)
{
fadeState = FadeState.Silence;
FadeOutComplete?.Invoke(this, EventArgs.Empty);

// clear out the end
ClearBuffer(buffer, sample + offset, sourceSamplesRead - sample);
break;
Expand All @@ -129,6 +143,8 @@ private void FadeIn(float[] buffer, int offset, int sourceSamplesRead)
if (fadeSamplePosition > fadeSampleCount)
{
fadeState = FadeState.FullVolume;
FadeInComplete?.Invoke(this, EventArgs.Empty);

// no need to multiply any more
break;
}
Expand Down
7 changes: 7 additions & 0 deletions NAudio.Core/Wave/WaveStreams/Mp3FileReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public Mp3FileReaderBase(Stream inputStream, FrameDecompressorBuilder frameDecom

}

/// <summary>
/// Constructor that takes an input stream and a frame decompressor builder
/// </summary>
/// <param name="inputStream">Input stream</param>
/// <param name="frameDecompressorBuilder">Factory method to build a frame decompressor</param>
/// <param name="ownInputStream">Whether we own the stream and should dispose it</param>
/// <exception cref="ArgumentNullException"></exception>
protected Mp3FileReaderBase(Stream inputStream, FrameDecompressorBuilder frameDecompressorBuilder, bool ownInputStream)
{
if (inputStream == null) throw new ArgumentNullException(nameof(inputStream));
Expand Down
12 changes: 12 additions & 0 deletions NAudio.Extras/AudioPlaybackEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class AudioPlaybackEngine : IDisposable
private readonly IWavePlayer outputDevice;
private readonly MixingSampleProvider mixer;

/// <summary>
/// Audio Playback Engine
/// </summary>
public AudioPlaybackEngine(int sampleRate = 44100, int channelCount = 2)
{
outputDevice = new WaveOutEvent();
Expand All @@ -21,6 +24,9 @@ public AudioPlaybackEngine(int sampleRate = 44100, int channelCount = 2)
outputDevice.Play();
}

/// <summary>
/// Fire and forget playback of sound
/// </summary>
public void PlaySound(string fileName)
{
var input = new AudioFileReader(fileName);
Expand All @@ -40,6 +46,9 @@ private ISampleProvider ConvertToRightChannelCount(ISampleProvider input)
throw new NotImplementedException("Not yet implemented this channel count conversion");
}

/// <summary>
/// Fire and forget playback of a cached sound
/// </summary>
public void PlaySound(CachedSound sound)
{
AddMixerInput(new CachedSoundSampleProvider(sound));
Expand All @@ -50,6 +59,9 @@ private void AddMixerInput(ISampleProvider input)
mixer.AddMixerInput(ConvertToRightChannelCount(input));
}

/// <summary>
/// Disposes this instance
/// </summary>
public void Dispose()
{
outputDevice.Dispose();
Expand Down
10 changes: 10 additions & 0 deletions NAudio.Extras/AutoDisposeFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ public class AutoDisposeFileReader : ISampleProvider
{
private readonly ISampleProvider reader;
private bool isDisposed;

/// <summary>
/// Creates a new file reader that disposes the source reader when it finishes
/// </summary>
public AutoDisposeFileReader(ISampleProvider reader)
{
this.reader = reader;
WaveFormat = reader.WaveFormat;
}

/// <summary>
/// Reads samples from this file reader
/// </summary>
public int Read(float[] buffer, int offset, int count)
{
if (isDisposed)
Expand All @@ -33,6 +40,9 @@ public int Read(float[] buffer, int offset, int count)
return read;
}

/// <summary>
/// The WaveFormat of this file reader
/// </summary>
public WaveFormat WaveFormat { get; }
}
}
11 changes: 11 additions & 0 deletions NAudio.Extras/CachedSound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ namespace NAudio.Extras
/// </summary>
public class CachedSound
{
/// <summary>
/// Audio data
/// </summary>
public float[] AudioData { get; }

/// <summary>
/// Format of the audio
/// </summary>
public WaveFormat WaveFormat { get; }

/// <summary>
/// Creates a new CachedSound from a file
/// </summary>
public CachedSound(string audioFileName)
{
using (var audioFileReader = new AudioFileReader(audioFileName))
Expand Down
12 changes: 12 additions & 0 deletions NAudio.Extras/Equalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class Equalizer : ISampleProvider
private readonly int bandCount;
private bool updated;

/// <summary>
/// Creates a new Equalizer
/// </summary>
public Equalizer(ISampleProvider sourceProvider, EqualizerBand[] bands)
{
this.sourceProvider = sourceProvider;
Expand All @@ -43,14 +46,23 @@ private void CreateFilters()
}
}

/// <summary>
/// Update the equalizer settings
/// </summary>
public void Update()
{
updated = true;
CreateFilters();
}

/// <summary>
/// Gets the WaveFormat of this Sample Provider
/// </summary>
public WaveFormat WaveFormat => sourceProvider.WaveFormat;

/// <summary>
/// Reads samples from this Sample Provider
/// </summary>
public int Read(float[] buffer, int offset, int count)
{
int samplesRead = sourceProvider.Read(buffer, offset, count);
Expand Down
12 changes: 12 additions & 0 deletions NAudio.Extras/EqualizerBand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
namespace NAudio.Extras
{
/// <summary>
/// Equalizer Band
/// </summary>
public class EqualizerBand
{
/// <summary>
/// Frequency
/// </summary>
public float Frequency { get; set; }
/// <summary>
/// Gain
/// </summary>
public float Gain { get; set; }
/// <summary>
/// Bandwidth
/// </summary>
public float Bandwidth { get; set; }
}
}
2 changes: 1 addition & 1 deletion NAudio.Extras/NAudio.Extras.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RepositoryUrl>https://github.com/naudio/NAudio</RepositoryUrl>
<Copyright>© Mark Heath 2023</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>2.2.1</Version>
<Version>2.2.1-SA</Version>
<PackageIcon>naudio-icon.png</PackageIcon>
</PropertyGroup>

Expand Down
Loading