Chinaunix首页 | 论坛 | 博客
  • 博客访问: 969050
  • 博文数量: 108
  • 博客积分: 3243
  • 博客等级: 中校
  • 技术积分: 964
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-15 22:09
文章分类

全部博文(108)

文章存档

2020年(2)

2019年(1)

2018年(2)

2017年(9)

2016年(20)

2015年(1)

2013年(1)

2012年(12)

2011年(28)

2010年(27)

2009年(4)

2008年(1)

分类: C/C++

2011-12-31 17:59:04

    转载请注明出处

 

    音频波形绘制时,主要就是需要了解绘制原理。总的原则为:把多少个采样点绘制到多宽(像素)的界面上,此时绘制分2种情况:1 需要绘制的采样点数少于或等于显示宽度(像素),此时可以直接绘制;2 需要绘制的采样点数大于显示宽度(像素),此时涉及到抛弃些采样点,但不是简单从多少个采样点中取一个或多个,通过下面的代码,相信你能悟出其中的原理。

 

void CTransWaveShow::paintEvent(QPaintEvent *event)

{

         QPainter painter(this);  

         painter.setRenderHint(QPainter::Antialiasing);

 

         QColor hourColor(127, 0, 127);

        

         int h = height() / 2;

         painter.translate(0, h);

 

         QPen vPen;

         vPen.setColor(Qt::red);

         vPen.setWidth(1);

         painter.setPen(vPen);

         painter.drawLine(0, 0, width(), 0);

 

         if (!(m_AudioList.size() > 0))

         {

                   return;

         }

 

         vPen.setColor(Qt::green);

         painter.setPen(vPen);

         int x1 = 0, y1 = 0, x2 = 0, y2 = 0;

 

         int vPointStep = 1;

         int vDataSetp  = 1;

         if (m_AudioList.size() >= width())

         {

                   vPointStep = 1;

                   vDataSetp  = m_AudioList.size() / width();

         }

         else

         {

                   vPointStep = width() / m_AudioList.size();

                   vDataSetp  = 1;

         }

 

         int vDataIdx = 0;

         for (int i = 0; i < width(); )

         {

                   int vPlusVal = 0;

                   int vNegaVal = 0;

             for(int j = 0; j < vDataSetp; j ++)

                   {

                            int vIdx = vDataIdx * vDataSetp + j;

                            if (!(vIdx < m_AudioList.size()))

                            {

                                     break;

                            }

 

                            int vCurVal = (m_AudioList[vIdx] * h) / (65535 / 2);

                            if (vCurVal > 0)

                            {

                                     if (vCurVal > vPlusVal)

                                     {

                                               vPlusVal = vCurVal;

                                     }

                            }

                            else if (vCurVal < 0)

                            {

                                     if (vCurVal < vNegaVal)

                                     {

                                               vNegaVal = vCurVal;

                                     }

                            }

                   }

 

                   if (vPlusVal > 0)

                   {

                            x2 = i;

                            y2 = - vPlusVal;

                            painter.drawLine(x1, y1, x2, y2);

                            x1 = x2;

                            y1 = y2;

                   }

                   if (vNegaVal < 0)

                   {

                            x2 = i;

                            y2 = - vNegaVal;

                            painter.drawLine(x1, y1, x2, y2);

                            x1 = x2;

                            y1 = y2;

                   }

 

                   i += vPointStep;

                   vDataIdx ++;

         }

}

 

阅读(8293) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~