技术文章 > 实时显示波形,支持双声道(BCB)

实时显示波形,支持双声道(BCB)

2018-06-18 19:11

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:
这个程序也是用了低级音频函数,但是它用了多线程,比我那个厉害多了。该程序用BCB4编写,我用BCB6试了一下,没问题。在此下载完整代码。
关于音频采集我就不写在这儿了,有兴趣去看《实时录音并显示波形》这篇文章。下面分析一下他的多线程。
//---------------------------------------------------------------------------
//
// Copyright (C) 2002 Mika S鰀erholm.
// All Rights Reserved.
//
//---------------------------------------------------------------------------
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Mika S鰀erholm.
4. Any commercial use is prohibited.
THIS SOFTWARE IS PROVIDED ``AS IS““ AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL MIKA S諨ERHOLM BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//---------------------------------------------------------------------------
// For more information contact email: mika.soderholm@luukku.com
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "CorrelationThread.h"
#include
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall CorrelationThread::CorrelationThread( bool CreateSuspended, int ic )
:TThread(CreateSuspended)
{
count = ic;//count是采样的点数
}
__fastcall CorrelationThread::~CorrelationThread()
{
}
//---------------------------------------------------------------------------
void __fastcall CorrelationThread::Execute()
{
while ( !Terminated )//只要线程不中止就一直执行FillBuffer
{
FillBuffer();
}
}
void __fastcall CorrelationThread::FillBuffer()
{
double ksum = 0.0;
double rqsum = 0.0;
double lqsum = 0.0;
short* pdatabuff = databuffer;
for(int i=0; i{
double r = static_cast(*pdatabuff++);
double l = static_cast(*pdatabuff++);
ksum += r * l;
rqsum += r * r;
lqsum += l * l;
}
double correlation = ksum / sqrt(lqsum * rqsum);
//
// Oscilloscope
//
view->Canvas->Lock();
view->Canvas->Brush->Color = clBlack;
view->Canvas->FillRect( valuerect );
int half = view->ClientWidth/2;
int i = half+correlation*half;
if( i+8 >= view->ClientWidth )
{
i = view->ClientWidth-5;
}
valuerect.Left=i-4;
valuerect.Right=valuerect.Left+8;
valuerect.Top=5;
valuerect.Bottom=view->Height-5;
view->Canvas->Brush->Color=clAqua;
view->Canvas->FillRect( valuerect );
view->Canvas->Unlock();
Suspend();//处理完数据后挂起线程,避免重复处理。
}
//---------------------------------------------------------------------------
void __fastcall CorrelationThread::Process( TPaintBox* in, short* idata )
{
databuffer=idata;//这个函数是由主程序调用的,一旦主程序有数据要处理就会调用这个函数
view = in;
Resume();//让挂起的线程重新启动,一旦启动线程就会立即调用FillBuffer
}
这个线程写的不错,但是对于处理速度比较慢的机器就会出现一些问题。当第一批数据到达的时候FillBuffer函数立刻开始运行,但是这个函数还没有完成的时候,第二批数据又来了,这时数据就会被改变,因此会出现混乱。解决方法也很简单,设置一个变量来指示数据处理是否完成就可以了。然后在Process函数里做必要的判断就可以了。