luojunyuan / LibALAC

Native Microsoft Windows dynamic-link library (DLL) and .NET standard implementation of the Apple Lossless Audio Codec

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LibALAC

Microsoft Windows dynamic-link library (DLL) implementation of the Apple Lossless Audio Codec (ALAC). Mirror copy of the ALAC encoder and decoder from http://alac.macosforge.org/ with changes for compiling as DLL in Visual Studio 2017.

LibALAC .NET

.NET standard wrapper for the native dynamic-link library (DLL) implementation of the Apple Lossless Audio Codec (ALAC).

Installation

Use Git or SVN to download the source code using the web URL:

https://github.com/GiteKat/LibALAC.git

Install the LibALAC NuGet package:

Install-Package LibALAC

C# code sample

The following example show resampling, ALAC-encoding and decoding of a .mp3 input file using the excellent CSCore .NET Audio Library and LibALAC:

using CSCore;
using CSCore.Codecs;
using CSCore.DSP;
using LibALAC;
using System;
using System.Linq;

namespace Demo
{
    class Program
    {
        const string FileName = @"C:\Music\demo.mp3";
        const int SampleRate = 44100;
        const int Channels = 2;
        const int BitsPerSample = 16;
        const int FramesPerPacket = 4096;

        static void Main(string[] args)
        {
            DateTime startTime = DateTime.Now;
            Encoder encoder = new Encoder(SampleRate, Channels, BitsPerSample, FramesPerPacket);
            Decoder decoder = new Decoder(SampleRate, Channels, BitsPerSample, FramesPerPacket);
            using (IWaveSource waveSource = CodecFactory.Instance.GetCodec(FileName))
            {
                WaveFormat waveFormat = new WaveFormatExtensible(SampleRate, BitsPerSample, Channels, AudioSubTypes.Pcm);
                using (DmoResampler resampler = new DmoResampler(waveSource, waveFormat))
                {
                    int read;
                    byte[] buffer = new byte[encoder.BytesPerPacket];
                    while ((read = resampler.Read(buffer, 0, encoder.BytesPerPacket)) > 0)
                    {
                        byte[] encoded = encoder.Encode(buffer, read);
                        byte[] decoded = decoder.Decode(encoded);
                        if (read < buffer.Length)
                            Array.Resize(ref buffer, read);
                        if (!decoded.SequenceEqual(buffer))
                            throw new Exception("Encoding/Decoding error!");
                    }
                }
            }
            encoder.Dispose();
            decoder.Dispose();
            Console.WriteLine("Encoding/Decoding-Time: " + DateTime.Now.Subtract(startTime));
            Console.ReadLine();
        }
    }
}

License

All sources are available under the Apache license 2.0.

About

Native Microsoft Windows dynamic-link library (DLL) and .NET standard implementation of the Apple Lossless Audio Codec

License:Apache License 2.0


Languages

Language:C++ 46.1%Language:C 43.8%Language:C# 10.2%