Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Bizzare issue playing audio files  (Read 12824 times)

0 Members and 1 Guest are viewing this topic.

AnoHito

  • Newbie
  • *
  • Posts: 7
    • View Profile
Bizzare issue playing audio files
« on: June 01, 2014, 07:37:49 am »
Hi, so just as a disclaimer, I am very new to both the D programming language and SFML, so there could easily be a facepalm worthy cause behind my current issue, but if there is I can't find it. I'm currently trying to get a simple test program that uses DSFML to work correctly. Here is my current code:

module winmain;

import core.runtime;
import core.sys.windows.windows;

import dsfml.system;
import dsfml.window;
import dsfml.graphics;
import dsfml.audio;

extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    int result;

    try
    {
        Runtime.initialize();
        result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
    }
    catch (Throwable error)             // catch any uncaught exceptions
    {
        MessageBoxA(null, cast(char *)error.toString(), "Error", MB_OK | MB_ICONEXCLAMATION);
        result = 0;             // failed
    }

    return result;
}

int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        void errorMessage(Throwable error)
        {
        MessageBoxA(null, cast(char *)error.toString(), "Error", MB_OK | MB_ICONEXCLAMATION);
        }

    // Load a sprite to display
        Texture texture;
        Sprite sprite;

    try
    {
                texture = new Texture();
                if(!texture.loadFromFile("cute_image.png"))
                        return 0;
                sprite = new Sprite(texture);
    }
    catch (Throwable error)
    {
                errorMessage(error);
        }

    // Create the main window
        auto size = texture.getSize();
    auto window = new RenderWindow(VideoMode(size.x, size.y), "DSFML window");

    // Create a graphical text to display
    auto font = new Font();
    if(!font.loadFromFile("arial.ttf"))
        return 0;
    auto text = new Text("Hello DSFML", font, 50);
        text.setColor(Color.Black);

        // Load a music to play
    try
    {
                auto music = new Music();
                if(!music.openFromFile("nice_music.ogg"))
                        return 0;

                // Play the music
                music.play();
    }
    catch (Throwable error)
    {
                errorMessage(error);
        }

    // Start the game loop
    while(window.isOpen())
    {
        // Process events
        Event event;
        while(window.pollEvent(event))
        {
            // Close window
            if(event.type == Event.EventType.Closed)
                window.close();
        }

        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(sprite);

        // Draw the string
        window.draw(text);

        // Update the window
        window.display();
    }

    return 0;
}
 

The problem is that while the audio file will load correctly, the program will throw a "Bad file descriptor" error when I actually try to play it. And if that weren't bad enough, when I try to close the program I get the following runtime error:

Quote
Unhandled exception at 0x7736A4B9 (ole32.dll) in DSFML_Example.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.

I really have no idea what's going on here. Looking through my code and configuration, it's not clear at all that I did anything horribly wrong. Here is a link to my entire Visual Studio project in case anyone wants to look at it (it requires you to have Visual D installed).

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Bizzare issue playing audio files
« Reply #1 on: June 01, 2014, 08:43:21 am »
Thanks for the heads up!

I'll look through this to see if I can find anything wrong. The current binding is kind of wonky in the audio module, so it might not be you. Unfortunately I am in preparation mode for my upcoming finals so I don't know how soon I can actually get to this. I might have a bit of free time Monday though, so I will look at it if I am able.


I did run your exe though, and since the error itself is coming from stdio, I don't think it is a DSFML problem. I'll get back to you as soon as I can!
DSFML - SFML for the D Programming Language.

AnoHito

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Bizzare issue playing audio files
« Reply #2 on: June 02, 2014, 01:55:56 am »
Okay, I think I solved the problem. I'm not really sure what the cause was, but by going through the whole process of building SFML and SFML-C on my own setup, I was able to get the audio working. :) But I did run into a couple of non-critical but still worrying issues during the process. First of all, when building dsfml-graphics, I encountered the following error:

