diondokter

Audiofile to samples C# UWP SharpDX.MediaFoundation

Oct 24th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1.         public override async Task<Sample> Import()
  2.         {
  3.             using (Stream Input = (await ImportTarget.OpenSequentialReadAsync()).AsStreamForRead())
  4.             {
  5.                 // Create the decoder and get it's samples
  6.                 using (AudioDecoder Decoder = new AudioDecoder(Input))
  7.                 {
  8.                     IEnumerable<DataPointer> PointerSamples = Decoder.GetSamples();
  9.  
  10.  
  11.                     // Extract all the bytes from the sample pointers.
  12.                     List<byte> ByteData = new List<byte>();
  13.                     unsafe
  14.                     {
  15.                         foreach (DataPointer DP in PointerSamples)
  16.                         {
  17.                             void* Pointer = DP.Pointer.ToPointer();
  18.                             for (int j = 0; j < DP.Size; j++)
  19.                             {
  20.                                 ByteData.Add(*((byte*)Pointer + j));
  21.                             }
  22.                         }
  23.                     }
  24.  
  25.                     // Convert the bytes to float values
  26.                     int BitDepth = Decoder.WaveFormat.BitsPerSample;
  27.                     int CurrentIndex = 0;
  28.  
  29.                     Func<float> ConvertFunc = null;
  30.  
  31.                     switch (BitDepth)
  32.                     {
  33.                         case 8:
  34.                             ConvertFunc = () => (sbyte)ByteData[CurrentIndex++];
  35.                             break;
  36.                         case 16:
  37.                             ConvertFunc = () =>
  38.                             {
  39.                                 byte[] Buffer = new byte[2];
  40.                                 Buffer[0] = ByteData[CurrentIndex++];
  41.                                 Buffer[1] = ByteData[CurrentIndex++];
  42.                                 return BitConverter.ToInt16(Buffer, 0);
  43.                             };
  44.                             break;
  45.                         case 24:
  46.                             ConvertFunc = () =>
  47.                             {
  48.                                 byte[] Buffer = new byte[4];
  49.                                 Buffer[1] = ByteData[CurrentIndex++];
  50.                                 Buffer[2] = ByteData[CurrentIndex++];
  51.                                 Buffer[3] = ByteData[CurrentIndex++];
  52.                                 return BitConverter.ToInt32(Buffer, 0);
  53.                             };
  54.                             break;
  55.                         case 32:
  56.                             ConvertFunc = () =>
  57.                             {
  58.                                 byte[] Buffer = new byte[4];
  59.                                 Buffer[0] = ByteData[CurrentIndex++];
  60.                                 Buffer[1] = ByteData[CurrentIndex++];
  61.                                 Buffer[2] = ByteData[CurrentIndex++];
  62.                                 Buffer[3] = ByteData[CurrentIndex++];
  63.                                 return BitConverter.ToSingle(Buffer, 0);
  64.                             };
  65.                             break;
  66.                         default:
  67.                             throw new NotSupportedException("Bitdepths other than 8, 16, 24 and 32 are not supported");
  68.                     }
  69.  
  70.                     float[] Samples = new float[ByteData.Count / (BitDepth / 8)];
  71.  
  72.                     float MaxValue = 0;
  73.  
  74.                     for (int i = 0; i < Samples.Length; i++)
  75.                     {
  76.                         Samples[i] = ConvertFunc();
  77.                         MaxValue = Math.Max(MaxValue, Math.Abs(Samples[i]));
  78.                     }
  79.  
  80.                     // Normalize all values
  81.                     for (int i = 0; i < Samples.Length; i++)
  82.                     {
  83.                         Samples[i] /= MaxValue;
  84.                     }
  85.  
  86.                     // Return my own sample
  87.                     return new LoadedSample(Samples, new AudioEncodingProperties() { Bitrate = (uint)Decoder.WaveFormat.AverageBytesPerSecond * 8, BitsPerSample = (uint)Decoder.WaveFormat.BitsPerSample, ChannelCount = (uint)Decoder.WaveFormat.Channels, SampleRate = (uint)Decoder.WaveFormat.SampleRate });
  88.                 }
  89.             }
  90.         }
Advertisement
Add Comment
Please, Sign In to add comment