我有一个文件,其中包含大约四万个以空格分隔的整数列表,每个整数的值介于0到255之间。这是这个文件:
https://github.com/johnlai2004/sound-project/blob/master/integers.txt
如果您将扬声器连接到ESP32分线板,然后通过数字到模拟转换器以24kHz的频率运行此整数列表,您将听到句子“那不是您错过的帖子”。
我想知道的是你如何使用FFMPEG将这个整数列表转换成一个声音文件,其他计算机可以播放以听到相同的短语?我试过这个命令:
ffmpeg -f u8 -ac 1 -ar 24000 -i integers.txt -y audio.wav
但我的audio.wav
听起来像白噪声。我为-f
和-ar
尝试了一些其他的值,但我听到的是不同频率的白噪声,也许是一些额外的嗡嗡声。
是否可以使用ffmpeg将我的整数列表转换为音频文件以供其他计算机播放?如果是这样,那么执行此操作的ffmpeg命令是什么?
其他说明
如果它有帮助,这是我上传到ESP32的草图文件,如果我想听到音频:
https://github.com/johnlai2004/sound-project/blob/master/play-audio.ino
简而言之,该文件如下所示:
#define speakerPin 25 //The pins to output audio on. (9,10 on UNO,Nano)
#define bufferTotal 1347
#define buffSize 32
byte buffer[bufferTotal][buffSize];
int buffItemN = 0;
int bufferN = 0;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
byte v = buffer[bufferN][buffItemN];
dacWrite(speakerPin,v);
buffItemN++;
if(buffItemN >= buffSize){ //If the buffer is empty, do the following
buffItemN = 0; //Reset the sample count
bufferN++;
if(bufferN >= bufferTotal)
bufferN = 0;
}
portEXIT_CRITICAL_ISR(&timerMux);
}
void setup() {
/* buffer records */
buffer[0][0]=88; // I split the long list of integers and load it into a 2D array
buffer[0][1]=88;
buffer[0][2]=86;
buffer[0][3]=85;
//etc....
buffer[1346][28]=94;
buffer[1346][29]=92;
buffer[1346][30]=92;
buffer[1346][31]=95;
/* end buffer records */
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 41, true);
timerAlarmEnable(timer);
}
void loop() {
}
buffer...
是integers.txt
文件中的整数列表。
正如@Gyan在评论中建议的那样,在运行ffmpeg命令之前,我必须先将我的整数列表转换为二进制文件。所以我创建了一个名为main.go
的golang脚本:
package main
import (
"io/ioutil"
"strings"
"strconv"
"os"
)
func main() {
input:="./integers.txt"
output:="./binary.raw"
// Load the list of integers into memory
contentbyte, _ := ioutil.ReadFile(input)
content := strings.Split(string(contentbyte)," ");
// Prepare to output a new binary file
f, err := os.OpenFile(output, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
for _,val := range content {
// Convert each integer to a binary value and write to output file
i,_ := strconv.Atoi(val)
if _, err = f.Write([]byte{byte(i)}); err != nil {
panic(err)
}
}
}
我运行go run main.go
给我binary.raw
文件。然后我按照ffmpeg -f u8 -ar 24000 -ac 1 -i binary.raw -y audio.wav
的问题在我的问题中发布了ffmpeg命令。
audio.wav
文件听起来就像我的ESP32 +扬声器的输出,这是我想要的。