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

Author Topic: I'm missing something fundamental RE threads and audio  (Read 5549 times)

0 Members and 1 Guest are viewing this topic.

tuanogus

  • Newbie
  • *
  • Posts: 7
    • View Profile
I'm missing something fundamental RE threads and audio
« on: June 13, 2009, 06:31:10 pm »
Hi,

I'm developing an audio sample manager and am prototyping it with wxWidgets and SFML. I am having trouble with sf::Thread...well...threading. I am spankin' new to SFML and have read as much as I can on this subject, and I am probably misunderstanding something fundamental.

I am using wxWidgets for the UI (the main thread) and am trying to spawn a thread to play a sound. For example, inside my wxFrame...
Code: [Select]

// Event handler for a wxListCtrl selection event...
void MainFrame::OnFileListItemSelected( wxListEvent& WXUNUSED(event) )
{
       std::string filename = this->wx2std(this->GetSelectedFile());
sf::Thread Thread(&ThreadFunction, &filename);
Thread.Launch();
}

void ThreadFunction(void* UserData)
{
std::string* str = (std::string*)UserData;

sf::SoundBuffer Buffer;

if (!Buffer.LoadFromFile(*str)) return;

sf::Sound Sound;
Sound.SetBuffer(Buffer); // Buffer is a sf::SoundBuffer
Sound.SetVolume(100.f);
Sound.Play();

while(Sound.GetStatus() == sf::Sound::Playing) {
sf::Sleep(0.1f);
}
}

While this actually plays the audio as I expected, my UI is blocked until the Sound has finished playing. Any feedback is appreciated. I'm looking at a lot of different libraries and want to have a decent understanding of SFML before proceeding in my evaluation.

Also, I don't know why I need the last while loop in order for the Sound to play. I expected to be able to just call Sound.Play and have the entire sound play.

Cheers! And, very nice work. I love the API design.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: I'm missing something fundamental RE threads and audio
« Reply #1 on: June 13, 2009, 06:48:12 pm »
Quote from: "tuanogus"
Also, I don't know why I need the last while loop in order for the Sound to play. I expected to be able to just call Sound.Play and have the entire sound play.
Normally, this is possible, you don't even have to start a separate thread. Have you tried just playing a sf::Sound and continuing the normal program flow?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

tuanogus

  • Newbie
  • *
  • Posts: 7
    • View Profile
I'm missing something fundamental RE threads and audio
« Reply #2 on: June 13, 2009, 09:06:00 pm »
Yes, I tried that approach with the same results. Because of that I assumed that SFML was not threaded internally, and explains why I'm now trying the approach posted above.

Thanks for quick response!

Any other ideas?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
I'm missing something fundamental RE threads and audio
« Reply #3 on: June 13, 2009, 09:11:07 pm »
Hi

1- Playing a sound doesn't block the application, so you can call sound.Play() in your main thread. The whole audio module is threaded actually. And you don't need the while loop of course, the sound just plays all alone.

2- The thread in your example is blocking because you're using a local sf::Thread variable, which will get destroyed after the function ends. And the destructor of sf::Thread is calling Wait(), which waits for the thread to finish.
Laurent Gomila - SFML developer

tuanogus

  • Newbie
  • *
  • Posts: 7
    • View Profile
I'm missing something fundamental RE threads and audio
« Reply #4 on: June 13, 2009, 10:22:24 pm »
Ah, okay, that makes sense. I'll move some things up in scope and give it whirl.

Tx!

tuanogus

  • Newbie
  • *
  • Posts: 7
    • View Profile
I'm missing something fundamental RE threads and audio
« Reply #5 on: June 14, 2009, 02:52:42 am »
Regarding #1... you're saying I should be able to do this?

Code: [Select]

void MainFrame::OnFileListItemSelected( wxListEvent& WXUNUSED(event) )
{
std::string filename = this->wx2std(this->GetSelectedFile());

sf::SoundBuffer Buffer;

if (!Buffer.LoadFromFile(filename)) return;

sf::Sound Sound;
Sound.SetBuffer(Buffer);
Sound.SetVolume(100.f);
Sound.Play();
}


And this should play the sound without blocking the caller?

tuanogus

  • Newbie
  • *
  • Posts: 7
    • View Profile
I'm missing something fundamental RE threads and audio
« Reply #6 on: June 14, 2009, 03:02:20 am »
Okee, I've got it figured out. Required a pointer (in my class) to a sf::Sound. Will follow up with my solution shortly.

tuanogus

  • Newbie
  • *
  • Posts: 7
    • View Profile
I'm missing something fundamental RE threads and audio
« Reply #7 on: June 14, 2009, 03:13:44 am »
In haste... So I created a sf::Sound pointer in my MainFrame class. My code, of course, looks more like the following now.
Code: [Select]

void MainFrame::OnFileListItemSelected( wxListEvent& WXUNUSED(event) )
{
std::string filename = this->wx2std(this->GetSelectedFile());

sf::SoundBuffer Buffer;

if (!Buffer.LoadFromFile(filename))  return;

this->Sound->SetBuffer(Buffer);
this->Sound->SetVolume(100.f);
this->Sound->Play();
}

There's a lot wrong with my code still, but at least the UI is free, audio is playing, and I'm starting to understanding SFML now. Thanks for your direction!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
I'm missing something fundamental RE threads and audio
« Reply #8 on: June 14, 2009, 08:04:47 am »
You don't need a pointer, you just need the sf::Sound instance to be alive as long as it's playing (so, making it a member of your class is ok).
Laurent Gomila - SFML developer

tuanogus

  • Newbie
  • *
  • Posts: 7
    • View Profile
I'm missing something fundamental RE threads and audio
« Reply #9 on: June 14, 2009, 03:21:46 pm »
Yes, of course. Thanks again for your attention! Off to build some rockets...