Skip to content

Commit 82522d8

Browse files
authored
Bugfix emscripten audio loading (#7632)
#changelog #emscripten
1 parent 4a7833f commit 82522d8

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,41 @@ var LibraryHTML5Audio = {
8080
},
8181

8282
html5audio_sound_load: function (player_id, url) {
83-
AUDIO.player[player_id].src = UTF8ToString(url);
83+
try {
84+
var filePath = UTF8ToString(url);
85+
86+
if( filePath.indexOf("http") == 0 ) {
87+
AUDIO.player[player_id].src = filePath;
88+
}else{
89+
90+
//console.log("file path is" + filePath);
91+
92+
var data = FS.readFile(filePath, { encoding: 'binary' });
93+
94+
var ext = filePath.split('.').pop();
95+
96+
var stats = FS.stat(filePath)
97+
var fileSizeInBytes = stats.size;
98+
99+
var tag = ext; //this covers most types
100+
if( ext == mp3 ){
101+
tag = 'mpeg';
102+
}else if( ext == 'oga'){
103+
tag = 'ogg';
104+
}else if( ext == 'weba'){
105+
tag = 'webm';
106+
}
107+
108+
const blob = new Blob([data], { type: 'audio/' + tag });
109+
const audioSrc = URL.createObjectURL(blob);
110+
111+
AUDIO.player[player_id].src = audioSrc;
112+
}
113+
114+
} catch (error) {
115+
console.error('Error reading file:' + filePath + " " + error);
116+
}
117+
84118
},
85119

86120
html5audio_sound_play: function (player_id, multiplay, volume, speed, pan, offset) {

addons/ofxEmscripten/src/ofxEmscriptenSoundPlayer.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ ofxEmscriptenSoundPlayer::~ofxEmscriptenSoundPlayer(){
3737
html5audio_sound_free(player_id);
3838
}
3939

40-
bool ofxEmscriptenSoundPlayer::load(const std::filesystem::path& fileName, bool stream){
41-
html5audio_sound_load(player_id, ofToDataPath(fileName).c_str());
40+
bool ofxEmscriptenSoundPlayer::load(const of::filesystem::path& filePath, bool stream){
41+
auto soundFilePath = filePath.string();
42+
if ( soundFilePath.substr(0, 7) != "http://" && soundFilePath.substr(0, 8) != "https://"){
43+
soundFilePath = ofToDataPath(soundFilePath);
44+
}
45+
html5audio_sound_load(player_id, soundFilePath.c_str());
4246
return true;
4347
}
4448

45-
bool ofxEmscriptenSoundPlayer::load(const std::string& fileName, bool stream){
46-
html5audio_sound_load(player_id, fileName.c_str());
47-
return true;
48-
}
49+
//bool ofxEmscriptenSoundPlayer::load(const std::string& fileName, bool stream){
50+
// html5audio_sound_load(player_id, fileName.c_str());
51+
// return true;
52+
//}
4953

5054
void ofxEmscriptenSoundPlayer::unload(){
5155
html5audio_sound_free(player_id);

addons/ofxEmscripten/src/ofxEmscriptenSoundPlayer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ofxEmscriptenSoundPlayer: public ofBaseSoundPlayer {
88
ofxEmscriptenSoundPlayer();
99
~ofxEmscriptenSoundPlayer();
1010

11-
bool load(const std::filesystem::path& fileName, bool stream = false);
12-
bool load(const std::string& fileName, bool stream = false);
11+
bool load(const of::filesystem::path& fileName, bool stream = false);
12+
// bool load(const std::string& fileName, bool stream = false);
1313
void unload();
1414
void play();
1515
void stop();

0 commit comments

Comments
 (0)