Hi there. A while ago i downloaded tdv's sourcecode and found the bpc shared component there and i've got a question about the dsound class. When i learnt how the library works, I wrote a wrapper for it which i'll be pasting the code below. Then tryed to make a simple thing where you move around and a sound loops and updates as you move but this thing gave me errors when setting sound's orientation. This one seems different than other libraries that i have seen. BPC wrapper code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BPCSharedComponent;
using BPCSharedComponent.ExtendedAudio;
using SharpDX;
using SharpDX.DirectInput;
using SharpDX.DirectSound;
namespace bpc_wrapper
{
public class audio
{
public string soundpath;
public double max_distance;
public double min_distance;
private List<SecondarySoundBuffer> soundlist = new List<SecondarySoundBuffer>();
private int slot;
public audio(IntPtr handel, bool encrypted, string ss = "", string root="", string encryption_key="")
{
soundpath = ss;
if (encrypted == false)
DSound.initialize(handel, root);
else
{
DSound.initialize(handel, encrypted, root);
}
DSound.initializeOgg();
DSound.SoundPath = soundpath;
}
public int play_stationary(string soundfile, bool closefirst=false, bool looping=false)
{
soundlist.Add(DSound.LoadSound(soundpath+"/"+soundfile));
DSound.PlaySound(soundlist left braket soundlist.Count - 1 right braket , closefirst, looping);
return soundlist.Count - 1;
}
public void setOrientation(double x1, double y1, double z1,
double x2, double y2, double z2)
{
DSound.setOrientation(x1, y1, z1, x2, y2, z2);
}
public int play_3d(string filename, double x, double y, double z, bool closefirst=false, bool looping=false)
{
soundlist.Add(DSound.LoadSound3d(soundpath+"/"+filename, min_distance, max_distance, true));
DSound.PlaySound3d(soundlist left braket soundlist.Count - 1 right braket , closefirst, looping, x, y, z);
return soundlist.Count - 1;
}
public void update_listener(double x, double y, double z)
{
DSound.SetCoordinates(x, y, z);
}
public bool is_playing(int index)
{
return DSound.isPlaying(soundlist left braket index right braket );
}
public bool is_looping(int index)
{
return DSound.isLooping(soundlist left braket index right braket );
}
public void playwait(string fn)
{
DSound.playAndWait(fn);
}
public void setRootDirectory(string root)
{
DSound.setRootDirectory(root);
}
public void cleanup()
{
DSound.cleanUp();
for(int i=0; i<soundlist.Count; i++)
{
if (soundlist left braket i right braket != null)
{
soundlist left braket i right braket .Dispose();
soundlist left braket i right braket = null;
}
}
}
}
}
Although this code have a fiew extra useless parts that i should remove but it doesn't make any problems in the runtime from what i know.
And here is the test class i wrote:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using bpc_wrapper;
using System.Windows.Input;
namespace bpc_wrapper_test
{
public partial class Form1 : Form
{
System.Diagnostics.Stopwatch walktimer = new System.Diagnostics.Stopwatch();
audio aud;
double x, y, z;
private void timer1_Tick(object sender, EventArgs e)
{
mainloop();
}
public Form1()
{
InitializeComponent();
x = 1.0;
y = 1.0;
aud = new audio(this.Handle, false, "sounds");
aud.min_distance = 5;
aud.max_distance = 50;
aud.update_listener(x, y, z);
aud.play_3d("tm.wav", 1, 1, 1, false, true);
walktimer.Start();
}
void mainloop()
{
aud.update_listener(x, y, z);
if (walktimer.ElapsedMilliseconds >= 100)
{
if (Keyboard.IsKeyDown(Key.Down))
{
walktimer.Restart();
y -= 1;
}
if (Keyboard.IsKeyDown(Key.Up))
{
walktimer.Restart();
y += 1;
}
if (Keyboard.IsKeyDown(Key.Left))
{
walktimer.Restart();
x -= 1;
}
if (Keyboard.IsKeyDown(Key.Right))
{
walktimer.Restart();
x += 1;
}
updateRotation();
}
void updateRotation()
{
double X = this.x, Y = this.y, Z = this.z;
aud.setOrientation(0.0, 0.0, 1.0, 0.0, 0.0, 1.0); //I didn't want to do this but just for test. Anyways I couldn't get x or y or z(all three values) to 0 as you can see. It raises a sharp dx error which i really don't understand why it can't handle them all as 0 and thus when i update the orientation when i'm moving, if i step on x/y 0 the program raises an exception.
}
}
}
}
I had to replace left brakets with " left braket " and right brakets with " right braket " to avoid forum warnings.
Thanks for reading. Any help really is a pritiated because it's likely the best wrapper i have ever found for sharp dx sound.
Co-founder of Sonorous Arts.
Check out Sonorous Arts on github: https://github.com/sonorous-arts/
my Discord: kianoosh.shakeri2#2988