Skip to content

Commit

Permalink
Merge pull request #655 from co-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
release-train[bot] committed Feb 3, 2020
2 parents 9cd6222 + aa4a012 commit 2f293ad
Show file tree
Hide file tree
Showing 12 changed files with 946 additions and 658 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"web-audio-test-api": "^0.5.2",
"webpack": "4.20.2",
"webpack-dev-middleware": "^3.4.0",
"webpack-dev-server": "3.1.11",
"webpack-dev-server": "3.10.2",
"worker-loader": "^2.0.0",
"yn": "^1.3.0"
},
Expand Down
20 changes: 19 additions & 1 deletion packages/bemuse-notechart/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,32 @@ export class Notechart {
return this._spacing.factor(beat)
}

/**
* Gets the keyMode from scratch
* @param scratch
* @returns {string}
*/
getKeyMode(scratch: string): string {
const usedColumns: { [column: string]: boolean } = {}
for (const note of this.notes) {
usedColumns[note.column] = true
}
if (scratch === 'off' && !usedColumns['1'] && !usedColumns['7']) return '5K'
if (scratch === 'left' && !usedColumns['6'] && !usedColumns['7'])
return '5K'
if (scratch === 'right' && !usedColumns['1'] && !usedColumns['2'])
return '5K'
return '7K'
}

