【发布时间】:2014-11-05 23:49:07
【问题描述】:
感谢您阅读本文 那么我要做的是获取一个.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();
}
}
【问题讨论】:
-
您的一般方法一开始并不完全正确。 WAV 文件存储时域数据;也就是说,信号与时间的关系(例如,440 Hz 的音调可能是,例如,WAV 文件中 440 Hz 正弦波的样本值)。您对“音调”的概念是频域的;也就是说,信号强度与频率。您需要执行某种类型的频率分析(查找离散 FFT)以从 WAV 文件中的原始波形中提取“旋律”信息,或者您需要修改硬件端并让 Arduino 播放原始数字音频参数化频率。
-
顺便说一下,对于文件格式本身,it is well-documented,还有一个很好的读取数据的例子here。但是,您最好使用已经为您完成工作的
AudioInputStream(并且支持除 WAV 之外的格式)。另请参阅 sweb.cityu.edu.hk/sm1204/2012A/page20/index.html,它描述了一些与 Java Sound API 集成的 FFT 库。 -
无论该方法是否正确,我已在答案中提供了您的代码的问题。希望对您有所帮助。
-
嗯,我已经尝试过你(CharlieS)所说的“类似”的东西,我说类似是因为输出文件完全相同,而且这有很多不同的数字,但不是真的我所期待的;我认为是因为 Alexander 说的,所以我会尝试这种方式......我也尝试了 AudioInputStream,但我得到了“UnsupportedAudioFileException”无论如何感谢很多人的帮助