SoundManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using Microsoft.Xna.Framework.Audio;
  12. using System.IO;
  13. namespace VocabManager.Voice
  14. {
  15. public class SoundManager
  16. {
  17. private Microphone _currentMicrophone;
  18. private MemoryStream _currentRecordingStream;
  19. private byte[] _audioBuffer;
  20. private bool _stopRequested;
  21. private int _sampleRate = 0;
  22. string _fileName;
  23. public bool IsRecording
  24. {
  25. get;
  26. set;
  27. }
  28. public SoundManager(string fileName)
  29. {
  30. _fileName = fileName;
  31. }
  32. public string FileName
  33. {
  34. get
  35. {
  36. return _fileName;
  37. }
  38. }
  39. public SoundManager():this(DateTime.Now.ToFileTimeUtc()+".wav")
  40. {
  41. }
  42. public void StartRecording()
  43. {
  44. if (_currentMicrophone == null)
  45. {
  46. _currentMicrophone = Microphone.Default;
  47. _currentMicrophone.BufferReady += new EventHandler<EventArgs>(_currentMicrophone_BufferReady);
  48. _audioBuffer = new byte[_currentMicrophone.GetSampleSizeInBytes(_currentMicrophone.BufferDuration)];
  49. _sampleRate = _currentMicrophone.SampleRate;
  50. }
  51. _stopRequested = false;
  52. _currentRecordingStream = new MemoryStream(1048576);
  53. IsRecording = true;
  54. _currentMicrophone.Start();
  55. }
  56. public void RequestStopRecording()
  57. {
  58. _stopRequested = true;
  59. }
  60. void _currentMicrophone_BufferReady(object sender, EventArgs e)
  61. {
  62. _currentMicrophone.GetData(_audioBuffer);
  63. _currentRecordingStream.Write(_audioBuffer, 0, _audioBuffer.Length);
  64. if (!_stopRequested) return;
  65. _currentMicrophone.Stop();
  66. var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
  67. if (isoStore.FileExists(_fileName))
  68. isoStore.DeleteFile(_fileName);
  69. using (var targetFile = isoStore.CreateFile( _fileName))
  70. {
  71. WaveHeaderWriter.WriteHeader(targetFile, (int)_currentRecordingStream.Length, 1, _sampleRate);
  72. var dataBuffer = _currentRecordingStream.GetBuffer();
  73. targetFile.Write(dataBuffer, 0, (int)_currentRecordingStream.Length);
  74. targetFile.Flush();
  75. targetFile.Close();
  76. }
  77. _currentMicrophone.BufferReady -= new EventHandler<EventArgs>(_currentMicrophone_BufferReady);
  78. IsRecording = false;
  79. _stopRequested = false;
  80. }
  81. private SoundEffectInstance _currentSound = null;
  82. public void PlayRecording()
  83. {
  84. SoundEffect se;
  85. if (_currentSound != null)
  86. {
  87. _currentSound.Stop();
  88. _currentSound = null;
  89. }
  90. var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
  91. if (isoStore.FileExists(_fileName))
  92. {
  93. using (var fileStream = isoStore.OpenFile(_fileName, FileMode.Open))
  94. {
  95. se = SoundEffect.FromStream(fileStream);
  96. fileStream.Close();//not really needed, but it makes me feel better.
  97. }
  98. _currentSound = se.CreateInstance();
  99. _currentSound.Play();
  100. }
  101. }
  102. }
  103. }