How to get an array of float samples from an audio file

Im developing a UWP application (for Windows 10) that works with audio data. It receives the sample buffer at the beginning as an array of samples float, the elements of which vary from -1f to 1f. I used to use NAudio.dll 1.8.0, which gives all the necessary functionality. Worked with WaveFileReader, waveBuffer.FloatBuffer, WaveFileWriter. However, when I finished this application and tried to create a version of Relese, I got this error: ILT0042: Arrays of pointer types are not currently supported: 'System.Int32 * []'.

Ive tried to solve this problem:

1) https://forums.xamarin.com/discussion/73169/uwp-10-build-fail-arrays-of-pointer-types-error

There are recommendations for removing the .dll link, but I need it.

2) I tried installing NAudio in the same version using Manage NuGet Packages, but WaveFileReader, WaveFileWriter is not available.

3) The developers of NAudio will answer ( How to save a .wav file in Windows 10 using NAudio ). I read about using AudioGraph, but I can build a floating-point array of samples only in real time, but I need to get a complete set of samples right after downloading the audio file. An example of obtaining samples during the recording or playback process: https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/audio-graphs

That's why I need help: how to get FloatBuffer to work with samples after downloading an audio file? For example, to create a sound wave or calculation for applying sound effects.

Thanks in advance.


  1. FileStream BitConverter.ToSingle(), NAudio. , Im .

    private float[] GetBufferArray()
    {
        string _path = ApplicationData.Current.LocalFolder.Path.ToString() + "/track_1.mp3";
        FileStream _stream = new FileStream(_path, FileMode.Open);
        BinaryReader _binaryReader = new BinaryReader(_stream);
        int _dataSize = _binaryReader.ReadInt32();
        byte[] _byteBuffer = _binaryReader.ReadBytes(_dataSize);
    
        int _sizeFloat = sizeof(float);
        float[] _floatBuffer = new float[_byteBuffer.Length / _sizeFloat];
        for (int i = 0, j = 0; i < _byteBuffer.Length - _sizeFloat; i += _sizeFloat, j++)
        {
            _floatBuffer[j] = BitConverter.ToSingle(_byteBuffer, i);
        }
        return _floatBuffer;
    }
    
+4
2

UWP - AudioGraph API. , Windows10.

namespace AudioGraphAPI_read_samples_from_file
{
    // App opens a file using FileOpenPicker and reads samples into array of 
    // floats using AudioGragh API
// Declare COM interface to access AudioBuffer
[ComImport]
[Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
    void GetBuffer(out byte* buffer, out uint capacity);
}

public sealed partial class MainPage : Page
{
    StorageFile mediaFile;

    AudioGraph audioGraph;
    AudioFileInputNode fileInputNode;
    AudioFrameOutputNode frameOutputNode;

    /// <summary>
    /// We are going to fill this array with audio samples
    /// This app loads only one channel 
    /// </summary>
    float[] audioData;
    /// <summary>
    /// Current position in audioData array for loading audio samples 
    /// </summary>
    int audioDataCurrentPosition = 0;

    public MainPage()
    {
        this.InitializeComponent();            
    }

    private async void Open_Button_Click(object sender, RoutedEventArgs e)
    {
        // We ask user to pick an audio file
        FileOpenPicker filePicker = new FileOpenPicker();
        filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
        filePicker.FileTypeFilter.Add(".mp3");
        filePicker.FileTypeFilter.Add(".wav");
        filePicker.FileTypeFilter.Add(".wma");
        filePicker.FileTypeFilter.Add(".m4a");
        filePicker.ViewMode = PickerViewMode.Thumbnail;
        mediaFile = await filePicker.PickSingleFileAsync();

        if (mediaFile == null)
        {
            return;
        }

        // We load samples from file
        await LoadAudioFromFile(mediaFile);

        // We wait 5 sec
        await Task.Delay(5000);

        if (audioData == null)
        {
            ShowMessage("Error loading samples");
            return;
        }

        // After LoadAudioFromFile method finished we can use audioData
        // For example we can find max amplitude
        float max = audioData[0];
        for (int i = 1; i < audioData.Length; i++)
            if (Math.Abs(audioData[i]) > Math.Abs(max))
                max = audioData[i];
        ShowMessage("Maximum is " + max.ToString());
    }

    private async void ShowMessage(string Message)
    {
        var dialog = new MessageDialog(Message);
        await dialog.ShowAsync();
    }

    private async Task LoadAudioFromFile(StorageFile file)
    {
        // We initialize an instance of AudioGraph
        AudioGraphSettings settings = 
            new AudioGraphSettings(
                Windows.Media.Render.AudioRenderCategory.Media
                );
        CreateAudioGraphResult result1 = await AudioGraph.CreateAsync(settings);
        if (result1.Status != AudioGraphCreationStatus.Success)
        {
            ShowMessage("AudioGraph creation error: " + result1.Status.ToString());
        }
        audioGraph = result1.Graph;

        if (audioGraph == null)
            return;

        // We initialize FileInputNode
        CreateAudioFileInputNodeResult result2 = 
            await audioGraph.CreateFileInputNodeAsync(file);
        if (result2.Status != AudioFileNodeCreationStatus.Success)
        {
            ShowMessage("FileInputNode creation error: " + result2.Status.ToString());
        }
        fileInputNode = result2.FileInputNode;

        if (fileInputNode == null)
            return;

        // We read audio file encoding properties to pass them to FrameOutputNode creator
        AudioEncodingProperties audioEncodingProperties = fileInputNode.EncodingProperties;

        // We initialize FrameOutputNode and connect it to fileInputNode
        frameOutputNode = audioGraph.CreateFrameOutputNode(audioEncodingProperties);
        fileInputNode.AddOutgoingConnection(frameOutputNode);

        // We add a handler achiving the end of a file
        fileInputNode.FileCompleted += FileInput_FileCompleted;
        // We add a handler which will transfer every audio frame into audioData 
        audioGraph.QuantumStarted += AudioGraph_QuantumStarted;

        // We initialize audioData
        int numOfSamples = (int)Math.Ceiling(
            (decimal)0.0000001
            * fileInputNode.Duration.Ticks
            * fileInputNode.EncodingProperties.SampleRate
            );
        audioData = new float[numOfSamples];

        audioDataCurrentPosition = 0;

        // We start process which will read audio file frame by frame
        // and will generated events QuantumStarted when a frame is in memory
        audioGraph.Start();

    }

