protected override void BufferLoaded() { buffer3D = new Buffer3D(buffer); //buffer3D.Mode = Mode3D.HeadRelative; buffer3D.Deferred = false; base.BufferLoaded(); }
private void restoreSoundBuffers() { if (device != null) { BufferDescription bufferDescription = new BufferDescription(); bufferDescription.ControlEffects = false; bufferDescription.ControlVolume = false; bufferDescription.ControlFrequency = false; bufferDescription.GlobalFocus = true; bufferDescription.Control3D = true; for (int i = 0; i < soundBank.Length; ++i) { if (secondaryBuffers[i] != null) { buffers3D[i].Dispose(); secondaryBuffers[i].Dispose(); } using (System.IO.Stream resourceStream = FileSystem.FileSystem.GetResource("ZunTzu.ResourceFiles." + soundBank[i] + ".wav").Open()) { secondaryBuffers[i] = new SecondaryBuffer(resourceStream, bufferDescription, device); buffers3D[i] = new Buffer3D(secondaryBuffers[i]); buffers3D[i].MinDistance = 2.0f; } } } }
/// <summary> /// Carga un archivo WAV de audio, indicando el volumen del mismo /// Solo se pueden cargar sonidos WAV que sean MONO (1 channel). /// Sonidos stereos (2 channels) no pueden ser utilizados. /// </summary> /// <param name="soundPath">Path del archivo WAV</param> /// <param name="volume">Volumen del mismo</param> public void loadSound(string soundPath, int volume, Device device) { try { dispose(); var bufferDescription = new BufferDescription(); bufferDescription.Control3D = true; if (volume != -1) { bufferDescription.ControlVolume = true; } SoundBuffer = new SecondaryBuffer(soundPath, bufferDescription, device); Buffer3d = new Buffer3D(SoundBuffer); Buffer3d.MinDistance = 50; if (volume != -1) { SoundBuffer.Volume = volume; } } catch (Exception ex) { throw new Exception("Error al cargar sonido estático WAV: " + soundPath, ex); } }
public static void Hook060001A0(int volspd, ref double vol, Vector3 vel, double voltgt, int eventtimer, SecondarySoundBuffer[] buf, int idx, double pitch, int freqmin, int freqmax, double distmin, Vector3 pos, Matrix rot, Matrix world, Vector3 listvel) { if (volspd <= 0 || Math.Abs(voltgt - vol) < 1E-05) { vol = voltgt; } else { vol += (voltgt - vol) * (1.0 - Math.Exp(-(double)eventtimer / (double)volspd)); } if (vol < 1E-05) { buf[idx].Stop(); return; } if (vol >= 1.0) { buf[idx].Volume = 0; } else if (vol > 1E-05) { buf[idx].Volume = (int)(Math.Log10(vol) * 2000.0); } else { buf[idx].Volume = -10000; } if (bufmap.ContainsKey(buf) || bufmap.ContainsKey(buf[idx])) { Buffer3D obj = bufmap.ContainsKey(buf) ? bufmap[buf] : bufmap[buf[idx]]; SoundBuffer3D[] buf3d = obj.buf3d; Matrix transformation = world; transformation.M41 = 0f; transformation.M42 = 0f; transformation.M43 = 0f; for (int i = 0; i < buf3d.Length; i++) { if (obj.pos != null) { buf3d[i].Position = obj.pos.GetValueOrDefault(); buf3d[i].MinDistance = buf3d[i].Position.Length(); } else { buf3d[i].MinDistance = (float)distmin; buf3d[i].Position = Vector3.TransformCoordinate(pos, rot); buf3d[i].Velocity = Vector3.TransformCoordinate(Vector3.Subtract(vel, listvel), transformation); } } } buf[idx].Frequency = Math.Min(Math.Max((int)(buf[idx].Format.SamplesPerSecond * pitch), freqmin), freqmax); }
/// <summary> /// Konstruktor parametrowy obiektu /// </summary> /// <param name="device">Uchwyt do karty dzwiekowej</param> /// <param name="fileName">Sciezka do pliku wav</param> /// <param name="position">Pozycja dzwieku w przestrzeni</param> public SoundBuffer(Device device, String fileName, Vector3 position) { BufferDescription description3D = new BufferDescription(); description3D.ControlEffects = false; description3D.Control3D = true; sound = new SecondaryBuffer(fileName, description3D, device); sound3D = new Buffer3D(sound); sound3D.Position = position; sound3D.MinDistance = 15f; }
/// <summary>Sets the origin of the sound in 3D space.</summary> /// <param name="track">Sound to play.</param> /// <param name="origin">Position of the origin of the sound.</param> public void SetAudioTrackOrigin(AudioTrack track, PointF origin) { if (device != null) { Buffer3D buffer = buffers3D[(int)track - 1]; float xCoeff = 1.0f; float yCoeff = -1.0f * gameDisplayArea.Height / gameDisplayArea.Width; buffer.Position = new Microsoft.DirectX.Vector3( xCoeff * ((2.0f * (origin.X - gameDisplayArea.Left) / gameDisplayArea.Width) - 1.0f), yCoeff * ((2.0f * (origin.Y - gameDisplayArea.Top) / gameDisplayArea.Height) - 1.0f), 1.0f); } }
public static void Hook0600019B(ref SecondarySoundBuffer[] buf) { if (buf[0].Capabilities.Control3D) { Buffer3D buf3d = bufmap.ContainsKey(buf[0]) ? bufmap[buf[0]] : new Buffer3D(); buf3d.buf3d = new SoundBuffer3D[1]; buf3d.buf3d[0] = new SoundBuffer3D(buf[0]); if (!bufmap.ContainsKey(buf[0])) { bufmap.Add(buf[0], buf3d); } } }
/// <summary> /// Carga un archivo WAV de audio, indicando el volumen del mismo /// Solo se pueden cargar sonidos WAV que sean MONO (1 channel). /// Sonidos stereos (2 channels) no pueden ser utilizados. /// </summary> /// <param name="soundPath">Path del archivo WAV</param> /// <param name="volume">Volumen del mismo</param> public void loadSound(string soundPath, Device device, BufferDescription bufferDescription) { try { dispose(); bufferDescription.Control3D = true; SoundBuffer = new SecondaryBuffer(soundPath, bufferDescription, device); Buffer3d = new Buffer3D(SoundBuffer); Buffer3d.MinDistance = 50; } catch (Exception ex) { throw new Exception("Error al cargar sonido estático WAV: " + soundPath, ex); } }
public static void Hook0600019C(ref SecondarySoundBuffer[] buf) { if (buf[0].Capabilities.Control3D) { Buffer3D buf3d = bufmap.ContainsKey(buf) ? bufmap[buf] : new Buffer3D(); buf3d.buf3d = new SoundBuffer3D[buf.Length]; for (int i = 0; i < buf.Length; ++i) { buf3d.buf3d[i] = new SoundBuffer3D(buf[i]); } if (!bufmap.ContainsKey(buf)) { bufmap.Add(buf, buf3d); } } }
public void PlaySound() { if ((buffer == null || !buffer.Status.Playing) && listBox.SelectedItem != null) { buffer = (Buffer)((DataRowView)listBox.SelectedItem).Row["buffer"]; Buffer3D buffer3d = new Buffer3D(buffer); buffer3d.Mode = Mode3D.Normal; buffer3d.Position = location; buffer.Play(0, BufferPlayFlags.Default); if (listBox.SelectedIndex < listBox.Items.Count - 1) { listBox.SelectedIndex = listBox.SelectedIndex + 1; } else { listBox.SelectedIndex = 0; } } }
/// <summary>Constructor.</summary> public AudioManager(Form owner, AudioProperties audioProperties) { this.audioProperties = audioProperties; try { device = new Device(); device.SetCooperativeLevel(owner, CooperativeLevel.Priority); } catch (ApplicationException) { device = null; System.Windows.Forms.MessageBox.Show( "Sound card doesn't meet ZunTzu's minimum requirements. Are the drivers installed?", "Can't use sound", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } secondaryBuffers = new SecondaryBuffer[soundBank.Length]; buffers3D = new Buffer3D[soundBank.Length]; restoreSoundBuffers(); }
internal MdxSound(Device device, string filename, bool is3d) { BufferDescription desc = new BufferDescription(); if (is3d) { desc.Control3D = true; desc.Guid3DAlgorithm = DSoundHelper.Guid3DAlgorithmDefault; desc.Mute3DAtMaximumDistance = true; } desc.ControlVolume = true; desc.ControlFrequency = true; _buffer = new SecondaryBuffer(filename, desc, device); if (is3d) { _buffer3d = new Buffer3D(_buffer); _buffer3d.Mode = Mode3D.Normal; _is3d = true; } }
public void PlaySound(Stream EmbeddedSoundStream, bool Loop) { try { _soundBuffer = new SecondaryBuffer(EmbeddedSoundStream, _bufferDesc, _soundDevice); _buffer3D = new Buffer3D(_soundBuffer); if (Loop) { _soundBuffer.Play(0, BufferPlayFlags.Looping); } else { _soundBuffer.Play(0, BufferPlayFlags.Default); } } catch (Exception expPlay) { Console.WriteLine(expPlay.Message); } }
public void PlaySound(string FullResourceName, bool Loop) { try { Stream strmTemp = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(FullResourceName); _soundBuffer = new SecondaryBuffer(strmTemp, _bufferDesc, _soundDevice); _buffer3D = new Buffer3D(_soundBuffer); if (Loop) { _soundBuffer.Play(0, BufferPlayFlags.Looping); } else { _soundBuffer.Play(0, BufferPlayFlags.Default); } } catch (Exception expPlay) { Console.WriteLine(expPlay.Message); } }
private void LoadSoundFile() { BufferDescription description = new BufferDescription(); description.Guid3DAlgorithm = DSoundHelper.Guid3DAlgorithmHrtfLight; description.Control3D = true; description.ControlFrequency = true; description.ControlVolume = true; if (null != soundBuffer) { soundBuffer.Stop(); soundBuffer.SetCurrentPosition(0); } // Load the wave file into a DirectSound buffer try { soundBuffer = new SecondaryBuffer(m_FileName, description, Listener.Device); soundBuffer3D = new Buffer3D(soundBuffer); } catch (Exception e) { GameEngine.Console.AddLine("Exception on loading " + m_FileName + ". Ensure file is Mono"); GameEngine.Console.AddLine(e.Message); } if (WaveFormatTag.Pcm != (WaveFormatTag.Pcm & description.Format.FormatTag)) { GameEngine.Console.AddLine("Wave file must be PCM for 3D control."); if (null != soundBuffer) { soundBuffer.Dispose(); } soundBuffer = null; } }
public Sound(string filename, int ID, short type) : base(filename, ID) { // get the file data WaveFile wf = FileManager.Instance.Load(filename); if(wf.WavFile != null) // we have a wave file with headers { // set up the buffer properties soundDesc = new BufferDescription(); soundDesc.GlobalFocus = false; soundDesc.ControlVolume = true; // enable 3D features for 3D sounds if(type == Sound.THREED_SOUND) { soundDesc.Control3D = true; soundDesc.Mute3DAtMaximumDistance = true; } // load the wave file from the stream into the buffer sound = new SecondaryBuffer(wf.WavFile, soundDesc, ((DirectSoundManager)SoundManager.Instance).Device); } else { // we have only raw PCM encoded sound data (usually from a decoder) // convert the format settings WaveFormat wfo = new WaveFormat(); wfo.BitsPerSample = wf.Bits; wfo.Channels = wf.Channels; wfo.SamplesPerSecond = wf.Frequency; wfo.BlockAlign = (short)(wf.Bits*wf.Channels / 8); wfo.FormatTag = WaveFormatTag.Pcm; wfo.AverageBytesPerSecond = wf.Frequency * wfo.BlockAlign; // set up buffer properties soundDesc = new BufferDescription(wfo); soundDesc.GlobalFocus = false; soundDesc.ControlVolume = true; soundDesc.BufferBytes = (int)wf.Data.Length; // enable 3D features for 3D sounds if(type == Sound.THREED_SOUND) { soundDesc.Control3D = true; soundDesc.Mute3DAtMaximumDistance = true; } // initialise the buffer and copy the (raw data) stream into it sound = new SecondaryBuffer(soundDesc, ((DirectSoundManager)SoundManager.Instance).Device); sound.Write(0, wf.Data, (int)wf.Data.Length, LockFlag.EntireBuffer); } // create a 3D buffer for 3D sounds if(type == Sound.THREED_SOUND) { threeDsound = new Buffer3D(sound); threeDsound.Mode = Mode3D.Normal; threeDsound.Deferred = true; } }
public static void Hook060000E8(string A_0, string A_1, int A_2, DirectSound directsound, ref SecondarySoundBuffer[] array) { SoundBufferDescription description = default(SoundBufferDescription); description.Flags = (BufferFlags.ControlVolume | BufferFlags.ControlFrequency | BufferFlags.Control3D); description.AlgorithmFor3D = DirectSound3DAlgorithmGuid.LightHrt3DAlgorithm; array = new SecondarySoundBuffer[A_2]; Buffer3D buf3d = new Buffer3D(); using (WaveStream waveStream = new WaveStream(A_1)) { description.Format = waveStream.Format; int num = 1; if (description.Format.Channels > 1) { num = (int)description.Format.Channels; description.Format.Channels = 1; description.Format.AverageBytesPerSecond /= num; WaveFormat format = description.Format; format.BlockAlignment /= (short)num; } description.SizeInBytes = (int)waveStream.Length; if (num > 1) { description.SizeInBytes /= num; description.SizeInBytes /= description.Format.BitsPerSample / 8; description.SizeInBytes *= description.Format.BitsPerSample / 8; } byte[] array2 = new byte[(int)waveStream.Length]; byte[] array3 = new byte[description.SizeInBytes]; waveStream.Read(array2, 0, (int)waveStream.Length); if (num == 1) { array3 = array2; } else { switch (description.Format.FormatTag) { case WaveFormatTag.Pcm: switch (description.Format.BitsPerSample) { case 32: for (int i = 0; i < description.SizeInBytes; i += 4) { int num2 = 0; for (int j = 0; j < num * 4; j += 4) { num2 += BitConverter.ToInt32(array2, i * num + j) / num; } Buffer.BlockCopy(BitConverter.GetBytes(num2), 0, array3, i, 4); } break; case 16: for (int k = 0; k < description.SizeInBytes; k += 2) { short num3 = 0; for (int l = 0; l < num * 2; l += 2) { num3 += (short)((int)BitConverter.ToInt16(array2, k * num + l) / num); } Buffer.BlockCopy(BitConverter.GetBytes(num3), 0, array3, k, 2); } break; case 8: for (int m = 0; m < description.SizeInBytes; m++) { byte b = 0; for (int n = 0; n < num; n++) { b += (byte)((int)array2[m * num + n] / num); } array3[m] = b; } break; } break; case WaveFormatTag.IeeeFloat: switch (description.Format.BitsPerSample) { case 32: for (int i = 0; i < description.SizeInBytes; i += 4) { float num2 = 0.0f; for (int j = 0; j < num * 4; j += 4) { num2 += BitConverter.ToSingle(array2, i * num + j) / num; } Buffer.BlockCopy(BitConverter.GetBytes(num2), 0, array3, i, 4); } break; } break; } } string trunk = A_0.LastIndexOf('.') != -1 ? A_0.Substring(0, A_0.LastIndexOf('.') + 1) : A_0; for (int num4 = 0; num4 < A_2; num4++) { array[num4] = new SecondarySoundBuffer(directsound, description); array[num4].Write <byte>(array3, 0, LockFlags.None); switch (trunk) { case "lb.": case "cp.": case "bg": buf3d.pos = new Vector3(0.0f, 0.5f, -trainlen * 0.4f) - cab; continue; case "fl.": case "run.": buf3d.pos = new Vector3(0.0f, 0.0f, -trainlen * 0.15f) - cab; continue; case "jt.": case "p.": case "b.": case "rv.": case "ats.": buf3d.pos = new Vector3(0.0f, -0.3f, 0.4f); continue; case "dl.": buf3d.pos = new Vector3(-cab.X - 1.5f, 0.0f, -1.0f); continue; case "hnm": case "hn0.": case "hn1.": buf3d.pos = new Vector3(0.0f, 0.0f, -0.5f) - cab; continue; case "sh": case "bp.": case "m.": case "bc.": buf3d.pos = new Vector3(0.0f, 0.5f, -trainlen * 0.15f) - cab; continue; case "lv.r.": buf3d.pos = new Vector3(1.0f, 1.0f, -trainlen * 0.15f) - cab; continue; case "bz.": case "pl.": buf3d.pos = new Vector3(-0.5f, 0.0f, 0.0f); continue; case "dr.": buf3d.pos = new Vector3(-cab.X + 1.5f, 0.0f, -1.0f); continue; case "lv.l.": buf3d.pos = new Vector3(-1.0f, 1.0f, -trainlen * 0.15f) - cab; continue; } buf3d.pos = new Vector3(0.0f, 0.0f, 0.0f); } } bufmap.Add(array, buf3d); }
/// <summary> /// Rewrites the audio data into the buffer and resets the format. /// </summary> public void RefreshSampleBuffer() { if (_sample == null) return; // Create the format description used by the buffer. WaveFormat format = new WaveFormat(); format.BitsPerSample = (short)_sample.BitsPerSample; format.Channels = (short)_sample.ChannelCount; format.FormatTag = WaveFormatTag.Pcm; format.SamplesPerSecond = _sample.SampleRate; format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8)); format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign; // Only mono sounds can be used for 3D. if ((_flags & SoundFlags.Positional) != 0 && format.Channels != 1) _flags &= ~SoundFlags.Positional; // Store some sample details. _frequency = _sample.SampleRate; _streamedBufferSize = MSToBytes(1000, format); // 1 seconds of audio. _streamThreshold = MSToBytes(400, format); // 400 millisecond threshold // Create the buffer description. BufferDescription desc = new BufferDescription(format); desc.BufferBytes = ((_flags & SoundFlags.Streamed) != 0) ? _streamedBufferSize : _sample.Data.Length; if ((_flags & SoundFlags.Positional) != 0) { desc.StickyFocus = true; desc.Control3D = true; desc.Mute3DAtMaximumDistance = true; desc.Guid3DAlgorithm = DSoundHelper.Guid3DAlgorithmHrtfFull; } else { desc.ControlVolume = true; desc.ControlPan = true; desc.ControlFrequency = true; } // Create DirectSound buffer. _buffer = new SecondaryBuffer(desc, _dx9Driver.Device); // Create the 3D buffer. if ((_flags & SoundFlags.Positional) != 0) { _3dBuffer = new Buffer3D(_buffer); _3dBuffer.Mode = Mode3D.Normal; _3dBuffer.MaxDistance = 300; _3dBuffer.MinDistance = 30; } // Write in the samples data. if (_sample.Data.Length > 0) { _buffer.Write(0, _sample.Data, LockFlag.EntireBuffer); _bufferLength = _sample.Data.Length; } }
public static void Hook06000799(WaveStream waveStream, DirectSound directsound, ref SecondarySoundBuffer buffer) { SoundBufferDescription description = default(SoundBufferDescription); description.Flags = (BufferFlags.ControlVolume | BufferFlags.ControlFrequency | BufferFlags.Control3D); description.AlgorithmFor3D = DirectSound3DAlgorithmGuid.LightHrt3DAlgorithm; description.Format = waveStream.Format; Buffer3D buf3d = new Buffer3D(); int num2 = 1; if (description.Format.Channels > 1 && description.Format.FormatTag == WaveFormatTag.Pcm) { num2 = (int)description.Format.Channels; description.Format.Channels = 1; description.Format.AverageBytesPerSecond /= num2; WaveFormat format = description.Format; format.BlockAlignment /= (short)num2; } description.SizeInBytes = (int)waveStream.Length; if (num2 > 1) { description.SizeInBytes /= num2; } buffer = new SecondarySoundBuffer(directsound, description); byte[] array = new byte[(int)waveStream.Length]; waveStream.Read(array, 0, (int)waveStream.Length); if (num2 == 1) { buffer.Write <byte>(array, 0, SlimDX.DirectSound.LockFlags.None); } else { byte[] array2 = new byte[description.SizeInBytes]; switch (description.Format.BitsPerSample) { case 32: for (int j = 0; j < description.SizeInBytes; j += 4) { int num3 = 0; for (int k = 0; k < num2 * 4; k += 4) { num3 += BitConverter.ToInt32(array, j * num2 + k) / num2; } Buffer.BlockCopy(BitConverter.GetBytes(num3), 0, array2, j, 4); } break; case 16: for (int l = 0; l < description.SizeInBytes; l += 2) { short num4 = 0; for (int m = 0; m < num2 * 2; m += 2) { num4 += (short)((int)BitConverter.ToInt16(array, l * num2 + m) / num2); } Buffer.BlockCopy(BitConverter.GetBytes(num4), 0, array2, l, 2); } break; case 8: for (int n = 0; n < description.SizeInBytes; n++) { byte b = 0; for (int num5 = 0; num5 < num2; num5++) { b += (byte)((int)array[n * num2 + num5] / num2); } array2[n] = b; } break; } buffer.Write <byte>(array2, 0, SlimDX.DirectSound.LockFlags.None); } buf3d.pos = new Vector3(0.0f, 1.0f, -5.0f); bufmap.Add(buffer, buf3d); }
bool OpenSoundFile() { //----------------------------------------------------------------------------- // Name: OnOpenSoundFile() // Desc: Called when the user requests to open a sound file //----------------------------------------------------------------------------- OpenFileDialog ofd = new OpenFileDialog(); Guid guid3DAlgorithm = Guid.Empty; // Get the default media path (something like C:\WINDOWS\MEDIA) if (string.Empty == Path) { Path = Environment.SystemDirectory.Substring(0, Environment.SystemDirectory.LastIndexOf("\\")) + "\\media"; } ofd.DefaultExt = ".wav"; ofd.Filter = "Wave Files|*.wav|All Files|*.*"; ofd.FileName = FileName; ofd.InitialDirectory = Path; if (null != applicationBuffer) { applicationBuffer.Stop(); applicationBuffer.SetCurrentPosition(0); } // Update the UI controls to show the sound as loading a file buttonPlay.Enabled = buttonStop.Enabled = false; labelStatus.Text = "Loading file..."; // Display the OpenFileName dialog. Then, try to load the specified file if (DialogResult.Cancel == ofd.ShowDialog(this)) { labelStatus.Text = "Load aborted."; timerMovement.Enabled = true; return(false); } FileName = ofd.FileName; Path = ofd.FileName.Substring(0, ofd.FileName.LastIndexOf("\\")); // Free any previous sound, and make a new one if (null != applicationBuffer) { applicationBuffer.Dispose(); applicationBuffer = null; } // Get the software DirectSound3D emulation algorithm to use // Ask the user for this sample, so display the algorithm dialog box. AlgorithmForm frm = new AlgorithmForm(); if (DialogResult.Cancel == frm.ShowDialog(this)) { // User canceled dialog box labelStatus.Text = "Load aborted."; labelFilename.Text = string.Empty; return(false); } LoadSoundFile(ofd.FileName); if (null == applicationBuffer) { return(false); } // Get the 3D buffer from the secondary buffer try { applicationBuffer3D = new Buffer3D(applicationBuffer); } catch (DirectXException) { labelStatus.Text = "Could not get 3D buffer."; labelFilename.Text = string.Empty; return(false); } // Get the 3D buffer parameters application3DSettings = applicationBuffer3D.AllParameters; // Set new 3D buffer parameters application3DSettings.Mode = Mode3D.HeadRelative; applicationBuffer3D.AllParameters = application3DSettings; if (true == applicationBuffer.Caps.LocateInHardware) { labelStatus.Text = "File loaded using hardware mixing."; } else { labelStatus.Text = "File loaded using software mixing."; } // Update the UI controls to show the sound as the file is loaded labelFilename.Text = FileName; EnablePlayUI(true); // Remember the file for next time if (null != applicationBuffer3D) { FileName = ofd.FileName; } Path = FileName.Substring(0, FileName.LastIndexOf("\\")); // Set the slider positions SetSlidersPos(0.0f, 0.0f, maxOrbitRadius, maxOrbitRadius * 2.0f); SliderChanged(); return(true); }
/// <summary> /// Rewrites the audio data into the buffer and resets the format. /// </summary> public void RefreshSampleBuffer() { if (_sample == null) { return; } // Create the format description used by the buffer. WaveFormat format = new WaveFormat(); format.BitsPerSample = (short)_sample.BitsPerSample; format.Channels = (short)_sample.ChannelCount; format.FormatTag = WaveFormatTag.Pcm; format.SamplesPerSecond = _sample.SampleRate; format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8)); format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign; // Only mono sounds can be used for 3D. if ((_flags & SoundFlags.Positional) != 0 && format.Channels != 1) { _flags &= ~SoundFlags.Positional; } // Store some sample details. _frequency = _sample.SampleRate; _streamedBufferSize = MSToBytes(1000, format); // 1 seconds of audio. _streamThreshold = MSToBytes(400, format); // 400 millisecond threshold // Create the buffer description. BufferDescription desc = new BufferDescription(format); desc.BufferBytes = ((_flags & SoundFlags.Streamed) != 0) ? _streamedBufferSize : _sample.Data.Length; if ((_flags & SoundFlags.Positional) != 0) { desc.StickyFocus = true; desc.Control3D = true; desc.Mute3DAtMaximumDistance = true; desc.Guid3DAlgorithm = DSoundHelper.Guid3DAlgorithmHrtfFull; } else { desc.ControlVolume = true; desc.ControlPan = true; desc.ControlFrequency = true; } // Create DirectSound buffer. _buffer = new SecondaryBuffer(desc, _dx9Driver.Device); // Create the 3D buffer. if ((_flags & SoundFlags.Positional) != 0) { _3dBuffer = new Buffer3D(_buffer); _3dBuffer.Mode = Mode3D.Normal; _3dBuffer.MaxDistance = 300; _3dBuffer.MinDistance = 30; } // Write in the samples data. if (_sample.Data.Length > 0) { _buffer.Write(0, _sample.Data, LockFlag.EntireBuffer); _bufferLength = _sample.Data.Length; } }
/// <summary> /// Carga un archivo WAV de audio, indicando el volumen del mismo /// Solo se pueden cargar sonidos WAV que sean MONO (1 channel). /// Sonidos stereos (2 channels) no pueden ser utilizados. /// </summary> /// <param name="soundPath">Path del archivo WAV</param> /// <param name="volume">Volumen del mismo</param> public void loadSound(string soundPath, int volume) { try { dispose(); BufferDescription bufferDescription = new BufferDescription(); bufferDescription.Control3D = true; if (volume != -1) { bufferDescription.ControlVolume = true; } soundBuffer = new SecondaryBuffer(soundPath, bufferDescription, GuiController.Instance.DirectSound.DsDevice); buffer3d = new Buffer3D(soundBuffer); buffer3d.MinDistance = 50; if (volume != -1) { soundBuffer.Volume = volume; } } catch (Exception ex) { throw new Exception("Error al cargar sonido estático WAV: " + soundPath, ex); } }
private void onReceive(object sender, ReceiveEventArgs e) { byte[] messageData = e.Message.ReceiveData.GetData(); byte messageType = messageData[0]; int senderId = BitConverter.ToInt32(messageData, 1); if (messageType >= (byte)ReservedMessageType.VideoFrame) { // videoconferencing message switch (messageType) { case (byte)ReservedMessageType.VideoFrame: handleVideoFrameMessage(senderId, messageData); break; case (byte)ReservedMessageType.VideoFrameAck: handleVideoFrameAckMessage(senderId, messageData); break; case (byte)ReservedMessageType.VideoCaptureDisabled: handleVideoCaptureDisabledMessage(senderId, messageData); break; case (byte)ReservedMessageType.VideoPlaybackToggled: handleVideoPlaybackToggledMessage(senderId, messageData); break; } e.Message.ReceiveData.Dispose(); } else { byte[] dataCopy = null; if (messageType > (byte)ReservedMessageType.VoicePlaybackStopped) { // application-defined message dataCopy = new byte[messageData.Length - 5]; if (dataCopy.Length > 0) { Array.Copy(messageData, 5, dataCopy, 0, dataCopy.Length); } } else if (messageType == (byte)ReservedMessageType.VoicePlaybackStarted) { if (voiceStatus == VoiceStatus.Connected && !soundBuffers.ContainsKey(senderId)) { // create a 3D sound buffer? lock (soundBuffers) { if (!soundBuffers.ContainsKey(senderId)) { // create a 3D sound buffer Buffer3D buffer = voiceClient.Create3DSoundBuffer(senderId); soundBuffers.Add(senderId, buffer); buffer.MinDistance = 2.0f; // adjust the origin of each voice adjustSoundOrigin(); } } } } else if (messageType == (byte)ReservedMessageType.PlayerHasLeft) { if (voiceStatus == VoiceStatus.Connected && soundBuffers.ContainsKey(senderId)) { // deallocate a 3D sound buffer? lock (soundBuffers) { Buffer3D buffer; if (soundBuffers.TryGetValue(senderId, out buffer)) { soundBuffers.Remove(senderId); voiceClient.Delete3DSoundBuffer(senderId, buffer); // adjust the origin of each voice adjustSoundOrigin(); } } } } e.Message.ReceiveData.Dispose(); // add the message to the message list NetworkMessage message = new NetworkMessage(senderId, messageType, dataCopy); lock (networkMessages) { networkMessages.Enqueue(message); } } }