感谢您阅读此内容 那么我想要做的就是获取一个.wav文件(只有一个短音频)并将其转换为整数,每一个代表音频的音调...... 如果您问我为什么这样做,是因为我正在做一个arduino项目,我想让arduino播放一首歌,为此我需要一个int数组,其中每个int都是一个基调。 所以我想,"好吧,如果我编写一个小应用程序来将任何.wav文件转换为存储表示旋律音符的整数的txt,我只需要将这些值复制到arduino项目代码" ; 所以在这之后,也许你会问"你的问题是什么?&#34 ;; 我完成了代码并且工作"唯一的问题是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();
}
}
答案 0 :(得分:1)
等待等待.....很多事情都不在这里......
您不能只读取字节并将它们发送到Arduino,因为正如您所说,Arduino需要注释数字。 Wav文件中的数字首先是带有音频信息的“标题”,然后是表示信号中离散点(Waveform)的数字。如果你想获得音符,你需要一些音调检测或音乐转录算法
如果您的音乐是单音或接近单声道,则音高检测可能有效。对于全乐队的歌曲,这将是麻烦的。
所以......我想“Arduino部分”会播放单声道音乐,你需要在特定的时刻提取信号的基频(这就是所谓的音调检测,有不同的方法)这样做(自相关,amdf,光谱分析))。您还必须保留笔记的时间。
提取频率时,有一个公式可将频率转换为整数,表示钢琴上的音符编号。 n = 12(log2(f / 440))+ 49其中n是整数音符编号,f是音符的基频。在计算之前,您还应该将从音高识别算法获得的频率量化为最接近的(谷歌的精确音符频率)。
不过我真的建议做更多的研究。在音乐中检测音符真的很难,你的乐器演奏很少,鼓,歌手都在一起....
答案 1 :(得分:0)
while ((read = in.read(buff)) > 0)
{
Text.println(read);
}
这段代码从in
读取1024字节的数据,然后将读取的字节数分配给read
,即1024,直到文件结束。然后,将read
打印到文本文件中。
您可能希望将buff
打印到文本文件中,但这将写入1024个字节,而不是您想要的1024个字节。
您需要创建一个for循环以将单个字节打印为整数。
while ((read = in.read(buff)) > 0)
{
for (int i = 0; i < buff.length; i++)
Text.print((int)buff[i]);
}