DanielSant0s / AthenaEnv

A complete Javascript environment for creating homebrew applications and games on PlayStation 2.
GNU General Public License v3.0
156 stars 20 forks source link

How do we have two audio streams #51

Closed ChristienChapman closed 6 months ago

ChristienChapman commented 7 months ago

I have tried to make two audio streams using wav. One is for music and the other for sounds. I have separated the functions but anytime you play a sound it cancels out the music and then stalls all audio altogether. Thanks.

`

IOP.loadDefaultModule(IOP.keyboard); Keyboard.init();

let bg = new Image("SWTC/LVL_PS2/Menu/background.png"); let menu_box_mesh = new Image("SWTC/LVL_PS2/Menu/menu_box_mesh.png"); let build = "Development Build 2"; let build_date = "3/29/2024"; let program_status = "menu"; let menu_option = 1; var menu_text_animation = 32;

var unsel_color = Color.new(255, 255, 255, 64); var sel_color = Color.new(17,49,255, 32);

let font = new Font("SWTC/LVL_PS2/Menu/CONSOLAB.TTF"); let font_medium = new Font("SWTC/LVL_PS2/Menu/CONSOLAB.TTF"); let font_bold = new Font("SWTC/LVL_PS2/Menu/CONSOLAB.TTF"); font.color = unsel_color; font_bold.scale = 0.7f font_medium.scale = 1.0f; font.scale = 0.44f; let font_text_box = new Font("SWTC/LVL_PS2/Menu/CONSOLAB.TTF") let font_text_box_selected = new Font("SWTC/LVL_PS2/Menu/CONSOLAB.TTF") font_text_box_selected.color = sel_color; let font_animation_sequence = 1;

function random(max) {

return Math.floor(Math.random() * max + 1);

}

let music_stream_timer = Timer.new(); let sound_stream_timer = Timer.new(); let music_stream = Sound.load("SWTC/LVL_PS2/Music/Menu" + random(3) + ".wav"); let sound_stream = Sound.load("SWTC/LVL_PS2/Sounds/back.wav"); let music_stream_duration; let sound_stream_duration; let local_duration = (Math.floor(Timer.getTime(music_stream_timer))); let local_sound_duration = (Math.floor(Timer.getTime(sound_stream_timer)));

//Music

music_stream_duration = Sound.duration(music_stream); Sound.play(music_stream); Timer.reset(music_stream_timer);

sound_stream_duration = Sound.duration(sound_stream); Sound.play(sound_stream); Timer.reset(sound_stream_timer);

let menu_ptr = 0;

let new_pad = Pads.get(); let old_pad = new_pad;

let old_kbd_char = 0; let kbd_char = 0;

const VK_OLD_UP = 27; const VK_NEW_UP = 44; const VK_OLD_DOWN = 27; const VK_NEW_DOWN = 43; const VK_RETURN = 10;

var ee_info = System.getCPUInfo();

function textAnimation(interval,integer) {

if(font_animation_sequence === 1) { integer = integer + interval; } else { integer = integer - interval; }

if(integer === 99) { font_animation_sequence = 2;

}

if(integer === 31) { font_animation_sequence = 1;

}

return integer;

}

function optionCeiling(numOfOptions,compare) {

if(compare > numOfOptions) { compare = 1; }

if(compare === 0) { compare = numOfOptions; }

return compare;

}

function print(posx, posy, text, type) {

if(type === "text_box") {

font_text_box_selected.print(posx, posy, text); } else {

if(type === "") {

font_text_box.print(posx, posy, text); }

}

}

function text_button(posx, posy, image, text, id) {

if(image === "menu_box_mesh") {

menu_box_mesh.draw(posx,posy); menu_box_mesh.width = 150; menu_box_mesh.height = 30;

}

function play_sound(sound) {

switch(sound){

case "select":

local_sound_duration = 0;

break;

}

}

if(menu_option === id) { print(posx,posy,text,"text_box"); } else { print(posx,posy,text,""); } }

function image_button(posx, posy, image) {

}

os.setInterval(() => {

old_pad = new_pad;
new_pad = Pads.get();

old_kbd_char = kbd_char;
kbd_char = Keyboard.get();

Screen.clear();

if(program_status === "menu") {

if(local_duration >= music_stream_duration) { local_duration = 0; Sound.pause(music_stream); let new_music_stream = Sound.load("SWTC/LVL_PS2/Music/Menu" + random(3) + ".wav"); music_stream_duration = Sound.duration(new_music_stream); Timer.reset(music_stream_timer); Sound.play(new_music_stream); let old_music_stream = music_stream; music_stream = new_music_stream; Sound.free(old_music_stream); }

local_duration = (Math.floor(Timer.getTime(music_stream_timer))); local_sound_duration = (Math.floor(Timer.getTime(sound_stream_timer))); print(30,30,"ld" + local_duration,"text_box"); print(30,50,"md" + music_stream_duration,"text_box"); print(30,90,"ld" + local_sound_duration,"text_box"); print(30,140,"md" + sound_stream_duration,"text_box"); menu_text_animation = textAnimation(4,menu_text_animation);

font_text_box_selected.color = Color.new(17,49,255,menu_text_animation);;

print(10,10,menu_option,"");
menu_option = optionCeiling(3,menu_option);

text_button(190, 125, "menu_box_mesh", "Campaign",1); text_button(190, 125+45, "menu_box_mesh", "Options",2); text_button(190, 125+45*2, "menu_box_mesh", "Exit Game",3);

bg.draw(0, 0);

print(15, 5, "Star Wars Tactical Command" + build + "" + build_date,"");

if(Pads.check(new_pad, Pads.UP) && !Pads.check(old_pad, Pads.UP) || old_kbd_char == VK_OLD_UP && kbd_char == VK_NEW_UP) {

menu_option = menu_option - 1;

let new_sound_stream = Sound.load("SWTC/LVL_PS2/Sounds/select.wav"); sound_stream_duration = Sound.duration(new_sound_stream); Timer.reset(sound_stream_timer); Sound.play(new_sound_stream); Sound.resume(music_stream); let old_sound_stream = sound_stream; sound_stream = new_sound_stream; Sound.free(old_sound_stream);

}

if(Pads.check(new_pad, Pads.DOWN) && !Pads.check(old_pad, Pads.DOWN) || old_kbd_char == VK_OLD_DOWN && kbd_char == VK_NEW_DOWN){

menu_option = menu_option + 1;

Sound.play(select);

}

if(Pads.check(new_pad, Pads.CROSS) && !Pads.check(old_pad, Pads.CROSS) || kbd_char == VK_RETURN){

Sound.play(affirm);

  }

  if(Pads.check(new_pad, Pads.CIRCLE) && !Pads.check(old_pad, Pads.CIRCLE)){

Sound.play(back);

  }

}

Screen.flip();

}, 0);

`