Quote
Error   1   error LNK2001: unresolved external symbol "public: static struct sf::Shader::CurrentTextureType sf::Shader::CurrentTexture" (?CurrentTexture@Shader@sf@@2UCurrentTextureType@12@A)   C:\Development\Libraries\DSFML-C\build\src\SFML\Graphics\Shader.obj   dsfml-graphics

I was able to work around it with a lazy hack, but I couldn't figure out what was actually causing the error...

void sfShader_setCurrentTextureParameter(sfShader* shader, const char* name)
{
    //CSFML_CALL(shader, setParameter(name, sf::Shader::CurrentTexture));
        return;
}

Secondly, I ran into an issue where even after closing the main window of the program, the audio would keep playing and the program would not exit. I was able to sort of fix things by manually calling exit(), but this causes my program to throw the following error when running in the debugger:

Quote
Unhandled exception at 0x7736A4B9 (ole32.dll) in DSFML_Example.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.

Since I build all the libraries with debugging information, I was able to track down the issue to the destructor of the SFML AudioDevice class:

AudioDevice::~AudioDevice()
{
    // Destroy the context
    alcMakeContextCurrent(NULL);
    if (audioContext)
        alcDestroyContext(audioContext);

    // Destroy the device
    if (audioDevice)
        alcCloseDevice(audioDevice);
}

It appears that the function is being called with an invalid "this" pointer. My best guess would some sort of garbage collection issue, but I don't really know enough about D to say for sure.

Here are the updated project files in case you'd like to look at them.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Bizzare issue playing audio files
« Reply #3 on: June 02, 2014, 04:44:00 am »
The audio parts of DSFML definitely need some work, and that is where I have been spending most of my time lately so a fix is probably already on the way.

Thanks for all your help. I'll get this sorted out!
DSFML - SFML for the D Programming Language.

AnoHito

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Bizzare issue playing audio files
« Reply #4 on: June 02, 2014, 06:10:42 am »
Okay, awesome. ;D But what about the unresolved external symbol error? Is there anything I could do to handle that a little more gracefully than just blanking out the function?

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Bizzare issue playing audio files
« Reply #5 on: June 02, 2014, 07:29:14 am »
From what I was able to gather, ole32.dll is specific to the Win32 API.

And just so you know, when I ran your program everything seems to work fine for me. I don't get any crashes on exit or anything like that. I am running it outside any IDE, so maybe it is an issue with the Mago Debugger in Visual D?

Edit:
Seems like I spoke too soon. I do get memory errors if the music plays all the way to the end. Those are most likely something going on with DSFML.
« Last Edit: June 02, 2014, 07:31:12 am by Jebbs »
DSFML - SFML for the D Programming Language.

AnoHito

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Bizzare issue playing audio files
« Reply #6 on: June 02, 2014, 08:30:14 am »
You do need to be running it in the debugger to see the error I was getting. If you run it normally it doesn't appear. I didn't even know about the error that happens if the music finished though.

Edit: Ah, yes, confirmed. I get a core.exception.InvalidMemoryOperationError if I let the music file play until the end.
« Last Edit: June 02, 2014, 08:35:40 am by AnoHito »

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Bizzare issue playing audio files
« Reply #7 on: June 27, 2014, 07:39:26 am »
Sorry for taking forever to get back to you!

I finally was able to make a lot of progress towards fixing this. As I get ready to push out DSFML 2.1 I am rewrapping some of the audio parts from SFML instead of trying to recreate them in D code, which is how things have been. Tonight I was able to successfully get SoundStream set up and it played a song with out error all the way through.

Getting this working was probably the hardest thing to get working because it has abstract methods (Interfacing D and C++ code like this is always fun ;) ), but now that I know everything is running fine I should have the rest of the audio module ready in the next couple of days. Once it is done I'll build and upload new shared/import libraries and after you grab the latest D sources you should be good to go.
DSFML - SFML for the D Programming Language.

AnoHito

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Bizzare issue playing audio files
« Reply #8 on: July 01, 2014, 07:05:13 am »
Awesome news. ;D Sorry, for the late reply, but I've doing a lot of game engine programming lately which has kept me pretty busy. I've really been enjoying coding in D too. It's such a welcome change to have a fast low level language that still retains a lot of the high level niceties that I've grown accustomed to by coding in Ruby. C++ actually scared me away from serious game programming for a long time...

