Browse Source

Audio using Howler library

master
Inderjit Gill 1 year ago
parent
commit
4115f625bf
13 changed files with 107 additions and 23 deletions
  1. +12
    -21
      package-lock.json
  2. +3
    -0
      package.json
  3. +33
    -0
      src/game.rs
  4. +2
    -2
      src/harbour.rs
  5. +5
    -0
      src/lib.rs
  6. +43
    -0
      web/js/Audio.js
  7. +9
    -0
      web/js/index.js
  8. BIN
      web/sfx/fanfare1.mp3
  9. BIN
      web/sfx/fanfare2.mp3
  10. BIN
      web/sfx/fanfare3.mp3
  11. BIN
      web/sfx/negative1.mp3
  12. BIN
      web/sfx/pause2_in.mp3
  13. BIN
      web/sfx/pause2_out.mp3

+ 12
- 21
package-lock.json View File

@@ -1986,14 +1986,12 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@@ -2008,20 +2006,17 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"core-util-is": {
"version": "1.0.2",
@@ -2138,8 +2133,7 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"ini": {
"version": "1.3.5",
@@ -2151,7 +2145,6 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@@ -2166,7 +2159,6 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@@ -2174,14 +2166,12 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"minipass": {
"version": "2.2.4",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"safe-buffer": "^5.1.1",
"yallist": "^3.0.0"
@@ -2200,7 +2190,6 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"minimist": "0.0.8"
}
@@ -2281,8 +2270,7 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
"dev": true,
"optional": true
"dev": true
},
"object-assign": {
"version": "4.1.1",
@@ -2294,7 +2282,6 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"wrappy": "1"
}
@@ -2416,7 +2403,6 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
"optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@@ -2682,6 +2668,11 @@
"integrity": "sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==",
"dev": true
},
"howler": {
"version": "2.0.14",
"resolved": "https://registry.npmjs.org/howler/-/howler-2.0.14.tgz",
"integrity": "sha512-U2ao6mRaPjjzOtwQTDA2OMk9ftQ+k+CrdaBrSIxcO6artSNBtK4kwdx8vUoajMvv6vGZWYiQOmBYnffOFWHt7w=="
},
"hpack.js": {
"version": "2.1.6",
"resolved": "https://registry.npmjs.org/hpack.js/-/hpack.js-2.1.6.tgz",

+ 3
- 0
package.json View File

@@ -11,5 +11,8 @@
"webpack": "4.16.1",
"webpack-cli": "3.1.0",
"webpack-dev-server": "3.1.4"
},
"dependencies": {
"howler": "2.0.14"
}
}

+ 33
- 0
src/game.rs View File

@@ -2,6 +2,7 @@ use controller::Controller;
use error;
use geometry::Geometry;
use log;
use audio_play;
use sen_colour::{Colour, Format};
use sprite::Sprite;
use std::collections::HashMap;
@@ -9,6 +10,17 @@ use units::*;
use Config;

#[derive(Clone, Copy)]
pub enum SoundEffect {
GameOver = 1,
Line1 = 2,
Line2 = 3,
Line3 = 4,
Line4 = 5,
PauseIn = 6,
PauseOut = 7,
}

#[derive(Clone, Copy)]
pub struct Block {
occupied: bool,
offset: Vec2D,
@@ -277,6 +289,14 @@ impl Game {
}
}

pub fn game_mode_changed_to_paused(&self) {
play_sound(SoundEffect::PauseIn);
}

pub fn game_mode_changed_to_playing(&self) {
play_sound(SoundEffect::PauseOut);
}

// counting on the fact that delta will always be a 'sensible' value.
// so if the player has paused the game for a while, on the first resumed call to tick_playing
// delta should be 0 rather than a large multi-second value
@@ -328,11 +348,20 @@ impl Game {
}

if num_lines > 0 {
match num_lines {
1 => play_sound(SoundEffect::Line1),
2 => play_sound(SoundEffect::Line2),
3 => play_sound(SoundEffect::Line3),
4 => play_sound(SoundEffect::Line4),
_ => ()
};

update_score(self, num_lines);
}

use_next_shape(self, random)?;
if check_for_game_over(self) {
play_sound(SoundEffect::GameOver);
log(&format!("i_count: {}", self.i_count));
log(&format!("j_count: {}", self.j_count));
log(&format!("l_count: {}", self.l_count));
@@ -362,6 +391,10 @@ impl Game {
}
}

fn play_sound(sound_effect: SoundEffect) {
audio_play(sound_effect as i32);
}

fn update_score(game: &mut Game, num_lines: i32) {
game.lines += num_lines;
game.score += score_for_current_move(game, num_lines);

+ 2
- 2
src/harbour.rs View File

@@ -62,10 +62,10 @@ impl Harbour {

if button == ControllerButton::Start && action == ControllerAction::Up {
if self.mode == GameMode::Playing {
log("Paused");
self.game.game_mode_changed_to_paused();
self.mode = GameMode::Paused;
} else {
log("Playing");
self.game.game_mode_changed_to_playing();
self.mode = GameMode::Playing;
self.resume_from_paused = true;
return true;

+ 5
- 0
src/lib.rs View File

@@ -29,6 +29,11 @@ extern "C" {
}

#[wasm_bindgen]
extern {
fn audio_play(id: i32);
}

#[wasm_bindgen]
pub struct KeyEventReturn {
call_tick: bool,
prevent_default: bool,

+ 43
- 0
web/js/Audio.js View File

@@ -0,0 +1,43 @@
import { Howl, Howler } from "howler";

export class Audio {
constructor(config) {
this.game_over = new Howl({
src: ['/web/sfx/negative1.mp3']
});
this.line1 = new Howl({
src: ['/web/sfx/fanfare1.mp3']
});
this.line2 = new Howl({
src: ['/web/sfx/fanfare1.mp3']
});
this.line3 = new Howl({
src: ['/web/sfx/fanfare2.mp3']
});
this.line4 = new Howl({
src: ['/web/sfx/fanfare3.mp3']
});
this.pause_in = new Howl({
src: ['/web/sfx/pause2_in.mp3']
});
this.pause_out = new Howl({
src: ['/web/sfx/pause2_out.mp3']
});
}

play(label) {
console.log(`audio: play ${label}`);

switch(label) {
case 1: this.game_over.play(); break;
case 2: this.line1.play(); break;
case 3: this.line2.play(); break;
case 4: this.line3.play(); break;
case 5: this.line4.play(); break;
case 6: this.pause_in.play(); break;
case 7: this.pause_out.play(); break;
default: console.error(`Audio: unable to play: ${label}`);
}

}
}

+ 9
- 0
web/js/index.js View File

@@ -1,6 +1,7 @@
import { Bridge, Config } from "../wasm_tetris";
import { memory } from "../wasm_tetris_bg";
import { GLRenderer } from "./GLRenderer";
import { Audio } from "./Audio";

let gState = {
canvasId: 'render-canvas',
@@ -12,6 +13,7 @@ let gState = {
bridge: undefined,
animationId: undefined,
renderer: undefined,
audio: undefined,

performanceNow: performance.now()
};
@@ -59,8 +61,13 @@ function reconfigure() {
gState.bridge.reconfigure(gState.config);
}

function audio_play(id) {
gState.audio.play(id);
}

window.reconfigure = reconfigure;
window.config = gState.config;
window.audio_play = audio_play;

function get_canvas_aspect_ratio(config) {

@@ -133,6 +140,8 @@ function main() {

resize_canvas_element(canvasElement, gState.config);

gState.audio = new Audio(config);

let renderer = new GLRenderer(gState.config, canvasElement);
renderer.loadTexture(gState.tileset).then(([tilesetWidth, tilesetHeight]) => {


BIN
web/sfx/fanfare1.mp3 View File


BIN
web/sfx/fanfare2.mp3 View File


BIN
web/sfx/fanfare3.mp3 View File


BIN
web/sfx/negative1.mp3 View File


BIN
web/sfx/pause2_in.mp3 View File


BIN
web/sfx/pause2_out.mp3 View File


Loading…
Cancel
Save