如何播放没有文件? [英] How Can I Play without file?

查看:141
本文介绍了如何播放没有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个响警报,无负载/从文件流。

I would like to play a ring alert without loading/streaming it from file.

我可以将它嵌入在code信号或音?我使用 n音讯

Can I embed it's signal or tone in code? I am using NAudio.

我的目的是通过去除IO动作提高了性能。

My purpose is improving performance by removing IO actions.

我不希望使用嵌入的资源。 只有我想产生code环警报。

I don't want to use embedded resources. Only I want generate ring alert in code.

推荐答案

使用n音讯,你有这些选项来做到这一点:

With NAudio you have these options to do that:

  • 要播放从流(在内存中)的文件。你必须在其他的答案code这一点,其实你甚至不需要n音讯这个任务'事业 System.Media.SoundPlayer 就够了)。
  • 要生成将演奏(无需将其保存的地方)的信号。
  • 要播放声音的命令字符串(不知何故大家都做了的好旧时代的与播放在BASIC)。
  • To play a file from a stream (in memory). You have code for this in other answers, actually you do not even need NAudio for this task 'cause System.Media.SoundPlayer is enough).
  • To generate a signal that will be played (without the need to save it somewhere).
  • To play a string of sound commands (somehow as we all did on good old times with play in BASIC).

这个例子对于第一种情况来自查尔斯Petzold的文章MSDN上(但看看到的 n音讯文档太):

The example for the first case comes from a Charles Petzold article on MSDN (but take a look to NAudio documentation too):

class SineWaveOscillator : WaveProvider16 {
  double phaseAngle;

  public SineWaveOscillator(int sampleRate): 
    base(sampleRate, 1) {
  }

  public double Frequency { set; get; }
  public short Amplitude { set; get; }

  public override int Read(short[] buffer, int offset, 
    int sampleCount) {

    for (int index = 0; index < sampleCount; index++) {
      buffer[offset + index] = 
        (short)(Amplitude * Math.Sin(phaseAngle));
      phaseAngle += 
        2 * Math.PI * Frequency / WaveFormat.SampleRate;

      if (phaseAngle > 2 * Math.PI)
        phaseAngle -= 2 * Math.PI;
    }
    return sampleCount;
  }
}

然后code扮演的语气很简单:

Then code to play a tone is simple:

SineWaveOscillator osc = new SineWaveOscillator(44100);
osc.Frequency = 440;
osc.Amplitude = 8192;

WaveOut waveOut = new WaveOut();
waveOut.Init(osc);
waveOut.Play();

播放MIDI

对于第二种情况(打一串串MIDI命令)重新看看另一个的查尔斯Petzold的文章。他的 MidiStringPlayer 类提供了你需要的一切(但你可能需要去掉WPF code使用它,你想要的)。基本code播放MIDI与n音讯是:

Play MIDI

For the second case (play a string of MIDI commands) take again a look to another Charles Petzold article. His MidiStringPlayer class offers everything you need (but you may need to strip out WPF code to use it where you want). Basic code to play MIDI with NAudio is:

using (MidiOut midiOut = new MidiOut(0))
{
    midiOut.Send(MidiMessage.StartNote(60, 127, 0).RawData);
    Thread.Sleep(1000);
    midiOut.Send(MidiMessage.StopNote(60, 0, 0).RawData);
    Thread.Sleep(1000);
}

如果你真的需要从一个字符串弹奏音符,那么你必须分析它,code是太长,它张贴在这里,但在链接,你会发现一切。

If you really need to play notes from a string then you have to parse it, code is too long to post it here but in the link you'll find everything.

这篇关于如何播放没有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