Exemplo n.º 1
0
 internal static extern AudioConverterStatus AudioConverterFillComplexBuffer(
     [NotNull] AudioConverterHandle handle,
     [NotNull] NativeCallbacks.AudioConverterComplexInputCallback inputCallback,
     IntPtr userData,
     ref uint packetSize,
     ref AudioBufferList outputData,
     [CanBeNull][In, Out] AudioStreamPacketDescription[] packetDescriptions);
Exemplo n.º 2
0
        public unsafe SampleBuffer DecodeSamples()
        {
            Span <int> buffer = stackalloc int[(int)(_defaultFrameCount * _outputDescription.ChannelsPerFrame)];

            var bufferList = new AudioBufferList
            {
                NumberBuffers = 1,
                Buffers       = new AudioBuffer[1]
            };

            bufferList.Buffers[0].NumberChannels = _outputDescription.ChannelsPerFrame;
            bufferList.Buffers[0].DataByteSize   = (uint)(buffer.Length * Marshal.SizeOf <int>());
            bufferList.Buffers[0].Data           = new IntPtr(Unsafe.AsPointer(ref MemoryMarshal.GetReference(buffer)));

            var frameCount = _defaultFrameCount;

            // ReSharper disable once PossibleNullReferenceException
            _converter.FillBuffer(ref frameCount, ref bufferList, null);

            if (frameCount == 0)
            {
                Finished = true;
            }

            var result = new SampleBuffer(
                buffer.Slice(0, (int)(frameCount * _outputDescription.ChannelsPerFrame)),
                (int)_outputDescription.ChannelsPerFrame, 32);

            return(result);
        }
Exemplo n.º 3
0
        public unsafe void Submit(SampleBuffer samples)
        {
            if (samples.Frames == 0)
            {
                return;
            }

            if (_replayGainExport != null)
            {
                samples = _replayGainExport.Value.Process(samples);
            }

            Span <float> buffer = stackalloc float[samples.Frames * samples.Channels];

            samples.CopyToInterleaved(buffer);

            var bufferList = new AudioBufferList
            {
                NumberBuffers = 1,
                Buffers       = new AudioBuffer[1]
            };

            bufferList.Buffers[0].NumberChannels = (uint)samples.Channels;
            bufferList.Buffers[0].DataByteSize   = (uint)(buffer.Length * Marshal.SizeOf <float>());
            bufferList.Buffers[0].Data           = new IntPtr(Unsafe.AsPointer(ref MemoryMarshal.GetReference(buffer)));

            // ReSharper disable once PossibleNullReferenceException
            var status = _audioFile.Write(bufferList, (uint)samples.Frames);

            if (status != ExtendedAudioFileStatus.Ok)
            {
                throw new AudioEncodingException($"Apple AAC encoder encountered error '{status}'.");
            }
        }
Exemplo n.º 4
0
        public unsafe void Submit(SampleBuffer samples)
        {
            if (samples.Frames == 0)
            {
                return;
            }

            Span <int> buffer = stackalloc int[samples.Frames * samples.Channels];

            samples.CopyToInterleaved(buffer, _bitsPerSample);

            var bufferList = new AudioBufferList
            {
                NumberBuffers = 1,
                Buffers       = new AudioBuffer[1]
            };

            bufferList.Buffers[0].NumberChannels = (uint)samples.Channels;
            bufferList.Buffers[0].DataByteSize   = (uint)(buffer.Length * sizeof(int));
            bufferList.Buffers[0].Data           = new(Unsafe.AsPointer(ref MemoryMarshal.GetReference(buffer)));

            var status = _audioFile !.Write(bufferList, (uint)samples.Frames);

            if (status != ExtendedAudioFileStatus.Ok)
            {
                throw new AudioEncodingException($"Apple Lossless encoder encountered error '{status}'.");
            }
        }
Exemplo n.º 5
0
 internal void FillBuffer(
     ref uint packetSize,
     ref AudioBufferList outputBuffer,
     [CanBeNull] AudioStreamPacketDescription[] packetDescriptions)
 {
     SafeNativeMethods.AudioConverterFillComplexBuffer(_handle, _inputCallback, IntPtr.Zero,
                                                       ref packetSize, ref outputBuffer, packetDescriptions);
 }
Exemplo n.º 6
0
        unsafe AudioConverterStatus InputCallback(
            IntPtr handle,
            ref uint numberPackets,
            ref AudioBufferList data,
            IntPtr packetDescriptions,
            IntPtr userData)
        {
            if (_buffer == null)
            {
                _buffer = MemoryPool <byte> .Shared.Rent((int)
                                                         (numberPackets * _audioFile.GetProperty <uint>(AudioFilePropertyId.PacketSizeUpperBound)));

                // ReSharper disable once PossibleNullReferenceException
                _bufferHandle = _buffer.Memory.Pin();
            }

            if (_descriptionsHandle.IsAllocated)
            {
                _descriptionsHandle.Free();
            }

            var inputDescriptions = new AudioStreamPacketDescription[numberPackets];

            _audioFile.ReadPackets(out var numBytes, inputDescriptions, _packetIndex, ref numberPackets,
                                   new IntPtr(_bufferHandle.Pointer));

            _packetIndex += numberPackets;

            data.Buffers[0].DataByteSize = numBytes;
            data.Buffers[0].Data         = new IntPtr(_bufferHandle.Pointer);

            // If this conversion requires packet descriptions, provide them
            // ReSharper disable once InvertIf
            if (packetDescriptions != IntPtr.Zero)
            {
                _descriptionsHandle = GCHandle.Alloc(inputDescriptions, GCHandleType.Pinned);
                Marshal.WriteIntPtr(packetDescriptions, _descriptionsHandle.AddrOfPinnedObject());
            }

            return(AudioConverterStatus.Ok);
        }
Exemplo n.º 7
0
 internal static extern ExtendedAudioFileStatus ExtAudioFileWrite(
     [NotNull] ExtendedAudioFileHandle handle,
     uint frames,
     ref AudioBufferList data);
Exemplo n.º 8
0
 internal ExtendedAudioFileStatus Write(AudioBufferList data, uint frames)
 {
     return(SafeNativeMethods.ExtAudioFileWrite(_handle, frames, ref data));
 }