2009-06-23 99 views
11

我想要看起来像这样的东西。两种不同的颜色不是无用的。如何可视化音频数据?

audacity on mac http://audacity.sourceforge.net/about/images/audacity-macosx.png

我已经在两个int阵列,每一个用于左和右信道从立体声WAV音频数据(一个样品/毫秒)。我已经做了一些尝试,但他们看起来并没有像这样清晰,我的尝试变成了spikey或紧凑的团块。

任何建议吗?我在c#中工作,但psuedocode是可以的。

假设我们有

  • 的函数的DrawLine(颜色,X1,Y1,X2,Y2)与
  • 两个int阵列数据向右[]和左边的[] lenght大号
  • 数据值在32767和-32768之间

如果您做出任何其他假设,只需在答案中写下它们即可。

for(i = 0; i < L - 1; i++) { 
    // What magic goes here? 
} 

这就是我申请the solution Han provided时的结果。 (只有一个频道)
alt text http://www.imagechicken.com/uploads/1245877759099921200.jpg

+0

Audacity是开源的,所以你可以看看代码。我会假设像下面这样... func getHeight(v){return abs(v)* 32767/viewArea.height/2); samplesPerPixelColumn = samples.len/viewArea.width; for i = 1 to viewArea.width {avgV = Avg(samples [i-i + samplesPerPixelColumn]); colHeight = getHeight(avgV);如果avgV> = 0 DrawLine(black,i,viewArea.height/2,i,(viewArea.height/2)+ colHeight)else DrawLine(black,i,viewArea.height/2,i,(viewArea.height/2 ) - colHeight);你可能需要在那里做一些舍入/范围处理,但这应该是主要的。 – steamer25 2009-06-23 23:14:21

+0

你为什么不把它写成答案。 – Nifle 2009-06-24 07:01:11

回答

2

对于每个像素,您可能会有超过1个样本。对于映射到单个像素的每组样本,可以从样本组中的最小值到最大值绘制(垂直)线段。如果你放大每个像素或更少的样本1个样本,这就不再适用了,'nice'解决方案将显示sinc插值。 由于DrawLine无法绘制单个像素,因此当最小值和最大值相同时会出现小问题。在这种情况下,你可以在期望的位置复制单个像素的图像,如在下面的代码:

double samplesPerPixel = (double)L/_width; 
double firstSample = 0; 
int endSample = firstSample + L - 1; 
for (short pixel = 0; pixel < _width; pixel++) 
{ 
    int lastSample = __min(endSample, (int)(firstSample + samplesPerPixel)); 
    double Y = _data[channel][(int)firstSample]; 
    double minY = Y; 
    double maxY = Y; 
    for (int sample = (int)firstSample + 1; sample <= lastSample; sample++) 
    { 
     Y = _data[channel][sample]; 
     minY = __min(Y, minY); 
     maxY = __max(Y, maxY); 
    } 
    x = pixel + _offsetx; 
    y1 = Value2Pixel(minY); 
    y2 = Value2Pixel(maxY); 
    if (y1 == y2) 
    { 
     g->DrawImageUnscaled(bm, x, y1); 
    } 
    else 
    { 
     g->DrawLine(pen, x, y1, x, y2); 
    } 
    firstSample += samplesPerPixel; 
} 

注意Value2Pixel缩放的采样值的像素值(在y方向上)。

0

你可能想看看这个R语言。我没有太多的经验,但主要用于统计分析/可视化场景。如果他们没有一些平滑功能来摆脱你所提到的极端情况,我会感到惊讶。

而且你应该没有麻烦导入你的数据。您不仅可以读取平面文本文件,而且它还可以用C轻松扩展,所以也可能有某种C#接口。