flutter声音流如何表示pcm数据?

发布于 2025-01-14 00:04:12 字数 931 浏览 3 评论 0原文

我需要制作一个通过绘制图形来可视化音频数据的应用程序,并且我尝试过 flutter sound声音流来获取原始音频数据。然而,这两个库都将声音捕获为 16 位 pcm,但返回 Uint8list 流。所以我不明白他们如何用 8 位整数表示 16 位 pcm。

我试图按原样绘制出数字,但似乎不正确。以下是我用来绘制 30hz 正弦波的代码,以及 sound_stream 提供的数据。

final Uint8List data; 
  @override
  void paint(Canvas canvas, Size size) { 
    double dx = size.width / data.length;
    for(int i = 1; i < data.length; i+=1){
    canvas.drawLine(Offset((i - 1) * dx, (data[i-1].toDouble()/256) * size.height), Offset((i) * dx, (data[i].toDouble()/256) * size.height),  Paint()..color = Colors.red..strokeWidth=1);
    }
  }

30hz

I need to make a app that visualizes audio data by graphing it, and I've tried fluter sound and sound stream to get the raw audio data. However both of these libraries captures the sound as 16 bit pcm, but returns a stream of Uint8list. So I don't understand how they are representing the 16 bit pcm with 8 bit integers.

I've tried to just graph out the numbers as is, but it doesnt appear to be right. The following is the code I used to graph out 30hz sine wave, with the data sound_stream provides.

final Uint8List data; 
  @override
  void paint(Canvas canvas, Size size) { 
    double dx = size.width / data.length;
    for(int i = 1; i < data.length; i+=1){
    canvas.drawLine(Offset((i - 1) * dx, (data[i-1].toDouble()/256) * size.height), Offset((i) * dx, (data[i].toDouble()/256) * size.height),  Paint()..color = Colors.red..strokeWidth=1);
    }
  }

30hz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2025-01-21 00:04:12

它是一个 Uint8List,因为它是一个字节数组,并且是跨原生 Dart 边界移动的内容。因此,您需要将该字节数组视为 16 位整数的列表。使用 ByteBuffer 来执行此操作。

final Uint8List data;
final pcm16 = data.buffer.asInt16List();
// pcm16 is an Int16List and will be half the length of data
// expect values in pcm16 to be between -32768 and +32767 so
// normalize by dividing by 32768.0 to get -1.0 to +1.0

您可能会发现获得的字节数是预期的两倍。如果您以 16kHz 采样,预计每秒 32k 字节,但当以 16 位整数查看时,您每秒将获得 16000 个样本。

It's a Uint8List because that's a byte array and is what gets moved across the native-Dart boundary. So you need to view that byte array as a list of 16 bit integers. Use ByteBuffer to do this.

final Uint8List data;
final pcm16 = data.buffer.asInt16List();
// pcm16 is an Int16List and will be half the length of data
// expect values in pcm16 to be between -32768 and +32767 so
// normalize by dividing by 32768.0 to get -1.0 to +1.0

You will probably find that you are getting twice as many bytes as you expect. If you are sampling at 16kHz, expect 32k bytes a second, but you'll get 16000 samples a second when viewed as 16 bit ints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文