Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SoundEffect.Android.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_ANDROID
4 
5 using System;
6 using System.Runtime.InteropServices;
7 
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.Mathematics;
10 using SiliconStudio.Paradox.Audio.Wave;
11 
12 namespace SiliconStudio.Paradox.Audio
13 {
14  public partial class SoundEffect
15  {
16  internal byte[] WaveDataArray;
17 
18  private GCHandle pinnedWaveData;
19 
20  private unsafe void AdaptAudioDataImpl()
21  {
22  var isStereo = WaveFormat.Channels == 2;
23 
24  // allocate the new audio buffer
25  var sampleRateRatio = Math.Min(1, WaveFormat.SampleRate / (float)SoundEffectInstance.SoundEffectInstanceFrameRate); // we don't down-sample in current version
26  var newWaveDataSize = (int)Math.Floor(WaveDataSize / sampleRateRatio) * (isStereo ? 1 : 2);
27  WaveDataArray = new byte[newWaveDataSize];
28 
29  fixed (byte* pNewWaveData = WaveDataArray)
30  {
31  // re-sample the audio data
32  if (Math.Abs(sampleRateRatio - 1f) < MathUtil.ZeroTolerance && !isStereo)
33  DuplicateTracks(WaveDataPtr, (IntPtr)pNewWaveData, newWaveDataSize);
34  else if (Math.Abs(sampleRateRatio - 0.5f) < MathUtil.ZeroTolerance)
35  UpSampleByTwo(WaveDataPtr, (IntPtr)pNewWaveData, newWaveDataSize, WaveFormat.Channels, !isStereo);
36  else
37  UpSample(WaveDataPtr, (IntPtr)pNewWaveData, newWaveDataSize, sampleRateRatio, WaveFormat.Channels, !isStereo);
38  }
39 
40  // update the wave data buffer
41  pinnedWaveData = GCHandle.Alloc(WaveDataArray, GCHandleType.Pinned);
42  WaveDataPtr = pinnedWaveData.AddrOfPinnedObject();
43  WaveDataSize = newWaveDataSize;
44 
45  // Free unused anymore C# data buffer
46  Utilities.FreeMemory(nativeDataBuffer);
47  nativeDataBuffer = IntPtr.Zero;
48  }
49 
50  private void DestroyImpl()
51  {
52  pinnedWaveData.Free();
53  }
54  }
55 }
56 
57 #endif