• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Dealing with external mp3 files

W

Woowoo

Guest
Hi!
I'm using the soundcloud API to download external mp3 files for my game, but it can only download mp3 files.
I couldn't find a way to play external mp3 files, and I even tried to make my own mp3 decoder in gml (that didn't end well). Is there a way to convert an already existing mp3 file into an ogg file with gamemaker, or play an mp3 file? All help is appreciated. :)

One other thing i should mention: I want to avoid using DLLs, but at this point it seems like the only option.
 
W

Woowoo

Guest
So after I get the audio data, I can just play it like normal with audio_play_sound()?
This is what I made from what you showed me (It's in a script):
Code:
var fname = argument0;
var result = 0;
if(file_exists(fname)){
    file = file_bin_open(fname,0);
    var size = file_bin_size(file);
    var gameTransfer = buffer_create(1024, buffer_grow, 1);
    for(var i=0;i<size;i+=1;){
        buffer_write(gameTransfer,buffer_s16,file_bin_read_byte(file));
    }
    result = audio_create_buffer_sound(gameTransfer,buffer_u8,44100,0,10,audio_stereo);
    file_bin_close(file);
}
return result;
if !file_exists(fname) show_debug_message("file_open_bin has failed to open the file");
 
W

Wraithious

Guest
Yep that should put it in the result variable in your script which then gets returned to the variable that called your script
 
W

Woowoo

Guest
I create an audio buffer from the result of the script, but it only plays whitenoise, I think its because mp3 files are compressed. Do you know how I could uncompress it? (thanks for your help so far)
Code:
sngbuff = wae_create_sound("assets\musicDefault\Mosaic-Emanations_from_Beyond.ogg");
sng = audio_create_buffer_sound(sngbuff,buffer_s16,44100,0,44100*10,audio_stereo);
audio_play_sound(sng,10,false);
 

poliver

Member
mp3 files are not raw pulse-code modulation like wavs of aiffs. playing them back as such would yield same results as playing a text file through the speakers.

mp3 algorithms remove 'sounds' dynamicaly that matter the least to a human ear using psychoacoustic principles thus reducing filesize. you need an mp3 decoder to play them back.

you can't uncompress mp3 files. you can convert/transcode only.
 
W

Wraithious

Guest
Yep what @Roman P. Said is correct, in my example topics I linked I was taking a file from the included files and transferimg it to a file the user or the developer could access and then use the file address to play the file, if it's .ogg you can use the audio create stream to play it, if not you need to implement MediaStore in your extension (android) or the equivolent in gms (if it exists) or the equivolent in a windows extension
 
W

Wraithious

Guest
Sorry I don't, when yoyo added the record audio functions it didnt work on android so that's why I tackled that project to make an extension that does that and more for android, hopefully someone on here knows more on the windows version. I would think tho is all you need to do is put the resulting .ogg into a file and use the myVariable = audio_create_stream( "filePath" ) ; and then play it with audio_play_sound(myVariable) ;
 
W

Woowoo

Guest
I figured it out! I set up a node js web server, and instead of making the request to the soundcloud api (which only supports mp3), I make the request to the node js server. The server gets the file, converts it to an ogg, then sends it to the client.

Code:
//I'll be honest, I have no idea what i'm doing
const fileSystem = require('fs');
const path = require('path');
const express = require('express');
const request = require('request');

var app = express();

app.get('/convert', function(req, res){
    var url = 'http://api.soundcloud.com/tracks/'+req.query.track+'/stream?client_id='+req.query.client_id;
    request(url, { json: true }, (err, resp, body) => {
        if (err) { return console.log(err); }
        console.log(Object.getOwnPropertyNames(resp));
        console.log('statusCode:', resp.statusCode);
        fileSystem.writeFile(req.query.track+".mp3", resp, function(err) {
            if(err) {return console.log(err);}
        });
    });
   
  res.send("Hi");
  console.log("Requested:   "+req.query.track+"  "+req.query.client_id);
});

app.listen(3000);
console.log('Listening on port 3000');
its not finished yet
 
T

Tony Long

Guest
How do I make a song still playe when turn off or sleep phone?
 
Top