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 May 3, 2020
2 parents 3e167fb + 5f5707f commit 36a10db
Show file tree
Hide file tree
Showing 16 changed files with 1,021 additions and 780 deletions.
3 changes: 3 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tasks:
- init: yarn
command: yarn start
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
54 changes: 24 additions & 30 deletions packages/bemuse-tools/src/bemuse-packer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Promise from 'bluebird'
import co from 'co'
import path from 'path'
import fs from 'fs'
import Payload from './payload'
Expand Down Expand Up @@ -27,37 +26,32 @@ export class BemusePacker {
this._refs.push(ref)
return ref
}
// TODO [#622]: Convert the `write` method to async function (instead of using `co`) in [bemuse-tools] src/bemuse-packer.js
// See issue #575 for more details.
write(folder) {
return co(
function*() {
let files = []
let refs = []
let nums = {}
for (let ref of this._refs) {
let payload = new Payload()
for (let file of ref.files) {
let [start, end] = payload.add(file.buffer)
files.push({ name: file.name, ref: [ref.index, start, end] })
}
let hash = payload.hash
let num = (nums[ref.name] || 0) + 1
nums[ref.name] = num
let out = ref.name + '.' + num + '.' + hash.substr(0, 8) + '.bemuse'
refs.push({ path: out, hash: hash })
yield this._writeBin(path.join(folder, out), Buffer.alloc(0), payload)
console.log(`Written ${out}`)
}
let metadata = { files, refs }
yield writeFile(
path.join(folder, 'metadata.json'),
JSON.stringify(metadata)
)
console.log(`Written metadata.json`)
}.bind(this)
async write(folder) {
let files = []
let refs = []
let nums = {}
for (let ref of this._refs) {
let payload = new Payload()
for (let file of ref.files) {
let [start, end] = payload.add(file.buffer)
files.push({ name: file.name, ref: [ref.index, start, end] })
}
let hash = payload.hash
let num = (nums[ref.name] || 0) + 1
nums[ref.name] = num
let out = ref.name + '.' + num + '.' + hash.substr(0, 8) + '.bemuse'
refs.push({ path: out, hash: hash })
await this._writeBin(path.join(folder, out), Buffer.alloc(0), payload)
console.log(`Written ${out}`)
}
let metadata = { files, refs }
await writeFile(
path.join(folder, 'metadata.json'),
JSON.stringify(metadata)
)
console.log(`Written metadata.json`)
}

_writeBin(path, metadataBuffer, payload) {
let file = fs.createWriteStream(path)
let size = Buffer.alloc(4)
Expand Down
45 changes: 20 additions & 25 deletions packages/bemuse-tools/src/packer.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 _ from 'lodash'

Expand All @@ -12,30 +11,26 @@ import BemusePacker from './bemuse-packer'
let mkdirp = Promise.promisify(require('mkdirp'))
let fileStat = Promise.promisify(fs.stat, fs)

// TODO [#624]: Convert the `packIntoBemuse` function to async function (instead of using `co`) in [bemuse-tools] src/packer.js
// See issue #575 for more details.
export function packIntoBemuse(path) {
return co(function*() {
let stat = yield fileStat(path)
if (!stat.isDirectory()) throw new Error('Not a directory: ' + path)

let directory = new Directory(path)
let packer = new BemusePacker(directory)

console.log('-> Loading audios')
let audio = yield directory.files('*.{mp3,wav,ogg}')

console.log('-> Converting audio to ogg [better audio performance]')
let oggc = new AudioConvertor('ogg', '-C', '3')
oggc.force = true
let oggs = yield dotMap(audio, file => oggc.convert(file))
packer.pack('ogg', oggs)

console.log('-> Writing...')
let out = join(path, 'assets')
yield mkdirp(out)
yield packer.write(out)
})
export async function packIntoBemuse(path) {
let stat = await fileStat(path)
if (!stat.isDirectory()) throw new Error('Not a directory: ' + path)

let directory = new Directory(path)
let packer = new BemusePacker(directory)

console.log('-> Loading audios')
let audio = await directory.files('*.{mp3,wav,ogg}')

console.log('-> Converting audio to ogg [better audio performance]')
let oggc = new AudioConvertor('ogg', '-C', '3')
oggc.force = true
let oggs = await dotMap(audio, file => oggc.convert(file))
packer.pack('ogg', oggs)

console.log('-> Writing...')
let out = join(path, 'assets')
await mkdirp(out)
await packer.write(out)
}

function dotMap(array, map) {
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
Loading

0 comments on commit 36a10db

Please sign in to comment.