繁体   English   中英

如何在 Flutter/Dart 中从音频文件中获取字节数据(必须是有符号整数/字节),如 AudioInputStream 在 java 中为您提供

[英]How to get byte data from audio file (must be as signed int / bytes) in Flutter/Dart like AudioInputStream gives you in java

再会。 我正在尝试在 Flutter 中创建一个音乐识别应用程序(如 Shazam)(我也是 Flutter 的新手),我希望它可以在移动设备和桌面上运行。

我有一段Java代码,它返回一个包含时域值的字节数组:

File soundFile;
AudioInputStream audioStream;
AudioFormat audioFormat;
SourceDataLine sourceLine;
int check = 0;
byte[] songBytes;
DataLine.Info info;

soundFile = new File("./testWave.wav");
songBytes = new byte[(int) soundFile.length()];
audioStream = AudioSystem.getAudioInputStream(soundFile);
audioFormat = audioStream.getFormat();
info = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
sourceLine.start();
while (check > -1) {
    check = audioStream.read(songBytes, 0, songBytes.length);
}
sourceLine.drain();
sourceLine.close();
for (int i = 0; i < songBytes.length; i++) {
    System.out.println(songBytes[i]);
}

我已经搜索过,但在 Flutter/Dart 中找不到任何方法来做到这一点。 如果可能的话,任何人都可以指导我在 Flutter/Dart 中执行此操作的最佳方法是什么,如果不能,请告诉我执行此操作的最佳方法

假设您的 WAV 标头有 74 个字节长。 (它会根据节的数量而变化,所以你真的需要解析它来确定。但是对于任何一个 WAV 文件源,它通常是相同的数字 - 使用十六进制转储来确定data块的偏移量加 4。)

(通过解析标题,您可以找出其他内容,例如采样率以及它是单声道还是立体声等。)

然后,如果bytesUint8List ,则需要bytes.buffer.asInt16List(74) 这意味着:将支持字节的缓冲区解释为有符号的 short,但从偏移量 74 开始 - 在标头之后。

  var dataOffset = 74; // parse the WAV header or determine from a hex dump
  var bytes = await file.readAsBytes();
  var shorts = bytes.buffer.asInt16List(dataOffset);
  print(shorts[0]); // the first sample of audio
  print(shorts.length); // the number of audio samples

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM