SoundFileOgg.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using UnityEngine;
  3. public class SoundFileOgg : IDisposableBase
  4. {
  5. public SoundFileOgg(AFileSystemBase fileSystem, float amp_late = 1f)
  6. {
  7. this.reader_ = new SoundReaderOGG(fileSystem);
  8. this.AmpRate = amp_late;
  9. }
  10. public void Open(string file_name, bool threeD, bool stream)
  11. {
  12. this.reader_.OpenFile(file_name);
  13. if (!this.reader_.IsOpenFile())
  14. {
  15. return;
  16. }
  17. UnityEngine.Object.DestroyImmediate(this.clip_);
  18. this.clip_ = null;
  19. int sample_length = (int)this.reader_.sample_length;
  20. this.clip_ = AudioClip.Create(file_name, sample_length, this.reader_.channels, this.reader_.sample_rate, stream, new AudioClip.PCMReaderCallback(this.OnAudioRead), new AudioClip.PCMSetPositionCallback(this.OnAudioSetPosition));
  21. }
  22. public float GetLength()
  23. {
  24. return (float)(this.reader_.sample_length / (double)this.reader_.sample_rate);
  25. }
  26. public void Close()
  27. {
  28. this.reader_.CloseFile();
  29. }
  30. public bool IsOpen()
  31. {
  32. return this.reader_.IsOpenFile();
  33. }
  34. public void Seek(int new_pos)
  35. {
  36. this.reader_.Seek(new_pos);
  37. }
  38. private void OnAudioRead(float[] data)
  39. {
  40. for (int i = 0; i < data.Length; i++)
  41. {
  42. data[i] = 0f;
  43. }
  44. this.reader_.Read(data);
  45. if (this.AmpRate != 1f)
  46. {
  47. for (int j = 0; j < data.Length; j++)
  48. {
  49. data[j] *= this.AmpRate;
  50. }
  51. }
  52. this.position_ += data.Length;
  53. }
  54. private void OnAudioSetPosition(int newPosition)
  55. {
  56. this.reader_.Seek(newPosition);
  57. this.position_ = newPosition;
  58. }
  59. protected override void DisposeEvent()
  60. {
  61. this.reader_.Dispose();
  62. this.reader_ = null;
  63. UnityEngine.Object.DestroyImmediate(this.clip_, true);
  64. this.clip_ = null;
  65. }
  66. public float AmpRate { get; set; }
  67. public AudioClip clip
  68. {
  69. get
  70. {
  71. return this.clip_;
  72. }
  73. }
  74. public int position
  75. {
  76. get
  77. {
  78. return this.position_;
  79. }
  80. }
  81. private AudioClip clip_;
  82. private SoundReaderOGG reader_;
  83. private int position_;
  84. }