感谢您阅读本文 好吧,我想做的是获取一个 .wav 文件(只有一段简短的音频)并将其转换为整数,每个整数代表音频的一种音调...... 如果你问我为什么这样做,是因为我正在做一个 arduino 项目,我想让 arduino 播放一首歌,为此我需要一个 int 数组,其中每个 int 都是一个音调。 所以我想,“如果我编写一个小应用程序来将任何 .wav 文件转换为 txt,其中存储代表旋律音符的整数,我只需要将这些值复制到 arduino 项目代码中”; 所以在这一切之后,也许你会问“你的问题是什么?”; 我完成了代码并且正在“工作”,唯一的问题是txt每行中只有“1024”...... 所以很明显我遇到了问题,不是所有的音都是 1024 -_-
package WaveToText;
import java.io.*;
/**
*
* @author Luis Miguel Mejía Suárez
* @project This porject is to convert a wav music files to a int array
* Which is going to be printed in a txt file to be used for an arduino
* @serial 1.0.1 (05/11/201)
*/
public final class Converter
{
/**
*
* @Class Here is where is going to be allowed all the code for the application
*
* @Param Text is an .txt file where is going to be stored the ints
* @Param MyFile is the input of the wav file to be converted
*/
PrintStream Text;
InputStream MyFile;
public Converter () throws FileNotFoundException, IOException
{
MyFile = new FileInputStream("C:\\Users\\luismiguel\\Dropbox\\ESTUDIO\\PROGRAMAS\\JAVA\\WavToText\\src\\WaveToText\\prueba.wav");
Text = new PrintStream(new File("Notes.txt"));
}
public void ConvertToTxt() throws IOException
{
BufferedInputStream in = new BufferedInputStream(MyFile);
int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
Text.println(read);
}
Text.close();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException{
// TODO code application logic here
Converter Exc = new Converter();
Exc.ConvertToTxt();
}
}
最佳答案
等等等等......很多东西都不对劲......
您不能只读取字节并将它们发送到 Arduino,因为正如您所说,Arduino 需要注释编号。 Wav 文件中的数字首先是包含音频信息的“ header ”,然后是代表信号(波形)中离散点的数字。如果您想获得音符,您需要一些用于音高检测或音乐转录的算法。
如果您的音乐是单声道或接近单声道的,则音调检测可能会起作用。对于完整的乐队歌曲来说会很麻烦。
所以...
我猜“Arduino部分”会播放单声道音乐,你需要提取特定时刻信号的基频(这称为音调检测,有不同的方法做到这一点(自相关、amdf、谱分析))。您还必须遵守笔记的时间安排。
当您提取频率时,有一个公式可以将频率转换为表示钢琴上音符编号的整数。 n=12(log2(f/440)) + 49 其中 n 是整数音符编号,f 是音符的基频。在计算之前,您还应该将从音高识别算法获得的频率量化为最接近的频率(谷歌搜索准确的音符频率)。
不过我真的建议做更多研究。如果你演奏的乐器很少,鼓、歌手都在一起,那么很难在音乐中检测到音符......
关于java - 将 wav 文件转换为 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26769345/