// 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(); }
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(); }); }
public void PlaySound(int samplingRate, byte[] pcmData) { DynamicSoundEffectInstance playback = new DynamicSoundEffectInstance(samplingRate, AudioChannels.Mono); playback.SubmitBuffer(pcmData); playback.Play(); }
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 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>(); }
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 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(); } } }
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(); } }
/// <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)); } }
private static void PlaybackSpeech() { // trace the operation TraceHelper.AddMessage("About to playback speech"); // create a sound effect instance DynamicSoundEffectInstance effect = new DynamicSoundEffectInstance(mic.SampleRate, AudioChannels.Mono); // submit all the buffers to the instance foreach (var buf in speechBufferList) if (buf.Length > 0) effect.SubmitBuffer(buf); speechBufferList.Clear(); // create an event handler to stop playback when all the buffers have been consumed effect.BufferNeeded += delegate { if (effect.PendingBufferCount == 0) { effect.Stop(); TraceHelper.AddMessage("Finished playing back speech"); } }; // play the speech FrameworkDispatcher.Update(); effect.Play(); }
private void UpdateCurrentTone() { if (_currentToneMillisecondsLeft > 0) { _currentToneMillisecondsLeft -= _dispatcherTimer.Interval.Milliseconds; return; } if (_dynamicSound != null) _dynamicSound.Stop(); // otherwise, buffer (1sec) is played out if (_toneQueue.Count == 0) { _dispatcherTimer.Stop(); return; } var newTone = _toneQueue.Dequeue(); _currentToneMillisecondsLeft = newTone.DurationInMilliSeconds - _dispatcherTimer.Interval.Milliseconds; if (newTone.Mute) return; _currentToneFrequency = newTone.Frequency; _dynamicSound = new DynamicSoundEffectInstance(48000, AudioChannels.Stereo); _dynamicSound.BufferNeeded += GetSoundBuffer; _dynamicSound.Play(); }
/// <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))); } }
public override void Initialize(AudioRenderInitializeArgs renderArgs) { Playback = new DynamicSoundEffectInstance(ClockRate, Channels == 2 ? AudioChannels.Stereo : AudioChannels.Mono); Playback.Play(); }
/// <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() { base.Initialize(); this.IsMouseVisible = true; // Sound sound = new DynamicSoundEffectInstance(SampleRate, Channels); sound.Play(); // Images colorTexture = new Texture2D(graphics.GraphicsDevice, FrameWidth, FrameHeight); LeftHand = new DepthImage(graphics.GraphicsDevice, DepthFrameWidth, DepthFrameHeight); RightHand = new DepthImage(graphics.GraphicsDevice, DepthFrameWidth, DepthFrameHeight); // Gesture Recognition LeftHandTracker = new HandTracker(); //Thread gestureThread = new Thread(new ThreadStart(IdentifyGestures)); //gestureThread.Start(); //Thread kinectThread = new Thread(new ThreadStart(StartKinect)); //kinectThread.Start(); StartKinect(); }