    private void FileInput_FileCompleted(AudioFileInputNode sender, object args)
    {
        audioGraph.Stop();
    }

    private void AudioGraph_QuantumStarted(AudioGraph sender, object args)
    {
        AudioFrame frame = frameOutputNode.GetFrame();
        ProcessInputFrame(frame);

    }

    unsafe private void ProcessInputFrame(AudioFrame frame)
    {
        using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Read))
        using (IMemoryBufferReference reference = buffer.CreateReference())
        {
            // We get data from current buffer
            ((IMemoryBufferByteAccess)reference).GetBuffer(
                out byte* dataInBytes,
                out uint capacityInBytes
                );
            // We discard first frame; it full of zeros because of latency
            if (audioGraph.CompletedQuantumCount == 1) return;

            float* dataInFloat = (float*)dataInBytes;
            uint capacityInFloat = capacityInBytes / sizeof(float);
            // Number of channels defines step between samples in buffer
            uint step = fileInputNode.EncodingProperties.ChannelCount;
            // We transfer audio samples from buffer into audioData
            for (uint i = 0; i < capacityInFloat; i += step)
            {
                if (audioDataCurrentPosition < audioData.Length)
                {
                    audioData[audioDataCurrentPosition] = dataInFloat[i];
                    audioDataCurrentPosition++;
                }
            }
        }
    }
}

}

. ,

+1

AudioData Wav.

PI wav , Ive wav float UWP. (, ), wav AudioGraph. . value1263424842 544501094. . Ive . , AudioGraph wav , - PCM. , , . , , - ? PI Ive . :

           using (FileStream fs = File.Open(filename, FileMode.Open))
            {
                BinaryReader reader = new BinaryReader(fs);

                int chunkID = reader.ReadInt32();
                int fileSize = reader.ReadInt32();
                int riffType = reader.ReadInt32();
                int fmtID;

                long _position = reader.BaseStream.Position;
                while (_position != reader.BaseStream.Length-1)
                {
                    reader.BaseStream.Position = _position;
                    int _fmtId = reader.ReadInt32();
                    if (_fmtId == 544501094) {
                        fmtID = _fmtId;
                        break;
                    }
                    _position++;
                }
                int fmtSize = reader.ReadInt32();
                int fmtCode = reader.ReadInt16();

                int channels = reader.ReadInt16();
                int sampleRate = reader.ReadInt32();
                int byteRate = reader.ReadInt32();
                int fmtBlockAlign = reader.ReadInt16();
                int bitDepth = reader.ReadInt16();

                int fmtExtraSize;
                if (fmtSize == 18)
                {
                    fmtExtraSize = reader.ReadInt16();
                    reader.ReadBytes(fmtExtraSize);
                }

                int dataID = reader.ReadInt32();
                int dataSize = reader.ReadInt32();

                byte[] byteArray = reader.ReadBytes(dataSize);

                int bytesForSamp = bitDepth / 8;
                int samps = dataSize / bytesForSamp;

                float[] asFloat = null;
                switch (bitDepth)
                {
                    case 16:
                        Int16[] asInt16 = new Int16[samps];
                        Buffer.BlockCopy(byteArray, 0, asInt16, 0, dataSize);
                        IEnumerable<float> tempInt16 =
                            from i in asInt16
                            select i / (float)Int16.MaxValue;
                        asFloat = tempInt16.ToArray();
                        break;
                    default:
                        return false;
                }

                //For one channel wav audio
                floatLeftBuffer.AddRange(asFloat);

. wav , . , AudioGraph - https://docs.microsoft.com/ru-ru/windows/uwp/audio-video-camera/audio-graphs. , AudioEncodingQuality recirdung MIC .

AudioData NAudio Nugget.

MediaFoundationReader.

        float[] floatBuffer;
        using (MediaFoundationReader media = new MediaFoundationReader(path))
        {
            int _byteBuffer32_length = (int)media.Length * 2;
            int _floatBuffer_length = _byteBuffer32_length / sizeof(float);

            IWaveProvider stream32 = new Wave16ToFloatProvider(media);
            WaveBuffer _waveBuffer = new WaveBuffer(_byteBuffer32_length);
            stream32.Read(_waveBuffer, 0, (int)_byteBuffer32_length);
            floatBuffer = new float[_floatBuffer_length];

            for (int i = 0; i < _floatBuffer_length; i++) {
                floatBuffer[i] = _waveBuffer.FloatBuffer[i];
            }
        }

, :

  • 1/1 000 000. , ( , );
  • AudioData MP3.

, .

0

Source: https://habr.com/ru/post/1670918/


All Articles