波形文件的时间长度
Length in time of a wave file
我有一些使用 TTS 生成 wave 文件然后播放的简单代码:
public void TestSpeech()
{
SpeechSynthesizer synth = new SpeechSynthesizer();
using (MemoryStream stream = new MemoryStream())
{
synth.SetOutputToWaveStream(stream);
synth.Speak("Hello world");
stream.Seek(0, SeekOrigin.Begin);
IWaveSource source = new WaveFileReader(stream);
EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
var soundOut = new WasapiOut();
soundOut.Initialize(source);
soundOut.Stopped += (s, e) => waitHandle.Set();
soundOut.Play();
waitHandle.WaitOne();
soundOut.Dispose();
source.Dispose();
}
}
一切正常,但我想在开始播放波形文件之前知道它会持续多长时间。是否有某种计算方法,或者在某处可用?
如果某处可用,它是如何计算的?我假设它与流中的数据量有关,但如何?
CSCore(摘自this sample, current playback position and total duration are used here):
using System;
using CSCore;
using CSCore.Codecs.WAV;
IWaveSource wavSource = new WaveFileReader(stream);
TimeSpan totalTime = wavSource.GetLength();
NAudio:
using System;
using NAudio.Wave;
using (var wfr = new WaveFileReader(stream))
{
TimeSpan totalTime = wfr.TotalTime;
}
另见 the MSDN docs for TimeSpan。
持续时间是根据 WAVE 数据的总长度(可以是压缩文件的估计值)和每秒平均字节数(根据 属性 中的 NAudio source TotalTime
):
totalTimeInSeconds = LengthInBytes / AverageBytesPerSecond;
using CSCore;
IWaveSource waveSource = new WaveFileReader(stream);
TimeSpan totalTime = waveSource.GetLength( ); //get length returns a timespan
如果有人正在寻找解决方法,我会这样处理:
(请原谅我的第一个 Whosebug 评论)
- 我创建了 bool mouseScrewsAround = false
- 计时器事件,在播放时改变 trackBar 的位置,仅在 !mouseScrewsAround
时触发
- trackBar_MouseDown -> mouseScrewsAround = true
- trackBar_MouseUp -> 改变轨道位置,然后 mouseScrewsAround = false
我有一些使用 TTS 生成 wave 文件然后播放的简单代码:
public void TestSpeech()
{
SpeechSynthesizer synth = new SpeechSynthesizer();
using (MemoryStream stream = new MemoryStream())
{
synth.SetOutputToWaveStream(stream);
synth.Speak("Hello world");
stream.Seek(0, SeekOrigin.Begin);
IWaveSource source = new WaveFileReader(stream);
EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
var soundOut = new WasapiOut();
soundOut.Initialize(source);
soundOut.Stopped += (s, e) => waitHandle.Set();
soundOut.Play();
waitHandle.WaitOne();
soundOut.Dispose();
source.Dispose();
}
}
一切正常,但我想在开始播放波形文件之前知道它会持续多长时间。是否有某种计算方法,或者在某处可用?
如果某处可用,它是如何计算的?我假设它与流中的数据量有关,但如何?
CSCore(摘自this sample, current playback position and total duration are used here):
using System;
using CSCore;
using CSCore.Codecs.WAV;
IWaveSource wavSource = new WaveFileReader(stream);
TimeSpan totalTime = wavSource.GetLength();
NAudio:
using System;
using NAudio.Wave;
using (var wfr = new WaveFileReader(stream))
{
TimeSpan totalTime = wfr.TotalTime;
}
另见 the MSDN docs for TimeSpan。
持续时间是根据 WAVE 数据的总长度(可以是压缩文件的估计值)和每秒平均字节数(根据 属性 中的 NAudio source TotalTime
):
totalTimeInSeconds = LengthInBytes / AverageBytesPerSecond;
using CSCore;
IWaveSource waveSource = new WaveFileReader(stream);
TimeSpan totalTime = waveSource.GetLength( ); //get length returns a timespan
如果有人正在寻找解决方法,我会这样处理: (请原谅我的第一个 Whosebug 评论)
- 我创建了 bool mouseScrewsAround = false
- 计时器事件,在播放时改变 trackBar 的位置,仅在 !mouseScrewsAround 时触发
- trackBar_MouseDown -> mouseScrewsAround = true
- trackBar_MouseUp -> 改变轨道位置,然后 mouseScrewsAround = false