123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using Microsoft.Xna.Framework.Audio;
- using System.IO;
- namespace VocabManager.Voice
- {
- public class SoundManager
- {
- private Microphone _currentMicrophone;
- private MemoryStream _currentRecordingStream;
- private byte[] _audioBuffer;
- private bool _stopRequested;
- private int _sampleRate = 0;
- string _fileName;
- public bool IsRecording
- {
- get;
- set;
- }
- public SoundManager(string fileName)
- {
- _fileName = fileName;
- }
- public string FileName
- {
- get
- {
- return _fileName;
- }
- }
- public SoundManager():this(DateTime.Now.ToFileTimeUtc()+".wav")
- {
- }
- public void StartRecording()
- {
- if (_currentMicrophone == null)
- {
- _currentMicrophone = Microphone.Default;
- _currentMicrophone.BufferReady += new EventHandler<EventArgs>(_currentMicrophone_BufferReady);
- _audioBuffer = new byte[_currentMicrophone.GetSampleSizeInBytes(_currentMicrophone.BufferDuration)];
- _sampleRate = _currentMicrophone.SampleRate;
- }
-
- _stopRequested = false;
- _currentRecordingStream = new MemoryStream(1048576);
- IsRecording = true;
- _currentMicrophone.Start();
- }
-
- public void RequestStopRecording()
- {
- _stopRequested = true;
- }
- void _currentMicrophone_BufferReady(object sender, EventArgs e)
- {
- _currentMicrophone.GetData(_audioBuffer);
- _currentRecordingStream.Write(_audioBuffer, 0, _audioBuffer.Length);
- if (!_stopRequested) return;
- _currentMicrophone.Stop();
- var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
- if (isoStore.FileExists(_fileName))
- isoStore.DeleteFile(_fileName);
- using (var targetFile = isoStore.CreateFile( _fileName))
- {
- WaveHeaderWriter.WriteHeader(targetFile, (int)_currentRecordingStream.Length, 1, _sampleRate);
- var dataBuffer = _currentRecordingStream.GetBuffer();
- targetFile.Write(dataBuffer, 0, (int)_currentRecordingStream.Length);
- targetFile.Flush();
- targetFile.Close();
- }
- _currentMicrophone.BufferReady -= new EventHandler<EventArgs>(_currentMicrophone_BufferReady);
-
- IsRecording = false;
- _stopRequested = false;
-
- }
- private SoundEffectInstance _currentSound = null;
- public void PlayRecording()
- {
- SoundEffect se;
- if (_currentSound != null)
- {
- _currentSound.Stop();
- _currentSound = null;
- }
- var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
- if (isoStore.FileExists(_fileName))
- {
- using (var fileStream = isoStore.OpenFile(_fileName, FileMode.Open))
- {
- se = SoundEffect.FromStream(fileStream);
- fileStream.Close();//not really needed, but it makes me feel better.
- }
- _currentSound = se.CreateInstance();
- _currentSound.Play();
- }
- }
- }
- }
|