ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

https://stackoverflow.com/questions/9981087/simple-c-sound-api suggests SDL_Mixer, I tried that out and the results are here: https://github.com/lucasw/sdl2_ros . It wasn't completely trivial to use SDL2 (maybe SDL would have been easier) see #q253311 .

  if (SDL_Init(SDL_INIT_AUDIO) < 0)
  {
    ROS_ERROR_STREAM("Couldn't initialize SDL " << SDL_GetError());
    return -1;
  }

  if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
  {
    ROS_ERROR_STREAM("Couldn't initialize SDL audio " << Mix_GetError());
    return -2;
  }

  std::string sound_file = "test.wav";
  ros::param::get("~sound", sound_file);

  Mix_Chunk* sound = NULL;
  sound = Mix_LoadWAV(sound_file.c_str());
  if (!sound)
  {
    ROS_ERROR_STREAM("Couldn't load sound" << sound_file << ", " << Mix_GetError());
    return -3;
  }

  Mix_PlayChannel(-1, sound, 0);
  while (ros::ok())
  {
    if (!Mix_Playing(-1))
      break;
    ros::Duration(0.2).sleep();
  }
  // shutdown
  Mix_FreeChunk(sound);
  sound = NULL;
  Mix_Quit();
  SDL_Quit();

There didn't turn out to be anything ros specific here, I don't think there should be any problem putting something like the above into an rqt plugin (say so if there are).

https://stackoverflow.com/questions/9981087/simple-c-sound-api suggests SDL_Mixer, I tried that out and the results are here: https://github.com/lucasw/sdl2_ros . It wasn't completely trivial to use SDL2 (maybe SDL would have been easier) see #q253311 .

  if (SDL_Init(SDL_INIT_AUDIO) < 0)
  {
    ROS_ERROR_STREAM("Couldn't initialize SDL " << SDL_GetError());
    return -1;
  }

  if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
  {
    ROS_ERROR_STREAM("Couldn't initialize SDL audio " << Mix_GetError());
    return -2;
  }

  std::string sound_file = "test.wav";
  ros::param::get("~sound", sound_file);

  Mix_Chunk* sound = NULL;
  sound = Mix_LoadWAV(sound_file.c_str());
  if (!sound)
  {
    ROS_ERROR_STREAM("Couldn't load sound" << sound_file << ", " << Mix_GetError());
    return -3;
  }

  Mix_PlayChannel(-1, sound, 0);
  while (ros::ok())
  {
    if (!Mix_Playing(-1))
      break;
    ros::Duration(0.2).sleep();
  }
  // shutdown
  Mix_FreeChunk(sound);
  sound = NULL;
  Mix_Quit();
  SDL_Quit();

There didn't turn out to be anything ros specific here, I don't think there should be any problem putting something like the above into an rqt plugin (say so if there are).is).

https://stackoverflow.com/questions/9981087/simple-c-sound-api suggests SDL_Mixer, I tried that out and the results are here: https://github.com/lucasw/sdl2_ros . It wasn't completely trivial to use SDL2 (maybe SDL would have been easier) see #q253311 .

  if (SDL_Init(SDL_INIT_AUDIO) < 0)
  {
    ROS_ERROR_STREAM("Couldn't initialize SDL " << SDL_GetError());
    return -1;
  }

  if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
  {
    ROS_ERROR_STREAM("Couldn't initialize SDL audio " << Mix_GetError());
    return -2;
  }

  std::string sound_file = "test.wav";
  ros::param::get("~sound", sound_file);

  Mix_Chunk* sound = NULL;
  sound = Mix_LoadWAV(sound_file.c_str());
  if (!sound)
  {
    ROS_ERROR_STREAM("Couldn't load sound" << sound_file << ", " << Mix_GetError());
    return -3;
  }

  Mix_PlayChannel(-1, sound, 0);
  while (ros::ok())
  {
    if (!Mix_Playing(-1))
      break;
    ros::Duration(0.2).sleep();
  }
  // shutdown
  Mix_FreeChunk(sound);
  sound = NULL;
  Mix_Quit();
  SDL_Quit();

There didn't turn out to be anything ros specific here, I don't think there should be any problem putting something like the above into an rqt plugin. But if there was, would there be anything wrong with separating the sound playback from the plugin (say so if there is).and use a topic to trigger the sound? In that case ncurses or perhaps the '\a' could be made to work in standalone nodes if not in a plugin.