本文整理汇总了C#中NAudio.Wave.WaveFileWriter.WriteSamples方法的典型用法代码示例。如果您正苦于以下问题:C# WaveFileWriter.WriteSamples方法的具体用法?C# WaveFileWriter.WriteSamples怎么用?C# WaveFileWriter.WriteSamples使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NAudio.Wave.WaveFileWriter
的用法示例。
在下文中一共展示了WaveFileWriter.WriteSamples方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Render
public static byte[] Render(Chart chart, Sound[] sounds)
{
Dictionary<long, Entry> lastNote = new Dictionary<long, Entry>();
Dictionary<int, Fraction> noteCutoff = new Dictionary<int, Fraction>();
Dictionary<int, byte[]> renderedSamples = new Dictionary<int, byte[]>();
int[] buffer = new int[0];
chart.Entries.Reverse();
foreach (Entry entry in chart.Entries)
{
if (entry.Type == EntryType.Sample)
{
lastNote[GetIdentifier(entry)] = entry;
}
else if (entry.Type == EntryType.Marker)
{
Sound sound;
if (entry.Value.Numerator > 0)
{
byte[] soundData = null;
int soundIndex = (int)entry.Value - 1;
sound = sounds[(int)entry.Value - 1];
if (renderedSamples.ContainsKey(soundIndex))
{
soundData = renderedSamples[soundIndex];
}
else if (sound != null)
{
soundData = sound.Render(1.0f);
renderedSamples[soundIndex] = soundData;
}
Fraction cutoff = new Fraction(-1, 1);
if (sound.Channel >= 0 && noteCutoff.ContainsKey(sound.Channel))
{
cutoff = noteCutoff[sound.Channel];
}
if (soundData != null)
{
Paste(soundData, ref buffer, entry.LinearOffset * chart.TickRate, cutoff * chart.TickRate);
}
if (sound.Channel >= 0)
noteCutoff[sound.Channel] = entry.LinearOffset;
}
}
}
chart.Entries.Reverse();
int length = buffer.Length;
Int16[] outputSamples = new Int16[length];
int normalization = 1;
for (int i = 0; i < length; i++)
{
// auto-normalize
int currentSample = buffer[i] / normalization;
while (currentSample > 32767 || currentSample < -32768)
{
normalization++;
currentSample = buffer[i] / normalization;
}
}
for (int i = 0; i < length; i++)
{
outputSamples[i] = (Int16)(buffer[i] / normalization);
}
using (MemoryStream mem = new MemoryStream())
{
using (WaveFileWriter writer = new WaveFileWriter(new IgnoreDisposeStream(mem), WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, 44100, 2, 44100 * 4, 4, 16)))
{
writer.WriteSamples(outputSamples, 0, length);
}
mem.Flush();
return mem.ToArray();
}
}
示例2: WriteIEEE32WaveFileMono
/// <summary>
/// Write a audio float array to a 32 bit float audio file
/// </summary>
/// <param name="outputFilePath">file path to output file</param>
/// <param name="sampleRate">sample rate</param>
/// <param name="audioData">the audio float array</param>
public static void WriteIEEE32WaveFileMono(string outputFilePath, int sampleRate, float[] audioData)
{
using (WaveFileWriter wavWriter = new WaveFileWriter(outputFilePath, WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, 1)))
{
wavWriter.WriteSamples(audioData, 0, audioData.Length);
}
}
示例3: CreateWaveFile
/// <summary>
/// Create a wavefile using audiodata
/// </summary>
/// <param name="audioData">the input audio data</param>
/// <param name="fileName">the audio file to save</param>
/// <param name="waveFormat">waveformat</param>
public static void CreateWaveFile(float[] audioData, string fileName, WaveFormat waveFormat)
{
using (WaveFileWriter writer = new WaveFileWriter(fileName, waveFormat))
{
writer.WriteSamples(audioData, 0, audioData.Length);
}
}
示例4: ChangeVolume
/// <summary>指定したパーセンテージで音量を増減させた新しいwavデータを生成します。</summary>
/// <param name="wav">元のwavデータ</param>
/// <param name="factorPercent">音量のパーセンテージ(0~200くらいを想定)</param>
/// <returns>音量を変更したあとのwavデータ</returns>
public static byte[] ChangeVolume(byte[] wav, int factorPercent)
{
var result = new byte[wav.Length];
using (var msIn = new MemoryStream(wav))
using (var fr = new WaveFileReader(msIn))
using (var msOut = new MemoryStream(result))
using (var fw = new WaveFileWriter(msOut, fr.WaveFormat))
{
var allVolumes = new List<float>();
while (fr.CurrentTime < fr.TotalTime)
{
var vs = fr.ReadNextSampleFrame();
foreach (var v in vs)
{
allVolumes.Add(v);
}
}
fw.WriteSamples(
allVolumes.Select(v => v * factorPercent / 100.0f).ToArray(),
0,
allVolumes.Count
);
}
return result;
}
示例5: HighPassFilter
public static void HighPassFilter(string innPath, string outtPath)
{
string inPath = "C:\\Users\\Administratorius\\Documents\\GitHub\\MusicEditor\\PawellsMusicEditor\\PawellsMusicEditor\\Content\\Songs\\NowEdited.WAV";
string outPath = "C:\\Users\\Administratorius\\Documents\\GitHub\\MusicEditor\\PawellsMusicEditor\\PawellsMusicEditor\\Content\\Songs\\NowEdited3.WAV";
Mp3ToWav(innPath, inPath);
WaveFileReader reader = new WaveFileReader(inPath);
var myFilter = BiQuadFilter.HighPassFilter(44F, 10F, 0.5F);
WaveFileWriter writer = new WaveFileWriter(outPath, reader.WaveFormat);
reader.Position = 0;
var endPos = reader.Length;
while (reader.Position < endPos)
{
int bytesRequired = (int)(endPos - reader.Position);
if (bytesRequired > 0)
{
float[] sample = new float[4];
for (int i = 0; i < 2; i++)
{
sample[i] = myFilter.Transform(reader.ReadNextSampleFrame()[i]);
}
writer.WriteSamples(sample, 0, 4);
}
}
reader.Dispose();
writer.Dispose();
ConvertWavToMp3(outPath, outtPath);
File.Delete(inPath);
File.Delete(outPath);
}
示例6: BuildTTS
protected override BakedTTS BuildTTS(IVoice voice, string text, double rate, int pitch)
{
var tts = RequestTTS((L2FVoice)voice, text);
if (tts == null)
return null;
//Create visemes
List<VisemeSpan> visemes = new List<VisemeSpan>();
//Convert phones to visemes and adjust time to rate
var mapper = LanguageResources.PT_PhonesToVisemes;
Viseme lastViseme = Viseme.Silence;
double totalVisemeTime = 0;
foreach (var r in tts)
{
var phones = ((JsonArray)r["phones"]).Select(t => ((JsonString)t).String);
var time = ((JsonArray) r["times"]).Select(t => Convert.ToDouble(((JsonNumber) t).Value));
var toProcess = phones.Zip(time, (s, d) => new { ph = s, entryTime = d }).ToArray();
totalVisemeTime += toProcess[0].entryTime;///rate;
lastViseme = Viseme.Silence;
for (int i = 1; i < toProcess.Length-1; i++)
{
var p = toProcess[i];
var nextTime = toProcess[i+1].entryTime;
Viseme currentViseme;
if (!mapper.TryGetValue(p.ph, out currentViseme))
currentViseme = Viseme.Silence;
if (lastViseme != currentViseme)
{
visemes.Add(new VisemeSpan() { duration = totalVisemeTime/rate, viseme = lastViseme });
lastViseme = currentViseme;
totalVisemeTime = 0;
}
var duration = nextTime - p.entryTime;
totalVisemeTime += duration;
}
}
if(totalVisemeTime>0)
visemes.Add(new VisemeSpan() { duration = totalVisemeTime/rate, viseme = lastViseme });
if (visemes.Count == 0)
return null;
//Create audio
var audioUrls = tts.Select(r => ((JsonString)r["url"]).String).ToArray();
var v = BuildAudioStream(audioUrls, rate, pitch);
const int MAX_SAMPLES = 5120;
var bufferSize = MAX_SAMPLES * v.WaveFormat.Channels;
var buffer = new float[bufferSize];
using (var m = new MemoryStream())
{
using (var w = new WaveFileWriter(m, v.WaveFormat))
{
int readed;
while ((readed = v.Read(buffer, 0, bufferSize)) > 0)
{
w.WriteSamples(buffer, 0, readed);
}
w.Flush();
return new BakedTTS() { visemes = visemes.ToArray(), waveStreamData = m.ToArray() };
}
}
}