convert audio msg to mp3 in rosjs

asked 2020-04-17 12:03:18 -0500

dinesh gravatar image

updated 2020-04-17 12:04:16 -0500

i am using rosjslib to stream audio msg in js. but when i log the msg it looks like this:

Received message on /audio/audio: //OYRN8XddtW3z2Geq6r/qpeekzxhLqwXQqKgMhyjo7ESRE+ikfYCRzompaUhpxGJhABlQNBs43EhJUy47RpoZwIiqKg81MWVinw6bvFHB1BZFMELShe0bhNZzNZG1vMYXqaLUpkT47e57na/c+Hy+7u30pq3ydVOkG1tKWo8tztmtqTql8xFDXbWEN5VJyr3wYQOJTB5pzmo7nSpmq3bW2CE4gd/3Z75mEAxuoC8hmR22yXoItQvSGELUrwGKmlS8PNRJlWEI2eZDYLixt78/mZNq9L0osoiE9UiFPj6dOMHBSoUIydW4FTzQnEC1pMr1puSCjCyIsE2x8w0hpDZK0uJkCiqbbCBmLTTl6jaKGs9rOea3pyYmmiVZI1/VZfdKp5GSlrZG4xgabucp0WLNJo5qVOE5Cx6futNfq00I8yZhnVlVicIgc0zoemoqrxmYmiogu0UJrCp5uMeIT6dgNvhB20bdjc

how do i convert this msg format into mp3 and play this live in web? here is small code i've written using roslibjs:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />

<script type="text/javascript" src="http://static.robotwebtools.org/EventEmitter2/current/eventemitter2.min.js"></script>
<script type="text/javascript" src="http://static.robotwebtools.org/roslibjs/current/roslib.min.js"></script>
</head>

<body>
  <h1>Simple roslib Example</h1>
  <p id="text"></p>
  <audio id="audio" autoplay=true type="audio/mpeg"></audio>
<script type="text/javascript" type="text/javascript">
  // Connecting to ROS
  // -----------------

  var ros = new ROSLIB.Ros({
    url : 'ws://localhost:9090'
  });

  ros.on('connection', function() {
    console.log('Connected to websocket server.');
  });

  ros.on('error', function(error) {
    console.log('Error connecting to websocket server: ', error);
  });

  ros.on('close', function() {
    console.log('Connection to websocket server closed.');
  });

  // Subscribing to a Topic
  // ----------------------

  var audio = new ROSLIB.Topic({
    ros : ros,
    name : '/audio/audio',
    messageType : 'audio_common_msgs/AudioData'
  });

  var video = new ROSLIB.Topic({
    ros : ros,
    name : '/cv_camera/image_raw/compressed',
    messageType : 'sensor_msgs/CompressedImage'
  });

  var audioObject = document.createElement("AUDIO");
  audioObject.autoplay = true;
  audioObject.type = 'audio/mpeg';

  audio.subscribe(function(message) {
    debugger;
    console.log('Received message on ' + audio.name + ': ' + message.data); // message.data is audio data
    audioObject.src = message.data;
    document.getElementById("audio").src = message.data;
    document.getElementById("text").innerHTML = message.data; 
    //audio.unsubscribe();
  });

  video.subscribe(function(message) {
    console.log('Received message on ' + video.name + ': ' + message.data); // message.data is audio data
    video.unsubscribe();
  });
</script>
</body>
</html>
edit retag flag offensive close merge delete