Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(qq): fix audio encoding #289

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
} else if (type === 'audio' && (attrs.src || attrs.url)) {
await this.flush()
const { data } = await this.bot.ctx.http.file(attrs.src || attrs.url, attrs)
if (data.slice(0, 7).toString().includes('#!SILK')) {
if (new TextDecoder().decode(data.slice(0, 7)).includes('#!SILK')) {
const onlineFile = await this.sendFile(type, {
src: `data:audio/amr;base64,` + Buffer.from(data).toString('base64'),
})
Expand All @@ -412,7 +412,20 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
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)) {
const result = await silk.encode(data, 0)
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 onlineFile = await this.sendFile(type, {
src: `data:audio/amr;base64,` + Buffer.from(result.data).toString('base64'),
})
Expand Down
Loading