AnoHito

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Bizzare issue playing audio files
« Reply #9 on: July 22, 2014, 09:57:52 am »
I just tested out the new version of of DSFML, and while I did have a few problems getting things to work, I was finally able to play audio files without triggering a crash. ;)

Here are some of the difficulties I ran into in case you want to look into fixing them:

  • The latest precompiled nightly seems to be missing several functions in the audio library, resulting in unresolved symbol errors when used with the latest DSFML sources.
  • Visual C++ does not allow implicit returns, so I had to add a few return statements in order to get DSFML-C to compile.
  • sfErrAudio_getOutput causes unresolved external symbol errors with the latest DSFML-C, so I had to comment out it's use.
  • An invalid file descriptor exception was thrown when I called music.openFromFile, even when the audio file existed. Trying to open a file that doesn't exist also results in an invalid memory operation error, which I don't think should happen normally... But I was able to get things working correctly by enclosing the call to openFromFile in a try/catch block, and once I suppressed the error everything worked perfectly.

So things are still a little hacked together on my end, but who cares if the solution is a little messy as long as it works. ;)

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Bizzare issue playing audio files
« Reply #10 on: July 27, 2014, 05:47:12 am »
Sorry for my own late reply! I have been kind of everywhere lately.

I'm glad you got it working, but let's see if I can make it less hacky.

Quote
  • The latest precompiled nightly seems to be missing several functions in the audio library, resulting in unresolved symbol errors when used with the latest DSFML sources.
This is because I haven't had a chance to update the binaries for the most recent version, so that one is on me. I'll be building them tonight for each system though.


Quote
  • Visual C++ does not allow implicit returns, so I had to add a few return statements in order to get DSFML-C to compile.

Can you let me know where this happens so I can fix it in the source code?

Quote
  • sfErrAudio_getOutput causes unresolved external symbol errors with the latest DSFML-C, so I had to comment out it's use.

This might have been caused by some miss matching with the D code and C libraries. One of the changes I did over in DSFML-C was merging SFML's source directly into the repository. Because of that, it fixed an issue that I had before where I had to define the error output for each module. sfErrAudio_getOutput shouldn't exist any more. There should only be sfErr_getOutput declared in dsfml.system.err.

Quote
  • An invalid file descriptor exception was thrown when I called music.openFromFile, even when the audio file existed. Trying to open a file that doesn't exist also results in an invalid memory operation error, which I don't think should happen normally... But I was able to get things working correctly by enclosing the call to openFromFile in a try/catch block, and once I suppressed the error everything worked perfectly.

That sounds like another error on my part. I'll look into it and get it fixed.



It's awesome you managed to get it working though! Let me know if you have any other problems.
DSFML - SFML for the D Programming Language.

AnoHito

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Bizzare issue playing audio files
« Reply #11 on: July 27, 2014, 06:45:30 pm »
This is because I haven't had a chance to update the binaries for the most recent version, so that one is on me. I'll be building them tonight for each system though.

Have you considered packaging the binaries with a copy of the DSFML source tree they are compatible with? That might make it easier for people to get everything up and running.

Can you let me know where this happens so I can fix it in the source code?

Sure thing. Here is a list of all the places the error occurs: dsfml/audio/sound.cpp - 160; dsfml/audio/soundbuffer.cpp - 126; dsfml/audio/soundrecorder.cpp - 58; dsfml/audio/soundrecorderstruct.cpp - 71; dsfml/graphics/image.cpp - 159; dsfml/graphics/view.cpp - 130; dsfml/network/http.cpp - 113, 120; dsfml/network/packet.cpp - 73, 80; dsfml/network/tcpsocket.cpp - 60, 66, 81

It's awesome you managed to get it working though! Let me know if you have any other problems.

Will do. But other than the audio issue things have actually been going really well. I was able to get dbox and DSFML working together in a reasonable way, and I'm well on my way to having a working game engine. :)