public WaveWriter(AudioFile AF, string TargetFile) { InitializeComponent(); this.prbBytesWritten.Select(); this.txtSource.Text = AF.Path; this.txtTarget.Text = TargetFile; this.AF = AF; this.AFS = AF.OpenStream(false); this.FS = new FileStream(TargetFile, FileMode.Create, FileAccess.Write); }
public AudioFileStream OpenStream(bool AddWAVHeader) { if (this.Type_ == AudioFileType.Unknown || this.Header_ == null) { return(null); } if (!AudioFileStream.IsFormatSupported(this.Header_.SampleFormat)) { return(null); } return(new AudioFileStream(this.Path_, this.Header_, AddWAVHeader)); }
private void StopPlayback() { lock (this) { if (this.CurrentBuffer != null) { this.CurrentBuffer.Stop(); this.CurrentBuffer.Dispose(); this.CurrentBuffer = null; } if (this.CurrentStream != null) { this.CurrentStream.Close(); this.CurrentStream = null; } this.AudioUpdateThread = null; } this.btnPause.Enabled = false; this.btnPause.Text = "Pa&use"; this.btnStop.Enabled = false; this.AudioIsLooping = false; }
private void PlayFile(FileInfo FI) { lock (this) { if (this.DS == null) { this.DS = new DirectSound(); this.DS.SetCooperativeLevel(this.Handle, CooperativeLevel.Normal); } this.StopPlayback(); var bd = new SoundBufferDescription { Format = new WaveFormat(FI.AudioFile.SampleRate, 16, FI.AudioFile.Channels), BufferBytes = this.AudioBufferSize, Flags = BufferFlags.GlobalFocus | BufferFlags.StickyFocus | BufferFlags.ControlVolume | BufferFlags.GetCurrentPosition2 | BufferFlags.ControlPositionNotify }; this.CurrentBuffer = new SecondarySoundBuffer(this.DS, bd); if (this.AudioUpdateTrigger == null) this.AudioUpdateTrigger = new AutoResetEvent(false); var chunkSize = this.AudioBufferSize / this.AudioBufferMarkers; var updatePositions = new NotificationPosition[this.AudioBufferMarkers]; for (var i = 0; i < this.AudioBufferMarkers; ++i) { updatePositions[i] = new NotificationPosition() { WaitHandle = this.AudioUpdateTrigger, Offset = chunkSize * i }; } this.CurrentBuffer.SetNotificationPositions(updatePositions); this.CurrentStream = FI.AudioFile.OpenStream(); { var bytes = new byte[this.CurrentBuffer.Capabilities.BufferBytes]; var readbytes = this.CurrentStream.Read(bytes, 0, this.CurrentBuffer.Capabilities.BufferBytes); if (readbytes < this.CurrentBuffer.Capabilities.BufferBytes) Array.Clear(bytes, readbytes, this.CurrentBuffer.Capabilities.BufferBytes - readbytes); DataStream audiodata2; var audiodata1 = this.CurrentBuffer.Lock(0, this.CurrentBuffer.Capabilities.BufferBytes, LockFlags.EntireBuffer, out audiodata2); audiodata1.Write(bytes, 0, this.CurrentBuffer.Capabilities.BufferBytes); this.CurrentBuffer.Unlock(audiodata1, audiodata2); } if (this.CurrentStream.Position < this.CurrentStream.Length) { this.AudioUpdateTrigger.Reset(); this.AudioUpdateThread = new Thread(this.AudioUpdate); this.AudioUpdateThread.Start(); this.btnPause.Enabled = true; this.btnStop.Enabled = true; this.AudioIsLooping = true; } else { this.CurrentStream.Close(); this.CurrentStream = null; this.AudioIsLooping = false; } this.CurrentBuffer.Play(0, (this.AudioIsLooping ? PlayFlags.Looping : PlayFlags.None)); } }
private void PlayFile(FileInfo FI) { lock (this) { if (this.AudioDevice == null) { this.AudioDevice = new Device(); AudioDevice.SetCooperativeLevel(this, CooperativeLevel.Normal); } this.StopPlayback(); WaveFormat fmt = new WaveFormat(); fmt.FormatTag = WaveFormatTag.Pcm; fmt.Channels = FI.AudioFile.Channels; fmt.SamplesPerSecond = FI.AudioFile.SampleRate; fmt.BitsPerSample = 16; fmt.BlockAlign = (short)(FI.AudioFile.Channels * (fmt.BitsPerSample / 8)); fmt.AverageBytesPerSecond = fmt.SamplesPerSecond * fmt.BlockAlign; BufferDescription BD = new BufferDescription(fmt); BD.BufferBytes = this.AudioBufferSize; BD.GlobalFocus = true; BD.StickyFocus = true; if (this.chkBufferedPlayback.Checked) { BD.ControlPositionNotify = true; this.CurrentBuffer = new SecondaryBuffer(BD, this.AudioDevice); if (this.AudioUpdateTrigger == null) { this.AudioUpdateTrigger = new AutoResetEvent(false); } int ChunkSize = this.AudioBufferSize / this.AudioBufferMarkers; BufferPositionNotify[] UpdatePositions = new BufferPositionNotify[this.AudioBufferMarkers]; for (int i = 0; i < this.AudioBufferMarkers; ++i) { UpdatePositions[i] = new BufferPositionNotify(); UpdatePositions[i].EventNotifyHandle = this.AudioUpdateTrigger.SafeWaitHandle.DangerousGetHandle(); UpdatePositions[i].Offset = ChunkSize * i; } Notify N = new Notify(this.CurrentBuffer); N.SetNotificationPositions(UpdatePositions); this.CurrentStream = FI.AudioFile.OpenStream(); this.CurrentBuffer.Write(0, this.CurrentStream, this.CurrentBuffer.Caps.BufferBytes, LockFlag.EntireBuffer); if (this.CurrentStream.Position < this.CurrentStream.Length) { this.AudioUpdateTrigger.Reset(); this.AudioUpdateThread = new Thread(new ThreadStart(this.AudioUpdate)); this.AudioUpdateThread.Start(); this.btnPause.Enabled = true; this.btnStop.Enabled = true; this.AudioIsLooping = true; } else { this.CurrentStream.Close(); this.CurrentStream = null; this.AudioIsLooping = false; } } else { this.CurrentStream = FI.AudioFile.OpenStream(true); this.CurrentBuffer = new SecondaryBuffer(this.CurrentStream, BD, this.AudioDevice); this.btnPause.Enabled = true; this.btnStop.Enabled = true; } this.CurrentBuffer.Play(0, (this.AudioIsLooping ? BufferPlayFlags.Looping : BufferPlayFlags.Default)); } }