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

Commit

Permalink
Deprecating Signal's Samples and Channels properties in favor of Numb…
Browse files Browse the repository at this point in the history
…erOfSamples and NumberOfChannels;

Adding a new NumberOfFrames property to report the number of samples at each signal channel;
Adding convertion overloads in the SampleConverter class to convert from float to double;
Adding a base class for audio signal generators;
  • Loading branch information
cesarsouza committed Oct 31, 2017
1 parent aa1e78b commit 47b7d7b
Show file tree
Hide file tree
Showing 34 changed files with 523 additions and 308 deletions.
10 changes: 4 additions & 6 deletions Sources/Accord.Audio.DirectSound/Accord.Audio.DirectSound.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<OutputPath>$(SolutionDir)..\Debug\</OutputPath>
<DocumentationFile></DocumentationFile>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net35|AnyCPU' ">
<DefineConstants>TRACE;NET35</DefineConstants>
Expand Down Expand Up @@ -40,8 +41,6 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />


<Choose>
<When Condition="'$(Configuration)' == 'net35'">
<ItemGroup>
Expand Down Expand Up @@ -80,7 +79,6 @@
</ItemGroup>
</Otherwise>
</Choose>

<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
Expand All @@ -99,9 +97,9 @@
<Compile Include="AudioOutputDevice.cs" />
<Compile Include="AudioDeviceInfo.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Formats\WaveEncoder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WaveDecoder.cs" />
<Compile Include="WaveEncoder.cs" />
<Compile Include="Formats\WaveDecoder.cs" />
<Compile Include="WaveFileAudioSource.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace Accord.Audio.Formats
/// </code>
/// </example>
///
[FormatEncoder("wav")]
public class WaveEncoder : IAudioEncoder
{
private Stream waveStream;
Expand Down Expand Up @@ -273,7 +274,7 @@ public void Encode(Signal signal)
}

// Update counters
numberOfSamples += signal.Samples;
numberOfSamples += signal.NumberOfSamples;
numberOfFrames += signal.Length;
bytes += signal.RawData.Length;
duration += (int)signal.Duration.TotalMilliseconds;
Expand Down Expand Up @@ -339,7 +340,7 @@ private void firstWriteHeaders()

