Skip to content

Silk generated does not have the expected pitch. #1

@aldy97

Description

@aldy97

My code could generate silk based on pcm file, but it has very weird pitch when being played on QQ APP.

async function convertPCMToSilk(pcmPath, sampleRate = 32000) {
	const silkPath = pcmPath.replace(".pcm", ".silk");
    try {
		const pcmBuffer = fs.readFileSync(pcmPath);
		const silkBuffer = pcm2slk(pcmBuffer, {
			sampleRateIn:  sampleRate,
			sampleRateOut: sampleRate,
			frameSizeMs:   20,        // 20 ms QQ packets
			mode:          "wb",      // wideband
			channels:      1,
			bitDepth:      16,
			// turn off any PLC or auto-gain:
			packetLossPercentage: 0,
			// if your version supports an explicit bandwidth flag:
			bandwidth:     "full"
		  });
		console.log("Converted PCM to SILK successfully: ", silkBuffer);
		fs.writeFileSync(silkPath, silkBuffer);
        return silkPath;
    } catch (error) {
        console.error("Conversion failed:", error);
        throw error;
    }
}

This is the alternative method using Python script which generates the expected voice but we couldn't solve the dependency on the cloud:

// 2. Convert PCM to SILK
async function convertPCMToSilk(pcmPath, sampleRate = 32000) {
	const silkPath = pcmPath.replace(".pcm", ".silk");
	const { exec } = require("child_process");
	const util = require("util");
	const execAsync = util.promisify(exec);

	try {
		// Using pysilk via Python command
		const pythonScript = `
import pysilk
with open("${pcmPath}", "rb") as pcm_in, open("${silkPath}", "wb") as silk_out:
    pysilk.encode(pcm_in, silk_out, ${sampleRate}, ${sampleRate})
`;

		await execAsync(`python3 -c '${pythonScript}'`);
		return silkPath;
	} catch (error) {
		console.error("PCM to SILK conversion error:", error);
		throw error;
	}
}

Can anyone help me with that? What's the exactly equivalent node-silk code for the alterntaive? Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions