• 首页
  • 文件
  • 下载
  • 状态
  • 常见问题
  • 邮件列表
  • 应用程序
  • 外部链结
  • 版权
  • 志愿者
  • 联络

示范程序:播放


「CIOS音讯核心」当中,不论是语音合成(Text To Speech)、播放音乐或是图形使用者介面的音效,都是使用这个功能。

如果您需要自己处理全部的流程,要使用 「CIOS音讯核心」播放功能,需要处理五个主要的部分:
  1. 导流管的继承
  2. 选取音讯设备
  3. 设定音讯参数
  4. 启动音讯驱动介面
  5. 处理音讯串流程序
如果您不需要自己处理启动音讯设备及串流侦测的问题,那麽只需要简单地使用Core当中的Play函式,并且实作导流管的继 承即可。


virtual bool Play (Conduit      * conduit              ,
 CaDeviceIndex deviceId , int channelCount ,
                   CaSampleFormat sampleFormat         ,
                   int            sampleRate           ,
                   int            frameCount           ,
                   CaStreamFlags  streamFlags   =  0   ,
                   int            sleepInterval = 50 ) ;

参数
说明
conduit
导 流管。
deviceId
音讯设备编号。
channelCount
声道数量。
sampleFormat
取样格式。格式请参考《CaSampleFormat》。
sampleRate
取样频率。「CIOS音讯核心」支持的范围为8000-192000,某些 音讯驱动介面可能不支持特定取样频率。
frameCount
每次取样的数量,一般建议为0.1秒或0.2秒。
如果您的取样频率是44100或48000,那麽这个frameCount值一般建议为4410、8820 或是4800、9600。
streamFlags
串流状态,一般为0,一般而言你不需要设定这个值,这是为某些特定音讯驱动 介面功能而给定的参数。
sleepInterval
侦测播放状态的时间间隔,一般建议值为50~250微秒,尽可能不要太长。

使用示范

Core core ;
MyOwnConduit * conduit = new MyOwnConduit () ;
...
core . Play ( conduit , 1 , 2 , cafInt16 , 44100 , 4410 , 0 , 100 ) ;
...

如果您需要无阻塞的播放方式,请参考《播放音讯档》当中的线绪实作方法。

导流管的继承

导流管是「CIOS音讯核心」当中最重要的观念之一,这个实作方式被集智作业系统当中其他系统广泛地应用。

使用者如果需要自行供应音讯串流给音讯设备,那麽您必须自行继承Conduit类别,实作一个自有的导流管。完整的继承处理 方式,请参考《导流管的继承处理》、《LinearConduit》或是《BridgeConduit》。

如果您只需要自行供应音讯数据来源,那麽导流管当中仅需处理两个函式,put函式只要留空白或是直接返回Complete即可:

virtual int  obtain (void) = 0 ;
virtual void finish (ConduitDirection direction = NoDirection ,FinishCondition  condition = Correct ) = 0 ;

obtain函式处理StreamIO Output,处理的方式大体如下:

int MyOwnConduit::obtain (void)
{
  if ( ... condition is ready for audio feeding ... ) {
     ... copy your audio feeding into Output.Buffer ...
     // example code in BridgeConduit.cpp : 
Buffer . get ( Output . Buffer , Output . Total ( ) ) ;
     return Continue ;
  }
  if ( ... condition is audio feeding is completely empty ... ) {
    return Complete ;
  }
  if ( ... condition has something unexpected ... ) {
    return Abort ;
  }
  return Postpone ;
}

整个串流被结束时,Stream会主动呼叫finish。

void MyOwnConduit::finish (ConduitDirection direction,FinishCondition condition = Correct)
{
  if ( OutputDirection != direction ) return ;
  ... handle your own finishing conditions here ...
}

选取音讯设备

您需要透过取得DeviceInfo来得知音讯设备的细节,详 细的设定请参考《DeviceInfo》。

如果您需要知道音讯设备的名称:

GetDeviceInfo((CaDeviceIndex)deviceId)->name

如果您想知道这个设备的名称编码:

HostApi * hostApi = NULL ;
DeviceInfo * deviceInfo = NULL ;

deviceInfo = core . GetDeviceInfo (deviceId) ;
if ( NULL != deviceInfo ) {
  core . GetHostApi ( &hostAPi , deviceInfo -> hostType ) ;
  if ( NULL != hostApi ) {
    if ( HostApi::NATIVE == hostApi -> encoding() ) {
      // deviceInfo->name is native OS encoding
    } else
    if ( HostApi::UTF8 == hostApi -> encoding() ) {
      // deviceInfo->name is UTF8 encoding
    } else
    if ( HostApi::UTF16 == hostApi -> encoding() ) {
      // deviceInfo->name is UTF16 encoding
    }
  }
} ;