_preTransform(
bmsNotes: BMS.BMSNote[],
playerOptions: Partial<PlayerOptions>
) {
let chain = _.chain(bmsNotes)
let keys = getKeys(bmsNotes)
if (playerOptions.scratch === 'off') {
chain = chain.map(note => {
chain = chain.map((note: BMS.BMSNote) => {
if (note.column && note.column === 'SC') {
return Object.assign({}, note, { column: null })
} else {
Expand Down
71 changes: 29 additions & 42 deletions packages/bemuse-tools/src/audio.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Promise from 'bluebird'
import co from 'co'
import fs from 'fs'
import Throat from 'throat'
import { cpus } from 'os'
Expand Down Expand Up @@ -28,47 +27,35 @@ export class AudioConvertor {
_doConvert(path, type) {
return this._SoX(path, type)
}
// TODO [#621]: Convert the `_SoX` method to async function (instead of using `co`) in [bemuse-tools] src/audio.js
// See issue #575 for more details.
_SoX(path, type) {
return co(
function*() {
let typeArgs = []
try {
let fd = yield Promise.promisify(fs.open, fs)(path, 'r')
let buffer = Buffer.alloc(4)
let read = yield Promise.promisify(fs.read, fs)(
fd,
buffer,
0,
4,
null
)
yield Promise.promisify(fs.close, fs)(fd)
if (read === 0) {
console.error('[WARN] Empty keysound file.')
} else if (
buffer[0] === 0x49 &&
buffer[1] === 0x44 &&
buffer[2] === 0x33
) {
typeArgs = ['-t', 'mp3']
} else if (buffer[0] === 0xff && buffer[1] === 0xfb) {
typeArgs = ['-t', 'mp3']
} else if (
buffer[0] === 0x4f &&
buffer[1] === 0x67 &&
buffer[2] === 0x67 &&
buffer[3] === 0x53
) {
typeArgs = ['-t', 'ogg']
}
} catch (e) {
console.error('[WARN] Unable to detect file type!')
}
return yield this._doSoX(path, type, typeArgs)
}.bind(this)
)
async _SoX(path, type) {
let typeArgs = []
try {
let fd = await Promise.promisify(fs.open)(path, 'r')
let buffer = Buffer.alloc(4)
let read = await Promise.promisify(fs.read)(fd, buffer, 0, 4, null)
await Promise.promisify(fs.close)(fd)
if (read === 0) {
console.error('[WARN] Empty keysound file.')
} else if (
buffer[0] === 0x49 &&
buffer[1] === 0x44 &&
buffer[2] === 0x33
) {
typeArgs = ['-t', 'mp3']
} else if (buffer[0] === 0xff && buffer[1] === 0xfb) {
typeArgs = ['-t', 'mp3']
} else if (
buffer[0] === 0x4f &&
buffer[1] === 0x67 &&
buffer[2] === 0x67 &&
buffer[3] === 0x53
) {
typeArgs = ['-t', 'ogg']
}
} catch (e) {
console.error('[WARN] Unable to detect file type!')
}
return this._doSoX(path, type, typeArgs)
}
_doSoX(path, type, inputTypeArgs) {
return throat(
Expand Down
30 changes: 13 additions & 17 deletions src/auto-synchro/music/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash'
import co from 'co'
import context from 'bemuse/audio-context'
import download from 'bemuse/utils/download'
import SamplingMaster from 'bemuse/sampling-master'
Expand All @@ -17,24 +16,21 @@ let ASSET_URLS = {
/**
* Loads the files and create a music instance.
*/
export function load() {
// TODO [#626]: Convert the `load` function to async function (instead of using `co`) in src/auto-synchro/music.js
// See issue #575 for more details.
return co(function*() {
let master = new SamplingMaster(context)
let sample = name =>
download(ASSET_URLS[`${name}.ogg`])
.as('arraybuffer')
.then(buf => master.sample(buf))
let samples = _.fromPairs(
yield Promise.all(
['bgm', 'intro', 'kick', 'snare'].map(name =>
sample(name).then(sampleObj => [name, sampleObj])
)
export async function load() {
let master = new SamplingMaster(context)

let sample = name =>
download(ASSET_URLS[`${name}.ogg`])
.as('arraybuffer')
.then(buf => master.sample(buf))
let samples = _.fromPairs(
await Promise.all(
['bgm', 'intro', 'kick', 'snare'].map(name =>
sample(name).then(sampleObj => [name, sampleObj])
)
)
return music(master, samples)
})
)
return music(master, samples)
}

/**
Expand Down
92 changes: 44 additions & 48 deletions src/coming-soon/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import $ from 'jquery'
import { Compiler, Notes, Reader, SongInfo, Timing } from 'bms'
import DndResources from 'bemuse/resources/dnd-resources'
import SamplingMaster from 'bemuse/sampling-master'
import co from 'co'
import ctx from 'bemuse/audio-context'

import template from './template.jade'
Expand Down Expand Up @@ -33,56 +32,53 @@ export function main(element) {
}
}

// TODO [#627]: Convert the `go` function to async function (instead of using `co`) in src/coming-soon/demo/index.js
// See issue #575 for more details.
function go(loader, element) {
async function go(loader, element) {
let master = new SamplingMaster(ctx)
let $log = element.find('.js-log')
let $play = element.find('.js-play').hide()
let $sampler = element.find('.js-sampler')

co(function*() {
log('Loading file list')
let list = yield loader.fileList
let bmsFile = list.filter(f => f.match(/\.(?:bms|bme|bml|pms)$/i))[0]
log('Loading ' + bmsFile)
log('Loading file list')
let list = await loader.fileList
let bmsFile = list.filter(f => f.match(/\.(?:bms|bme|bml|pms)$/i))[0]
log('Loading ' + bmsFile)

let arraybuffer = yield (yield loader.file(bmsFile)).read()
let buffer = Buffer.from(new Uint8Array(arraybuffer))
let text = yield Promise.promisify(Reader.readAsync)(buffer)
let chart = Compiler.compile(text).chart
var timing = Timing.fromBMSChart(chart)
var notes = Notes.fromBMSChart(chart)
var info = SongInfo.fromBMSChart(chart)
$('<pre wrap></pre>')
.text(JSON.stringify(info, null, 2))
.appendTo($sampler)
log('Loading samples')
var samples = yield loadSamples(notes, chart)
log('Click the button to play!')
yield waitForPlay()
void (function() {
master.unmute()
for (let note of notes.all()) {
setTimeout(() => {
let sample = samples[note.keysound]
if (!sample) {
console.log('warn: unknown sample ' + note.keysound)
return
}
let span = $('<span></span>')
.text('[' + note.keysound + '] ')
.appendTo($sampler)
let instance = sample.play()
$sampler[0].scrollTop = $sampler[0].scrollHeight
instance.onstop = function() {
span.addClass('is-off')
}
}, timing.beatToSeconds(note.beat) * 1000)
}
return false
})()
}).done()
let loadedFile = await loader.file(bmsFile)
let arraybuffer = await loadedFile.read()
let buffer = Buffer.from(new Uint8Array(arraybuffer))
let text = await Promise.promisify(Reader.readAsync)(buffer)
let chart = Compiler.compile(text).chart
var timing = Timing.fromBMSChart(chart)
var notes = Notes.fromBMSChart(chart)
var info = SongInfo.fromBMSChart(chart)
$('<pre wrap></pre>')
.text(JSON.stringify(info, null, 2))
.appendTo($sampler)
log('Loading samples')
var loadedSamples = await loadSamples(notes, chart)
log('Click the button to play!')
await waitForPlay()
void (function() {
master.unmute()
for (let note of notes.all()) {
setTimeout(() => {
let sample = loadedSamples[note.keysound]
if (!sample) {
console.log('warn: unknown sample ' + note.keysound)
return
}
let span = $('<span></span>')
.text('[' + note.keysound + '] ')
.appendTo($sampler)
let instance = sample.play()
$sampler[0].scrollTop = $sampler[0].scrollHeight
instance.onstop = function() {
span.addClass('is-off')
}
}, timing.beatToSeconds(note.beat) * 1000)
}
return false
})()

function waitForPlay() {
return new Promise(function(resolve) {
Expand All @@ -98,17 +94,17 @@ function go(loader, element) {
$log.text(t)
}

function loadSamples(notes, chart) {
function loadSamples(_notes, _chart) {
var samples = {}
var promises = []
let completed = 0

for (let note of notes.all()) {
for (let note of _notes.all()) {
let keysound = note.keysound
if (!(keysound in samples)) {
samples[keysound] = null
promises.push(
loadKeysound(chart.headers.get('wav' + keysound))
loadKeysound(_chart.headers.get('wav' + keysound))
.then(blob => master.sample(blob))
.then(sample => (samples[keysound] = sample))
.catch(e => console.error('Unable to load ' + keysound + ': ' + e))
Expand Down
21 changes: 5 additions & 16 deletions src/game/display/player-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import { getGauge } from './Gauge'

export class PlayerDisplay {
constructor(player, skinData) {
let notechart = player.notechart
this._currentSpeed = 1
this._player = player
this._noteArea = new NoteArea(notechart.notes, notechart.barLines)
this._noteArea = new NoteArea(
player.notechart.notes,
player.notechart.barLines
)
this._stateful = {}
this._defaultData = {
placement: player.options.placement,
scratch: player.options.scratch,
key_mode: getKeyMode(notechart, player.options.scratch),
key_mode: player.notechart.getKeyMode(player.options.scratch),
lane_lift: Math.max(0, -player.options.laneCover),
lane_press: Math.max(0, player.options.laneCover),
}
Expand Down Expand Up @@ -221,16 +223,3 @@ export class PlayerDisplay {
}

export default PlayerDisplay

// TODO [#629]: MOVE THIS (getKeyMode) TO bemuse-notechart
//
function getKeyMode(notechart, scratch) {
const usedColumns = {}
for (const note of notechart.notes) {
usedColumns[note.column] = true
}
if (scratch === 'off' && !usedColumns['1'] && !usedColumns['7']) return '5K'
if (scratch === 'left' && !usedColumns['6'] && !usedColumns['7']) return '5K'
if (scratch === 'right' && !usedColumns['1'] && !usedColumns['2']) return '5K'
return '7K'
}

0 comments on commit 2f293ad

Please sign in to comment.