public static void Skip(int roll) { if (roll >= 7 && waveOutDevice.PlaybackState.Equals(PlaybackState.Playing)) { audioFileReader.Skip(-1); } if (roll <= 1 && waveOutDevice.PlaybackState.Equals(PlaybackState.Playing)) { audioFileReader.Skip(1); } }
/// <summary> /// 截取音频后,对每个小音频获语音转文字 /// </summary> /// <param name="startMilliSecond">开始时间戳</param> /// <param name="endMilliSecond">结束时间戳</param> /// <param name="reader">音频流</param> /// <param name="i">用于创建新的文件</param> /// <param name="language">语言(zh,en)</param> /// <param name="splitTime">时间间隔</param> private void GetBaiduSpeech1(TimeSpan startMilliSecond, TimeSpan endMilliSecond, string sound_path, int i, string language, double splitTime) { try { var reader = new AudioFileReader(sound_path); //开始时间往前取startMilliSecond一半的偏移,结束时间往后取间隔时间的一半的偏移 if (i == 0) { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(startMilliSecond.TotalMilliseconds / 2); } else { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(splitTime / 2); } if (endMilliSecond < reader.TotalTime)//最后一次不用取偏移 { endMilliSecond = endMilliSecond + TimeSpan.FromMilliseconds(splitTime / 2); } TimeSpan span = endMilliSecond - startMilliSecond; if (span.TotalSeconds > 60)//超过60s,只取60秒 { span = TimeSpan.FromSeconds(60); } var trimed = reader.Skip(startMilliSecond).Take(endMilliSecond - startMilliSecond); //保存新的音频文件 string fileName = Path.GetFileNameWithoutExtension(sound_path) + "_" + i + Path.GetExtension(sound_path);//重命名文件 string newFolder = System.AppDomain.CurrentDomain.BaseDirectory + "NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/"; //重新存储到一个新的文件目录 if (!System.IO.Directory.Exists(newFolder)) { System.IO.Directory.CreateDirectory(newFolder); } string newFile = newFolder + fileName; WaveFileWriter.CreateWaveFile16(newFile, trimed); //绝对路径 string path_absolute = context.Server.MapPath("/NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/" + fileName); voiceFiles.Add(UploadFile.PostFile(path_absolute)); if (span == TimeSpan.FromSeconds(60))//音频大于60s { //小音频 var _reader = new AudioFileReader(sound_path); var _trimed = _reader.Skip(startMilliSecond).Take(span); //保存新的音频文件 string _fileName = "_" + Path.GetFileNameWithoutExtension(sound_path) + "_" + i + Path.GetExtension(sound_path);//重命名文件 string _newFile = newFolder + _fileName; WaveFileWriter.CreateWaveFile16(_newFile, _trimed); baidu_text.Add(BaiduSpeech.BaiduTranslateToText(_newFile, language, _trimed.WaveFormat.SampleRate.ToString())); } else { baidu_text.Add(BaiduSpeech.BaiduTranslateToText(newFile, language, trimed.WaveFormat.SampleRate.ToString())); } reader.Close(); reader.Dispose(); } catch (Exception ex) { LogHelper.Error(ex.Message); } }
private void trackBar1_Scroll(object sender, EventArgs e) { if (afr != null && trackBar1.Value != lastvalue) { afr.Skip((int)((trackBar1.Value - lastvalue) * 0.01f * allseconds)); } }
public void ChangeScrollBarValue(int skipTime) { Pause(); AudioFileReader.Position = 0; AudioFileReader.Skip(skipTime); Play(); }
/// <summary> /// Reads samples from this sample provider /// </summary> /// <param name="buffer">Buffer to read into</param> /// <param name="offset">Offset within buffer to write to</param> /// <param name="count">Number of samples desired</param> /// <returns>Number of samples read</returns> public int Read(float[] buffer, int offset, int count) { if (isDisposed) { return(0); } int samplesRead = 0; while (samplesRead < count) { if (soundTouch.NumberOfSamplesAvailable == 0) { var readFromSource = source.Read(sourceReadBuffer, 0, sourceReadBuffer.Length); if (readFromSource > 0) { soundTouch.PutSamples(sourceReadBuffer, readFromSource / channelCount); } else { soundTouch.Flush(); } } var desiredSampleFrames = (count - samplesRead) / channelCount; var received = soundTouch.ReceiveSamples(soundTouchReadBuffer, desiredSampleFrames) * channelCount; // use loop instead of Array.Copy due to WaveBuffer for (int n = 0; n < received; n++) { buffer[offset + samplesRead++] = soundTouchReadBuffer[n]; } } if (AutoRepeat && source.Length == source.Position) { source.Position = 0; source.Skip(StartingTime); soundTouch.ReceiveSamples(soundTouchReadBuffer, soundTouch.NumberOfSamplesAvailable); } lock (lockObject) { if (FadeState == FadeState.FadingIn) { FadeIn(buffer, offset, samplesRead); } else if (FadeState == FadeState.FadingOut) { FadeOut(buffer, offset, samplesRead); } else if (FadeState == FadeState.Silence) { ClearBuffer(buffer, offset, count); playerToStop?.Stop(); } } return(samplesRead); }
private void TimeLabelsBox_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (TimeLabelsBox.Items.Count != 0 && TimeLabelsBox.SelectedItems.Count != 0) { if (afr != null) { waveout.Stop(); waveout.PlaybackStopped += OnPlaybackStopped; afr = new AudioFileReader(inputname); waveout = new WaveOut(); waveout.Init(afr); waveout.Play(); afr.Skip(ChangeTimeFormat(TimeLabelsBox.SelectedItem.ToString())); } } } }
public void SkipSound(int timeValue) { try { if (audioFileReader != null && playbackState == CustomPlayBackState.Playing && waveOutDevice.PlaybackState == PlaybackState.Playing) { int fadeOutTime, fadeInTime; fadeOutTime = fadingValues[FadingActions.ForwardBackward]["fadeOut"]; fadeInTime = fadingValues[FadingActions.ForwardBackward]["fadeIn"]; if (fadeOutTime > 0) { DoSeekFadeOut(fadeOutTime); } audioFileReader.Skip(timeValue); if (fadeInTime > 0) { sampleProvider.BeginFadeIn(fadeInTime); } } } catch (Exception ex) { Logger.NewLog(ex); } }
/// <summary> /// 截取音频后,对每个小音频获语音转文字,一个音频对应一段文字 /// </summary> /// <param name="startMilliSecond"></param> /// <param name="endMilliSecond"></param> /// <param name="reader"></param> /// <param name="sound_path"></param> /// <param name="_name"></param> /// <param name="language"></param> /// <param name="splitTime"></param> private void GetBaiduSpeech(TimeSpan startMilliSecond, TimeSpan endMilliSecond, string sound_path, int i, string language, double splitTime) { string newFile = ""; string _newFile = ""; string pcm_newFile = ""; bool need_delete = true; //将文件保存到新的文件夹(sound_path是原音频路径,newFolder是新的小音频路径,使用完成后将上传到服务器成功的音频删除) string newFolder = System.AppDomain.CurrentDomain.BaseDirectory + "NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/"; newFolder = newFolder.Replace("/", "\\"); AudioFileReader reader = new AudioFileReader(sound_path); AudioFileReader _reader = null;//超过60s音频截取小音频 try { #region 为截取音频做准备 //开始时间往前取startMilliSecond一半的偏移,结束时间往后取间隔时间的一半的偏移 if (i == 0) { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(startMilliSecond.TotalMilliseconds / 2); } else { startMilliSecond = startMilliSecond - TimeSpan.FromMilliseconds(splitTime / 2); } if (endMilliSecond < reader.TotalTime)//最后一次不用取偏移 { endMilliSecond = endMilliSecond + TimeSpan.FromMilliseconds(splitTime / 2); } TimeSpan span = endMilliSecond - startMilliSecond; if (span.TotalSeconds > 60)//超过60s,只取50秒 { span = TimeSpan.FromSeconds(50); } var trimed = reader.Skip(startMilliSecond).Take(endMilliSecond - startMilliSecond); #endregion string fileName = Path.GetFileNameWithoutExtension(sound_path) + "_" + i + Path.GetExtension(sound_path);//重命名文件 //重新存储到一个新的文件目录 if (!System.IO.Directory.Exists(newFolder)) { System.IO.Directory.CreateDirectory(newFolder); } //拼接后的文件路径 newFile = newFolder + fileName; //截取小音频文件 WaveFileWriter.CreateWaveFile16(newFile, trimed); //上传到文件服务器 string server_path = UploadFile.PostFile(newFile); if (Util.isNotNull(server_path)) { //上传成功 voiceFiles.Add(server_path); } else { need_delete = false; //上传失败,在服务器上的路径 voiceFiles.Add(Util.getServerPath() + "/NewSoundFiles/" + Path.GetFileNameWithoutExtension(sound_path) + "/" + fileName); } //大于60s的需要再处理 if (span == TimeSpan.FromSeconds(50))//音频大于60s,只截取50s { //小音频 _reader = new AudioFileReader(sound_path); var _trimed = _reader.Skip(startMilliSecond).Take(span); //保存新的音频文件 string _fileName = "_" + Path.GetFileNameWithoutExtension(sound_path) + "_" + i + ".pcm";//重命名文件 _newFile = newFolder + _fileName; WaveFileWriter.CreateWaveFile16(_newFile, _trimed); //将音频转换为文字 //baidu_text.Add(BaiduSpeech.BaiduTranslateToText(_newFile, language, _trimed.WaveFormat.SampleRate.ToString())); baidu_text.Add(Rays.Utility.BadiAI.BaiduAI.BaiduTranslateToText(_newFile, language, trimed.WaveFormat.SampleRate.ToString())); } else { //将wav转换成pcm可以提高识别速度 pcm_newFile = NAudioHelper.GetPcmPath(newFile); //将音频转换为文字 //baidu_text.Add(BaiduSpeech.BaiduTranslateToText(pcm_newFile, language, trimed.WaveFormat.SampleRate.ToString())); baidu_text.Add(Rays.Utility.BadiAI.BaiduAI.BaiduTranslateToText(pcm_newFile, language, trimed.WaveFormat.SampleRate.ToString())); } } catch (Exception ex) { LogHelper.Error("GetBaiduSpeech:" + ex.Message); } finally { reader.Close(); reader.Dispose(); if (_reader != null) { _reader.Close(); _reader.Dispose(); } //删除文件 //if (File.Exists(_newFile)) //{ // File.Delete(_newFile); //} //if (need_delete) //{ // if (File.Exists(newFile)) // { // File.Delete(newFile); // } //} ////删除目录 //if (Directory.Exists(newFolder) && Directory.GetFiles(newFolder).Length == 0) //{ // Directory.Delete(newFolder); //} //if (File.Exists(pcm_newFile)) //{ // File.Delete(pcm_newFile); // string pcm_folder = Path.GetDirectoryName(pcm_newFile); // //删除目录 // if (Directory.Exists(pcm_folder) && Directory.GetFiles(pcm_folder).Length == 0) // { // Directory.Delete(pcm_folder); // } //} } }
public void PlaySound(string files, Guid audioDevice, int timeStart, float volume) { try { if (!CheckFiles(files)) { throw new AudioFileMissingOrInvalidException(files); } StopSound(true); // files' paths separated by a * if (files.Contains("*")) { if (tracksOrdered == null || currentTracksList != files) { currentTracksList = files; OrderTracks(); } files = GetTrack(); } else { tracksOrdered = null; } audioFileReader = new AudioFileReader(files); currentTrackPlayed = files; waveOutDevice = new DirectSoundOut(audioDevice, AudioLatency); currentAudioDevice = audioDevice; currentVolume = volume; audioFileReader.Volume = muted ? 0.0f : currentVolume; soundTouch = new SoundTouch(); SetSoundTouchSettings(soundTouch, audioFileReader); if (ResetAutoRepeat) { autoRepeat = false; } sampleProvider = new CustomSampleProvider(audioFileReader, soundTouch, autoRepeat); waveOutDevice.Init(sampleProvider); if (timeStart > 0 && timeStart < audioFileReader.TotalTime.TotalSeconds) { audioFileReader.Skip(timeStart); sampleProvider.StartingTime = timeStart; } int fadeInTime = fadingValues[FadingActions.StartSound]["fadeIn"]; if (fadeInTime > 0) { sampleProvider.BeginFadeIn(fadeInTime); } waveOutDevice.Play(); playbackState = CustomPlayBackState.Playing; NewNotification("Playing :", files); } catch (AudioFileMissingOrInvalidException ex) { MessageBox.Show(ex.MessageToUser, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(string.Format(ErrorHelper.UnexpectedErrorPlayback, files), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Logger.NewLog(ex, string.Format("Unknown playback Error. Files : \"{0}\" | Guid : \"{1}\" | Timestart : \"{2}\" | Volume : \"{3}\".", files, audioDevice.ToString(), timeStart, volume)); } }
public void Rewind(int value) { //audioFile.Position = 20000000 ; audioFile.Skip(value); }