public void PlaySound(Tone tone, TimeSpan duration) { Deployment.Current.Dispatcher.BeginInvoke(() => { if (_timer == null) { _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(33) }; _timer.Tick += delegate { try { FrameworkDispatcher.Update(); } catch { } }; } if (_timer.IsEnabled) _timer.Stop(); _timeLeft = duration; FrameworkDispatcher.Update(); _frequency = tone; _dynamicSound = new DynamicSoundEffectInstance(SampleRate, AudioChannels.Mono); _dynamicSound.BufferNeeded += dynamicSound_BufferNeeded; _dynamicSound.Play(); _bufferSize = _dynamicSound.GetSampleSizeInBytes(TimeSpan.FromSeconds(1)); _soundBuffer = new byte[_bufferSize]; _timer.Start(); }); }
/// <summary> /// Plays the effect. /// </summary> /// <param name="asEffect">Set to false for music, true for sound effects.</param> public void Play(bool asEffect = true) { double now = UltimaGame.TotalMS; CullExpiredEffects(now); m_ThisInstance = GetNewInstance(asEffect); if (m_ThisInstance == null) { this.Dispose(); return; } BeforePlay(); byte[] buffer = GetBuffer(); if (buffer != null && buffer.Length > 0) { m_ThisInstance.BufferNeeded += new EventHandler<EventArgs>(OnBufferNeeded); m_ThisInstance.SubmitBuffer(buffer); m_ThisInstance.Play(); List<Tuple<DynamicSoundEffectInstance, double>> list = (asEffect) ? m_EffectInstances : m_MusicInstances; list.Add(new Tuple<DynamicSoundEffectInstance, double>(m_ThisInstance, now + (m_ThisInstance.GetSampleDuration(buffer.Length).Milliseconds))); } }
// Constructor public MainPage() { InitializeComponent(); DispatcherTimer timer = new DispatcherTimer(); // Run it a little faster than our buffer updates timer.Interval = TimeSpan.FromMilliseconds(80); timer.Tick += OnTimerTick; timer.Start(); FrameworkDispatcher.Update(); // Add in the event handler for when a new buffer of audio is available audioIn.BufferReady += new EventHandler<EventArgs>(Microphone_BufferReady); // XNA is limited to 100ms latency. :( audioIn.BufferDuration = TimeSpan.FromMilliseconds(100); // Create a buffer of the appropriate length int bufferLen = audioIn.GetSampleSizeInBytes(audioIn.BufferDuration); audioBuffer = new byte[bufferLen]; // Create our audio out interface with the same samplerate and channels of the audio input // We couldn't create this above because we needed to get audioIn.SampleRate audioOut = new DynamicSoundEffectInstance(audioIn.SampleRate, AudioChannels.Mono); // Start recording and playing audioIn.Start(); audioOut.Play(); }
/// <summary> /// Plays the effect. /// </summary> /// <param name="asEffect">Set to false for music, true for sound effects.</param> public void Play(bool asEffect, AudioEffects effect = AudioEffects.None, float volume = 1.0f) { double now = UltimaGame.TotalMS; CullExpiredEffects(now); m_ThisInstance = GetNewInstance(asEffect); if (m_ThisInstance == null) { this.Dispose(); return; } switch (effect) { case AudioEffects.PitchVariation: float pitch = (float)Utility.RandomValue(-5, 5) * .025f; m_ThisInstance.Pitch = pitch; break; } BeforePlay(); byte[] buffer = GetBuffer(); if (buffer != null && buffer.Length > 0) { m_ThisInstance.BufferNeeded += new EventHandler<EventArgs>(OnBufferNeeded); m_ThisInstance.SubmitBuffer(buffer); m_ThisInstance.Volume = volume; m_ThisInstance.Play(); List<Tuple<DynamicSoundEffectInstance, double>> list = (asEffect) ? m_EffectInstances : m_MusicInstances; double ms = m_ThisInstance.GetSampleDuration(buffer.Length).TotalMilliseconds; list.Add(new Tuple<DynamicSoundEffectInstance, double>(m_ThisInstance, now + ms)); } }
public void Dispose() { if (source != null) source.Dispose(); source = null; musicStream = null; }
public void PlaySound(int samplingRate, byte[] pcmData) { DynamicSoundEffectInstance playback = new DynamicSoundEffectInstance(samplingRate, AudioChannels.Mono); playback.SubmitBuffer(pcmData); playback.Play(); }
public XnaAudioDriver() { _audioFormat = new AudioFormat(44100); _buffer = new byte[13230 * 2]; _dsei = new DynamicSoundEffectInstance(_audioFormat.SampleRate, _audioFormat.Channels == 2 ? AudioChannels.Stereo : AudioChannels.Mono); _dsei.BufferNeeded += OnBufferNeeded; }
public NewButtonPage() { InitializeComponent(); // Create new Microphone and set event handler buttonMic = Microphone.Default; buttonMic.BufferReady += OnMicrophoneBufferReady; buttonPlayback = new DynamicSoundEffectInstance(buttonMic.SampleRate, AudioChannels.Mono); }
public SoundReversibleInstance(SoundReversible sound, byte[] audioBytes, int sampleRate, AudioChannels channels, bool inReverse) { this.sound = sound; this.sampleRate = sampleRate; this.channels = channels; reversed = inReverse; baseAudioBytes = audioBytes; dynamicSound = NewDynamicSoundEffectInstance(); count = dynamicSound.GetSampleSizeInBytes(TimeSpan.FromMilliseconds(BUFFER_CHUNK_SIZE)); }
public OggSong(string oggFile) { reader = new VorbisReader(oggFile); effect = new DynamicSoundEffectInstance(reader.SampleRate, (AudioChannels)reader.Channels); buffer = new byte[effect.GetSampleSizeInBytes(TimeSpan.FromMilliseconds(500))]; nvBuffer = new float[buffer.Length / 2]; // when a buffer is needed, set our handle so the helper thread will read in more data effect.BufferNeeded += (s, e) => readNextBuffer(); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load<SpriteFont>("Fonts/Default"); var waveFileStream = TitleContainer.OpenStream(@"Content\Sounds\48K16BSLoop.wav"); streamingWave = new StreamingWave(waveFileStream, TimeSpan.FromMilliseconds(100)); dynamicSound = streamingWave.DynamicSound; }
// コンストラクター public MainPage() { InitializeComponent(); microphone = Microphone.Default; microphone.BufferReady += OnMicrophoneBufferReady; playback = new DynamicSoundEffectInstance(microphone.SampleRate, AudioChannels.Mono); playback.BufferNeeded += OnPlaybackBufferNeeded; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here _dynamicSound = new DynamicSoundEffectInstance(48000, AudioChannels.Mono, 32); _audioCapture = new AudioCapture(); _audioCapture.BufferReady += BufferReady; _audioCapture.Start(); base.Initialize(); }
/// <summary> /// Constructor. /// </summary> public XnaAudio() { // Event handler for getting audio data when the buffer is full microphone.BufferDuration = TimeSpan.FromMilliseconds(100); microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady); // initialize dynamic sound effect instance playback = new DynamicSoundEffectInstance(microphone.SampleRate, AudioChannels.Mono); playback.BufferNeeded += GetSamples; sampleSize = playback.GetSampleSizeInBytes(TimeSpan.FromMilliseconds(100)); }
public void Dispose() { if (m_ThisInstance != null) { if (!m_ThisInstance.IsDisposed) { m_ThisInstance.Stop(); m_ThisInstance.Dispose(); } m_ThisInstance = null; } }
public void Dispose() { if (m_Playing) { Stop(); } m_Instance.Dispose(); m_Instance = null; m_Stream.Close(); m_Stream = null; }
public Game1() { DSEI = new DynamicSoundEffectInstance(44100, AudioChannels.Mono); if (!isMicrophoneRecording) { // we are starting to record Microphone.Default.Start(); } isMicrophoneRecording = !isMicrophoneRecording; }
public void Dispose() { if (m_ThisInstance != null) { m_ThisInstance.BufferNeeded -= OnBufferNeeded; if (!m_ThisInstance.IsDisposed) { m_ThisInstance.Stop(); m_ThisInstance.Dispose(); } m_ThisInstance = null; } }
public MP3Player(string path) { Stream = new Mp3Stream(path); Stream.DecodeFrames(1); //let's get started... DecodeNext = new AutoResetEvent(true); BufferDone = new AutoResetEvent(false); Inst = new DynamicSoundEffectInstance(Stream.Frequency, AudioChannels.Stereo); Inst.IsLooped = false; Inst.BufferNeeded += SubmitBufferAsync; SubmitBuffer(null, null); SubmitBuffer(null, null); NextBuffers = new List<byte[]>(); NextSizes = new List<int>(); Requests = 1; MainThread = Thread.CurrentThread; DecoderThread = new Thread(() => { try { while (MainThread.IsAlive) { DecodeNext.WaitOne(128); bool go; lock (this) go = Requests > 0; while (go) { var buf = new byte[524288]; var read = Stream.Read(buf, 0, buf.Length); lock (this) { Requests--; NextBuffers.Add(buf); NextSizes.Add(read); if (read == 0) { EndOfStream = true; return; } BufferDone.Set(); } lock (this) go = Requests > 0; } } } catch (Exception e) { } }); DecoderThread.Start(); }
public SoundPlayer(int sampleRate, bool stereo, int framesPerSecond) { soundInstance = new DynamicSoundEffectInstance(sampleRate, stereo ? AudioChannels.Stereo : AudioChannels.Mono); stream_buffer_size = (uint)(((ulong)MAX_BUFFER_SIZE * (ulong)sampleRate) / 44100); var wBitsPerSample = 16; var nChannels = stereo ? 2 : 1; var nBlockAlign = wBitsPerSample * nChannels / 8; stream_buffer_size = (uint)(stream_buffer_size * nBlockAlign) / 4; stream_buffer_size = (uint)((stream_buffer_size * 30) / framesPerSecond); stream_buffer_size = (stream_buffer_size / 1024) * 1024; waveBuffer = new byte[stream_buffer_size];//soundInstance.GetSampleSizeInBytes(TimeSpan.FromMilliseconds(25))]; soundInstance.Play(); }
public PCMStreamPlayer() { audioBufferList = new byte[AudioBufferCount][]; for (int i = 0; i < audioBufferList.Length; ++i) { audioBufferList[i] = new byte[ChannelCount * BytesPerSample * AudioBufferSize]; } if (dynamicSound != null) { dynamicSound.Stop(); dynamicSound.Dispose(); } dynamicSound = new DynamicSoundEffectInstance(44100, AudioChannels.Stereo); dynamicSound.BufferNeeded += BufferNeeded; }
private static void PlayThings(Context ctx, Node srcNode) { FrameworkDispatcher.Update(); using (var aud = new DynamicSoundEffectInstance(48000, AudioChannels.Mono)) { SampleSize = aud.GetSampleSizeInBytes(new TimeSpan(0, 0, 0, 0, SampleTimeMs)); var sub = ctx.Socket(SocketType.SUB); sub.Subscribe(new byte[0]); sub.Connect(srcNode.Url); mBar.SignalAndWait(); bool bufferNeeded = true; aud.BufferNeeded += (_, __) => bufferNeeded = true; while (isRunning) { if (bufferNeeded) { foreach (var n in mNodes.Where(n => n is GeneratorNode).Cast<GeneratorNode>()) { n.SignalForData(); } bufferNeeded = false; } var bytes = sub.Recv(SampleTimeMs / 2); if (bytes != null) { aud.SubmitBuffer(bytes); if (aud.State == SoundState.Stopped) aud.Play(); } FrameworkDispatcher.Update(); //running = false; } aud.Stop(); sub.Dispose(); } }
public Synth() { // Create DynamicSoundEffectInstance object and start it _instance = new DynamicSoundEffectInstance(SampleRate, Channels == 2 ? AudioChannels.Stereo : AudioChannels.Mono); _instance.Play(); // Create buffers const int bytesPerSample = 2; _xnaBuffer = new byte[Channels * SamplesPerBuffer * bytesPerSample]; _workingBuffer = new float[Channels, SamplesPerBuffer]; // Create voice structures _voicePool = new Voice[Polyphony]; for (int i = 0; i < Polyphony; ++i) { _voicePool[i] = new Voice(this); } _freeVoices = new Stack<Voice>(_voicePool); _activeVoices = new List<Voice>(); _keyRegistry = new Dictionary<int, Voice>(); }
/// <summary> /// Constructor: Setup RTTY and Sound settings. /// </summary> /// <param name="frequency">The frequency of the sine wave.</param> /// <param name="samplerate">The sample rate of the sound.</param> /// <param name="baud">The baud rate of the data transmission.</param> /// <param name="shift">The eventual radio frequency shift in Hz. (Not used)</param> /// <param name="low">The amplitude for a 'low' bit. 0.0->1.0</param> /// <param name="high">The amplitude for a 'high' bit. 0.0->1.0</param> /// <param name="stopbits">The number of stop bits for each byte.</param> public RTTY(double frequency = 3000, int samplerate = 42000, int baud = 300, int shift = 425, double low = 0.1, double high = 1.0, int stopbits = 2) { _sampleRate = samplerate; _frequency = frequency; _timechange = _frequency * (1.0d / _sampleRate); _baudrate = baud; _shift = shift; _stopBits = stopbits; lowVolume = low; highVolume = high; _BufferLength = (int)((1d/(double)_baudrate) * 11d * (double)_sampleRate * 2d); _BitLength = _BufferLength / 11 /2; System.Diagnostics.Debug.WriteLine(_BitLength); _FloatBuffer = new double[_BufferLength]; _ByteBuffer = new byte[_BufferLength * 2]; _dynamicSound = new DynamicSoundEffectInstance(_sampleRate, AudioChannels.Mono); _dynamicSound.BufferNeeded += BufferNeeded; _dynamicSound.Volume = 1.0f; SoundEffect.MasterVolume = 1.0f; }
public MainPage() { InitializeComponent(); // Create new Microphone and set event handler microphone = Microphone.Default; microphone.BufferReady += OnMicrophoneBufferReady; // Create new DynamicSoundEffectInstace for playback playback = new DynamicSoundEffectInstance(microphone.SampleRate, AudioChannels.Mono); playback.BufferNeeded += OnPlaybackBufferNeeded; // Enumerate existing memo waveform files in isolated storage using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) { // Show filenames with most recent first string[] filenames = storage.GetFileNames(); Array.Sort(filenames); Array.Reverse(filenames); foreach (string filename in filenames) { using (IsolatedStorageFileStream stream = storage.OpenFile(filename, FileMode.Open, FileAccess.Read)) { TimeSpan duration = microphone.GetSampleDuration((int)stream.Length); MemoInfo memoInfo = new MemoInfo(filename, stream.Length, duration); memoFiles.Add(memoInfo); } } } // Set memo collection to ListBox memosListBox.ItemsSource = memoFiles; // Set-up record button recordButton.DataContext = spaceTime; UpdateRecordButton(false); }
public static void WarmupFAudio() { var dummy = new Microsoft.Xna.Framework.Audio.DynamicSoundEffectInstance(44000, Microsoft.Xna.Framework.Audio.AudioChannels.Stereo); }
public void Play() { float now = UltimaVars.EngineVars.TheTime; // Check to see if any existing instances of this sound effect have stopped playing. If // they have, remove the reference to them so the garbage collector can collect them. for (int i = 0; i < m_instances.Count; i++) if (m_instances[i].Item2 < now) { m_instances.RemoveAt(i); i--; } DynamicSoundEffectInstance instance = new DynamicSoundEffectInstance(22050, AudioChannels.Mono); instance.BufferNeeded += new EventHandler<EventArgs>(instance_BufferNeeded); instance.SubmitBuffer(m_waveBuffer); instance.Play(); m_instances.Add(new Tuple<DynamicSoundEffectInstance, float>(instance, now + (instance.GetSampleDuration(m_waveBuffer.Length).Milliseconds / 1000f))); }
public void Dispose() { if (mSoundEffectInstance != null) { mSoundEffectInstance.Dispose(); } mOnBufferNeededCallback = null; mSoundEffectInstance = null; }
public void Stop() { if (_dynamicSound != null) { lock (DynamicSoundSync) { if (_dynamicSound != null) { IsPlaying = false; _dynamicSound.Stop(); _dynamicSound = null; } } } }
public void Play() { lock (DynamicSoundSync) { if (_dynamicSound == null) { IsPlaying = true; Time = 0; CurrentFillBufferIndex = 0; CurrentPlayBufferIndex = 0; _dynamicSound = new DynamicSoundEffectInstance(SampleRate, (ChannelCount == 1) ? AudioChannels.Mono : AudioChannels.Stereo); _dynamicSound.BufferNeeded += BufferNeeded; FillBuffer(); BufferHitCount = 0; SubmitBuffer(); _dynamicSound.Play(); } } }
public XnaBufferedMusic(Stream stream) { musicStream = new MusicStreamFactory().Load(stream); var channels = musicStream.Channels == 2 ? AudioChannels.Stereo : AudioChannels.Mono; source = new DynamicSoundEffectInstance(musicStream.Samplerate, channels); }