private void initialize(Signal signal)
{
this.channels = signal.Channels;
this.channels = signal.NumberOfChannels;
this.sampleRate = signal.SampleRate;
this.sampleFormat = signal.SampleFormat;
this.bitsPerSample = Signal.GetSampleSize(signal.SampleFormat);
Expand Down
4 changes: 3 additions & 1 deletion Sources/Accord.Audio/Accord.Audio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
<Compile Include="Features\IAudioFeatureExtractor.cs" />
<Compile Include="Filters\AddFilter.cs" />
<Compile Include="Filters\MonoFilter.cs" />
<Compile Include="Formats\AudioEncoder.cs" />
<Compile Include="Formats\AudioDecoder.cs" />
<Compile Include="Formats\FrameInfo.cs" />
<Compile Include="Formats\IAudioDecoder.cs" />
<Compile Include="Formats\IAudioEncoder.cs" />
<Compile Include="Generators\Base\BaseSignalGenerator.cs" />
<Compile Include="Generators\SignalGenerator.cs" />
<Compile Include="Generators\SineGenerator.cs" />
<Compile Include="AudioSourceMixer.cs" />
Expand Down Expand Up @@ -100,7 +102,7 @@
<Compile Include="Signals\Signal.cs" />
<Compile Include="Signals\ComplexSignal.cs" />
<Compile Include="Generators\CosineGenerator.cs" />
<Compile Include="Generators\ISignalGenerator.cs" />
<Compile Include="Generators\Base\ISignalGenerator.cs" />
<Compile Include="Generators\SquareGenerator.cs" />
<Compile Include="Filters\Base\IFilter.cs" />
<Compile Include="IAudioSource.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/AudioSourceMixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private unsafe void WorkerThread()

short* src = (short*)signal.Data.ToPointer();

if (signal.Channels < channels)
if (signal.NumberOfChannels < channels)
{
for (int j = 0; j < frameSize; j += 2, src++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class BaseComplexFilter : IComplexFilter
public ComplexSignal Apply(ComplexSignal complexSignal)
{
// get number of channels and samples
int channels = complexSignal.Channels;
int channels = complexSignal.NumberOfChannels;
int samples = complexSignal.Length;

// retrieve other information
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/ComplexFilters/CombFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void generateBaseSignal()
///
protected override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
{
int samples = sourceData.Samples;
int samples = sourceData.NumberOfSamples;

unsafe
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/ComplexFilters/EnvelopeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected unsafe override void ProcessFilter(ComplexSignal sourceData, ComplexSi
if (sourceData.Status != ComplexSignalStatus.Analytic)
throw new ArgumentException("Signal must be in analytic form.", "sourceData");

int samples = sourceData.Samples;
int samples = sourceData.NumberOfSamples;

Complex* src = (Complex*)sourceData.Data.ToPointer();
Complex* dst = (Complex*)destinationData.Data.ToPointer();
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/Filters/AddFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public AddFilter(Signal overlaySignal)
protected unsafe override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int channels = sourceData.Channels;
int channels = sourceData.NumberOfChannels;
int length = sourceData.Length;

if (format == SampleFormat.Format32BitIeeeFloat)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Accord.Audio/Filters/Base/BaseFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public Signal Apply(Signal signal)
CheckSourceFormat(signal.SampleFormat);

// get number of channels and samples
int channels = signal.Channels;
int samples = signal.Length;
int channels = signal.NumberOfChannels;
int samples = signal.NumberOfFrames;

// retrieve other information
int rate = signal.SampleRate;
Expand Down
4 changes: 2 additions & 2 deletions Sources/Accord.Audio/Filters/Base/BaseInPlaceFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public Signal Apply(Signal signal)
CheckSourceFormat(signal.SampleFormat);

// get number of channels and samples
int channels = signal.Channels;
int samples = signal.Length;
int channels = signal.NumberOfChannels;
int samples = signal.NumberOfFrames;

// retrieve other information
int rate = signal.SampleRate;
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/Filters/EnvelopeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public EnvelopeFilter(float alpha)
protected unsafe override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int channels = sourceData.Channels;
int channels = sourceData.NumberOfChannels;
int length = sourceData.Length;

if (format == SampleFormat.Format32BitIeeeFloat)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/Filters/ExtractChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected override Signal NewSignal(int channels, int samples, int rate, SampleF
protected unsafe override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int channels = sourceData.Channels;
int channels = sourceData.NumberOfChannels;
int length = sourceData.Length;

if (Channel < 0 || Channel > channels)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/Filters/HighPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public HighPassFilter(float alpha)
protected override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int channels = sourceData.Channels;
int channels = sourceData.NumberOfChannels;
int length = sourceData.Length;

if (format == SampleFormat.Format32BitIeeeFloat)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/Filters/LowPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public LowPassFilter(float alpha)
protected override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int channels = sourceData.Channels;
int channels = sourceData.NumberOfChannels;
int length = sourceData.Length;

if (format == SampleFormat.Format32BitIeeeFloat)
Expand Down
8 changes: 6 additions & 2 deletions Sources/Accord.Audio/Filters/MonoFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace Accord.Audio.Filters
{
using Accord.Diagnostics;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -61,8 +62,11 @@ protected override Signal NewSignal(int channels, int samples, int rate, SampleF
protected unsafe override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int channels = sourceData.Channels;
int length = sourceData.Length;
int channels = sourceData.NumberOfChannels;
int length = sourceData.NumberOfFrames;

Debug.Assert(sourceData.NumberOfFrames == destinationData.NumberOfSamples);
Debug.Assert(destinationData.NumberOfChannels == 1);

if (format == SampleFormat.Format32BitIeeeFloat)
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/Filters/VolumeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public VolumeFilter(float volume)
protected override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int channels = sourceData.Channels;
int channels = sourceData.NumberOfChannels;
int length = sourceData.Length;

if (format == SampleFormat.Format32BitIeeeFloat)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Accord.Audio/Filters/WaveRectifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public WaveRectifier(bool halfRectificationOnly)
protected unsafe override void ProcessFilter(Signal sourceData, Signal destinationData)
{
SampleFormat format = sourceData.SampleFormat;
int samples = sourceData.Samples;
int samples = sourceData.NumberOfSamples;

if (format == SampleFormat.Format32BitIeeeFloat)
{
Expand Down
9 changes: 5 additions & 4 deletions Sources/Accord.Audio/Formats/AudioDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Accord.Audio.Formats
/// </summary>
///
/// <remarks><para>The class represent a help class, which simplifies decoding of audio
/// files finding appropriate image decoder automatically (using list of registered
/// files finding appropriate audio decoder automatically (using list of registered
/// audio decoders). Instead of using required audio decoder directly, users may use this
/// class, which will find required decoder by file's extension.</para>
///
Expand Down Expand Up @@ -84,7 +84,7 @@ public static Signal DecodeFromFile(string fileName, out FrameInfo frameInfo)
{
string fileExtension = FormatDecoderAttribute.GetNormalizedExtension(fileName);

IAudioDecoder decoder = FormatDecoderAttribute.GetDecoder(fileExtension, decoderTypes, decoders.Value);
IAudioDecoder decoder = FormatDecoderAttribute.GetDecoders(fileExtension, decoderTypes, decoders.Value);

if (decoder != null)
{
Expand All @@ -99,14 +99,15 @@ public static Signal DecodeFromFile(string fileName, out FrameInfo frameInfo)

decoder.Close();

frameInfo = new FrameInfo(signal.Channels, signal.SampleRate, Signal.GetSampleSize(signal.SampleFormat), 0, signal.Length);
frameInfo = new FrameInfo(signal.NumberOfChannels, signal.SampleRate, Signal.GetSampleSize(signal.SampleFormat), 0, signal.Length);

return signal;
}
}

throw new ArgumentException(String.Format("No suitable decoder has been found for the file format {0}. If ", fileExtension) +
"you are trying to decode .wav files, please add a reference to Accord.Audio.DirectSouond.", "fileName");
"you are trying to decode .wav files, please add a reference to Accord.Audio.DirectSound. You might need to instantiate" +
"at least one type from this assembly to make sure it has been loaded in the AppDomain of your applicatoin.", "fileName");
}

}
Expand Down
91 changes: 91 additions & 0 deletions Sources/Accord.Audio/Formats/AudioEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Accord Audio Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//

namespace Accord.Audio.Formats
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Accord.Compat;

/// <summary>
/// Audio encoder to encode different custom audio file formats.
/// </summary>
///
/// <remarks><para>The class represent a help class, which simplifies encoding of audio
/// files finding appropriate audio encoder automatically (using list of registered
/// audio encoders). Instead of using required audio encoder directly, users may use this
/// class, which will find required encoder by file's extension.</para>
///
/// <para>
/// By default the class will query all referenced assemblies for types that are marked
/// with the <see cref="FormatEncoderAttribute"/>. If the user would like to implement
/// a new encoder, all that is necessary is to mark a new class with the <see cref="FormatEncoderAttribute"/>
/// and make it implement the <see cref="IAudioEncoder"/> interface.</para>
/// </remarks>
///
public class AudioEncoder
{
private static Dictionary<string, Type> encoderTypes = new Dictionary<string, Type>();
private static ThreadLocal<Dictionary<string, IAudioEncoder>> encoders =
new ThreadLocal<Dictionary<string, IAudioEncoder>>(() => new Dictionary<string, IAudioEncoder>());

/// <summary>
/// Encodes a signal from the specified file.
/// </summary>
///
/// <param name="fileName">File name to save the signal to.</param>
/// <param name="signal">The audio signal that should be saved to disk.</param>
///
public static void EncodeToFile(string fileName, Signal signal)
{
string fileExtension = FormatHandlerAttribute.GetNormalizedExtension(fileName);

IAudioEncoder encoder = FormatEncoderAttribute.GetEncoder(fileExtension, encoderTypes, encoders.Value);

if (encoder != null)
{
// open stream
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
// open decoder
encoder.Open(stream);

// write all audio frames
encoder.Encode(signal);

encoder.Close();
}

return;
}

throw new ArgumentException(String.Format("No suitable encoder has been found for the file format {0}. If ", fileExtension) +
"you are trying to encode .wav files, please add a reference to Accord.Audio.DirectSound.", "fileName");
}

}
}
Loading

0 comments on commit 47b7d7b

Please sign in to comment.