Problem loading sounds (OpenAL & c#)

Started by
3 comments, last by 44cll 11 years, 11 months ago
I've been trying to figure this out all day/night and I am completely at a loss. Every time I run the code below I get an "Assertion Failed" error. It seems like the problem is with the LoadSound method in the SoundManager class. I've checked the binary directory of my project folder I think 50 times now to make sure the sound files are actually in there as well as the alut.dll, ILU.dll and OpenAL32.dll files...

These are all the classes in the engine related to sound as well as what's relevant in the form.cs file. Any sort of help or nudge in the right direction will be greatly appreciated wacko.png

Form1.cs
[spoiler]

namespace exByte_Test
{
public partial class Form1 : Form
{
SoundManager _soundManager = new SoundManager();
public Form1()
{
InitializeComponent();
InitializeSounds();
}
private void InitializeSounds()
{
_soundManager.LoadSound("effect", "soundeffect1.wav");
_soundManager.LoadSound("effect2", "soundeffect2.wav");
}
}
}

[/spoiler]

SoundManager.cs
[spoiler]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenAl;
using System.IO;
namespace exByte
{
public class SoundManager : IDisposable
{
struct SoundSource
{
public SoundSource(int bufferId, string filePath)
{
_bufferId = bufferId;
_filePath = filePath;
}
public int _bufferId;
string _filePath;
}
Dictionary<string, SoundSource> _soundIdentifier = new Dictionary<string, SoundSource>();
readonly int MaxSoundChannels = 256;
List<int> _soundChannels = new List<int>();
float _masterVolume = 1.0f;
public SoundManager()
{
Alut.alutInit();
DicoverSoundChannels();
}
private void DicoverSoundChannels()
{
while (_soundChannels.Count < MaxSoundChannels)
{
int src;
Al.alGenSources(1, out src);
if (Al.alGetError() == Al.AL_NO_ERROR)
{
_soundChannels.Add(src);
}
else
{
break; // there's been an error - we've filled all the channels.
}
}
}
private bool IsChannelPlaying(int channel)
{
int value = 0;
Al.alGetSourcei(channel, Al.AL_SOURCE_STATE, out value);
return (value == Al.AL_PLAYING);
}
private int FindNextFreeChannel()
{
foreach (int slot in _soundChannels)
{
if (!IsChannelPlaying(slot))
{
return slot;
}
}
return -1;
}

public void LoadSound(string soundId, string path)
{
// Generate a buffer.
int buffer = -1;
Al.alGenBuffers(1, out buffer);
int errorCode = Al.alGetError();
System.Diagnostics.Debug.Assert(errorCode == Al.AL_NO_ERROR);
int format;
float frequency;
int size;
System.Diagnostics.Debug.Assert(File.Exists(path), "File not found");
IntPtr data = Alut.alutLoadMemoryFromFile(path, out format, out size, out frequency);
System.Diagnostics.Debug.Assert(data != IntPtr.Zero);
// Load wav data into the generated buffer.
Al.alBufferData(buffer, format, data, size, (int)frequency);
// Every seems ok, add it to the library.
_soundIdentifier.Add(soundId, new SoundSource(buffer, path));
}
public Sound PlaySound(string soundId)
{
// Default play sound doesn't loop.
return PlaySound(soundId, false);
}
public Sound PlaySound(string soundId, bool loop)
{
int channel = FindNextFreeChannel();
if (channel != -1)
{
Al.alSourceStop(channel);
Al.alSourcei(channel, Al.AL_BUFFER, _soundIdentifier[soundId]._bufferId);
Al.alSourcef(channel, Al.AL_PITCH, 1.0f);
Al.alSourcef(channel, Al.AL_GAIN, 1.0f);
if (loop)
{
Al.alSourcei(channel, Al.AL_LOOPING, 1);
}
else
{
Al.alSourcei(channel, Al.AL_LOOPING, 0);
}
// Al.alSourcef(channel, Al.AL_GAIN, _masterVolume);
Al.alSourcePlay(channel);
return new Sound(channel);
}
else
{
// Error sound
return new Sound(-1);
}
}

public void ChangeVolume(Sound sound, float value)
{
Al.alSourcef(sound.Channel, Al.AL_GAIN, _masterVolume * value);
}
public bool IsSoundPlaying(Sound sound)
{
return IsChannelPlaying(sound.Channel);
}
public void StopSound(Sound sound)
{
if (sound.Channel == -1)
{
return;
}
Al.alSourceStop(sound.Channel);
}
public void MasterVolume(float value)
{
_masterVolume = value;
foreach (int channel in _soundChannels)
{
Al.alSourcef(channel, Al.AL_GAIN, value);
}
}
#region IDisposable Members
public void Dispose()
{
foreach (SoundSource soundSource in _soundIdentifier.Values)
{
SoundSource temp = soundSource;
Al.alDeleteBuffers(1, ref temp._bufferId);
}
_soundIdentifier.Clear();
foreach (int slot in _soundChannels)
{
int target = _soundChannels[slot];
Al.alDeleteSources(1, ref target);
}
Alut.alutExit();
}
#endregion
}
}

[/spoiler]

Sound.cs
[spoiler]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenAl;
namespace exByte
{
public class Sound
{
public int Channel { get; set; }
public bool FailedToPlay
{
get
{
// minus is an error state.
return (Channel == -1);
}
}
public Sound(int channel)
{
Channel = channel;
}
}
}

SoundTestState.cs

[/spoiler]

SoundTestState.cs
[spoiler]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using exByte;

namespace exByte_Test
{
class SoundTestState : IGameObject
{
SoundManager _soundManager;
double _count = 3;

public SoundTestState(SoundManager soundManager)
{
_soundManager = soundManager;
_soundManager.MasterVolume(0.1f);
}

public void Render()
{
// The sound test doesn't need to render anything.
}

public void Update(double elapsedTime)
{
_count -= elapsedTime;

if (_count < 0)
{
_count = 3;
_soundManager.PlaySound("effect");
_soundManager.PlaySound("effect2");
}
}
}
}

[/spoiler]
Advertisement
Set some breakpoints and step through the code to see which assertion is actually failing. If it is in LoadSound, it can be any of at least 3 assertions. Move from there. Double check the path you're sending in to make sure it's 100% correct. Try a variety of other files and filetypes, maybe it's choking on an incompatible codec.

Between Scylla and Charybdis: First Look <-- The game I'm working on

Object-Oriented Programming Sucks <-- The kind of thing I say

Hey, thanks for the reply. I have minimal programming experience so I get lost in the woods pretty often... After a quick google search to see what Breakpoints do, I added one in at the top of the LoadSound method and everything seems to be working fine until it hits the 3rd assertion. Also, the path variables contain the right sound file name. I'm just not sure where to go from here, but I'll keep tinkering with it tonight.

edit
Actually, I take that back. The assertion failed error pops up on the line after the 3rd assertion.

Al.alBufferData(buffer, format, data, size, (int)frequency);


edit
Well, now I'm not entirely sure... It doesn't crap out while the 3rd assertion is highlighted, it's when I pass the breakpoint beyond that line. So does that mean the problem is in the next line or the line you're moving the breakpoint from?

Well, now I'm not entirely sure... It doesn't crap out while the 3rd assertion is highlighted, it's when I pass the breakpoint beyond that line. So does that mean the problem is in the next line or the line you're moving the breakpoint from?

Don't know much about c#, so can not help with your actual problem. Anyway. Highlight is for marking the command that will be run next (in every debugger i have ever seen) => the assertion is the one failing.
Looks like the data variable doesn't contain any information because alutLoadMemoryFromFile is failing. I tried some other file types but still no luck.

This topic is closed to new replies.

Advertisement