CaPlay当中的示范程序

void ListOutputDevices(void)
{
  Core core                                       ;
  core . Initialize ( )                           ;
  for (int i=0;i<core.DeviceCount();i++)          {
    DeviceInfo * dev                              ;
    dev = core.GetDeviceInfo((CaDeviceIndex)i)    ;
    if ( NULL != dev && dev->maxOutputChannels>0) {
      printf("%4d : %s\n",i,dev->name)            ;
    }                                             ;
  }                                               ;
  core . Terminate  ( )                           ;
}

一般而言,音讯设备必须事先得知并取得编号,如果您没有设定音讯设备的话,系统会自动选用:

virtual CaDeviceIndex DefaultOutputDevice (void) ;

这个函式所设定的输出设备。

由于某些作业系统底层音讯驱动程序实作的问题,这个输出设备不见得可以正常运作。由于「CIOS音讯核心」使用这些底层的音讯驱动程序,如果输出设备不能正常运作,在
「CIOS音讯核心」当中寻找解决途径一般是徒劳无功的。

设定音讯参数

设定音讯参数需要设定StreamParameters, 详细使用方法请见《StreamParameters》。

一般而言,仅需要在宣告的时候,使用以下宣告即可设定音讯参数:

StreamParameters outputParameters ( deviceId , channelCount , sampleFormat ) ;

取样频率的设定,必须在设备打开的时候设定,请见《启动音讯驱动介面》这一部分。

启动音讯驱动介面


启动及停止音讯驱动介面,需要以下步骤:

Core core                                                                  ;
...
core . Initialize
( ) ;
//////////////////////////////////////////////////////////////////////////// ... configure output paraments here ... //////////////////////////////////////////////////////////////////////////// rt = core . Open ( &stream , NULL , &outputParameters , sampleRate , frameCount , streamFlags , conduit ) ; if ( ( NoError != rt ) || ( NULL == stream ) ) correct = false ; //////////////////////////////////////////////////////////////////////////// if ( correct ) { rt = core . Start ( stream ) ; if ( NoError != rt ) correct = false ;
////////////////////////////////////////////////////////////////////////// ... handle your stream process here ... ////////////////////////////////////////////////////////////////////////// if ( correct && ( 0 != core.IsStopped ( stream ) ) ) core.Stop ( stream ) ; } core . Close ( stream ) ; core . Terminate ( ) ;
...


处理音讯串流程序


音讯串流启动以后,您需要侦测它是否被停止或是被启动,下面两个Core函式是您所需要的:

virtual CaError IsStopped (Stream * stream) ;
virtual CaError IsActive (Stream * stream) ;

示范:

while ( 1 == core . IsActive  ( stream ) ) {
  Timer :: Sleep ( sleepInterval ) ;
}

示范程序


/* Play conduit into specific device */
bool Core::Play ( Conduit      * conduit       ,
                  CaDeviceIndex  deviceId      ,
                  int            channelCount  ,
                  CaSampleFormat sampleFormat  ,
                  int            sampleRate    ,
                  int            frameCount    ,
                  CaStreamFlags  streamFlags   ,
                  int            sleepInterval )
{
  if ( NULL == conduit ) return false                                        ;
  ////////////////////////////////////////////////////////////////////////////
  StreamParameters INSP ( deviceId , channelCount , sampleFormat )           ;
  Stream         * stream  = NULL                                            ;
  CaError          rt                                                        ;
  bool             correct = true                                            ;
  ////////////////////////////////////////////////////////////////////////////
  Initialize ( )                                                             ;
  ////////////////////////////////////////////////////////////////////////////
  INSP . suggestedLatency = GetDeviceInfo(deviceId)->defaultLowOutputLatency ;
  ////////////////////////////////////////////////////////////////////////////
  rt = Open ( &stream                                                        ,
              NULL                                                           ,
              &INSP                                                          ,
              sampleRate                                                     ,
              frameCount                                                     ,
              streamFlags                                                    ,
              conduit                                                      ) ;
  if ( ( NoError != rt ) || ( NULL == stream ) ) correct = false             ;
  ////////////////////////////////////////////////////////////////////////////
  if ( correct )                                                             {
    rt = Start ( stream )                                                    ;
    if ( NoError != rt ) correct = false                                     ;
    while ( correct && ( 1 == IsActive  ( stream ) ) )                       {
      Timer :: Sleep ( sleepInterval )                                       ;
    }                                                                        ;
    //////////////////////////////////////////////////////////////////////////
    if ( correct && ( 0 != IsStopped ( stream ) ) ) Stop ( stream )          ;
  }                                                                          ;
  Close     ( stream )                                                       ;
  Terminate (        )                                                       ;
  return correct                                                             ;
}