Skip to content

Commit

Permalink
opt
Browse files Browse the repository at this point in the history
  • Loading branch information
idranme committed Jul 16, 2024
1 parent 5d1e54c commit 4283fa7
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,33 +411,21 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
} else {
const silk = this.bot.ctx.get('silk')
if (!silk) return this.bot.logger.warn('missing silk service, cannot send non-silk audio')
if (silk.isWav(data)) {
let result: Dict
const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000]
const { fmt } = silk.getWavFileInfo(data)
if (!allowSampleRate.includes(fmt.sampleRate)) {
if (!this.bot.ctx.get('ffmpeg')) return this.bot.logger.warn('missing ffmpeg service, cannot send some audio')
const wavBuf = await this.bot.ctx.get('ffmpeg')
.builder()
.input(Buffer.from(data))
.outputOption('-ar', '24000', '-ac', '1', '-f', 's16le')
.run('buffer')
result = await silk.encode(wavBuf, 24000)
} else {
result = await silk.encode(data, 0)
}
const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000]
if (silk.isWav(data) && allowSampleRate.includes(silk.getWavFileInfo(data).fmt.sampleRate)) {
const result = await silk.encode(data, 0)
const onlineFile = await this.sendFile(type, {
src: `data:audio/amr;base64,` + Buffer.from(result.data).toString('base64'),
})
if (onlineFile) this.attachedFile = onlineFile
} else {
if (!this.bot.ctx.get('ffmpeg')) return this.bot.logger.warn('missing ffmpeg service, cannot send non-silk audio except wav')
const wavBuf = await this.bot.ctx.get('ffmpeg')
if (!this.bot.ctx.get('ffmpeg')) return this.bot.logger.warn('missing ffmpeg service, cannot send non-silk audio except some wav')
const pcmBuf = await this.bot.ctx.get('ffmpeg')
.builder()
.input(Buffer.from(data))
.outputOption('-ar', '24000', '-ac', '1', '-f', 's16le')
.run('buffer')
const result = await silk.encode(wavBuf, 24000)
const result = await silk.encode(pcmBuf, 24000)
const onlineFile = await this.sendFile(type, {
src: `data:audio/amr;base64,` + Buffer.from(result.data).toString('base64'),
})
Expand Down

0 comments on commit 4283fa7

Please sign in to comment.