TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kaudioplaystream.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* This file is part of the KDE project
  2. Copyright (C) 2003 Arnold Krille <arnold@arnoldarts.de>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License version 2 as published by the Free Software Foundation.
  6. This library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Library General Public License for more details.
  10. You should have received a copy of the GNU Library General Public License
  11. along with this library; see the file COPYING.LIB. If not, write to
  12. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  13. Boston, MA 02110-1301, USA.
  14. */
  15. #include "kaudioplaystream.h"
  16. #include "kaudioplaystream_p.h"
  17. #include <kartsserver.h>
  18. #include <kaudiomanagerplay.h>
  19. #include <artsflow.h>
  20. #include <soundserver.h>
  21. #include <tdeglobal.h>
  22. #include <kdebug.h>
  23. #include <tqstring.h>
  24. //#include <tqptrqueue.h>
  25. //#include <tqcstring.h> //QByteArray
  26. #include <string.h> // for strncpy
  27. //#include <assert.h>
  28. KAudioPlayStreamPrivate::KAudioPlayStreamPrivate( KArtsServer* server, const TQString title, TQObject* p, const char* n )
  29. : TQObject( p,n )
  30. , _server( server )
  31. , _play( new KAudioManagerPlay( _server, title ) )
  32. , _effectrack( Arts::StereoEffectStack::null() )
  33. , _polling( true ), _attached( false ), _effects( true )
  34. {
  35. kdDebug( 400 ) << k_funcinfo << endl;
  36. initaRts();
  37. }
  38. KAudioPlayStreamPrivate::~KAudioPlayStreamPrivate()
  39. {
  40. kdDebug( 400 ) << k_funcinfo << endl;
  41. _play->stop();
  42. if ( _effects ) _effectrack.stop();
  43. _bs2a.stop();
  44. }
  45. void KAudioPlayStreamPrivate::initaRts() {
  46. kdDebug( 400 ) << k_funcinfo << endl;
  47. _effectrack = Arts::DynamicCast( _server->server().createObject( "Arts::StereoEffectStack" ) );
  48. if ( _effectrack.isNull() )
  49. {
  50. kdWarning( 400 ) << "Couldn't create EffectStack!" << endl;
  51. _effects = false;
  52. }
  53. _bs2a = Arts::DynamicCast( _server->server().createObject( "Arts::ByteStreamToAudio" ) );
  54. if ( _bs2a.isNull() )
  55. kdFatal( 400 ) << "Couldn't create ByteStreamToAudio" << endl;
  56. if ( _effects )
  57. {
  58. Arts::connect( _effectrack, _play->amanPlay() );
  59. Arts::connect( _bs2a, _effectrack );
  60. } else {
  61. Arts::connect( _bs2a, _play->amanPlay() );
  62. }
  63. _play->start();
  64. if ( _effects ) _effectrack.start();
  65. }
  66. KAudioPlayStream::KAudioPlayStream( KArtsServer* server, const TQString title, TQObject* p, const char* n )
  67. : TQObject( p,n )
  68. , d( new KAudioPlayStreamPrivate( server, title, this ) )
  69. {
  70. kdDebug( 400 ) << k_funcinfo << endl;
  71. }
  72. KAudioPlayStream::~KAudioPlayStream()
  73. {
  74. kdDebug( 400 ) << k_funcinfo << endl;
  75. }
  76. void KAudioPlayStream::setPolling( bool n ) { d->_polling = n; }
  77. bool KAudioPlayStream::polling() const { return d->_polling; }
  78. bool KAudioPlayStream::running() const { return d->_attached; }
  79. Arts::StereoEffectStack KAudioPlayStream::effectStack() const {
  80. return d->_effectrack;
  81. }
  82. void KAudioPlayStream::start( int samplingRate, int bits, int channels )
  83. {
  84. kdDebug( 400 ) << k_funcinfo << "samplingRate: " << samplingRate << " bits: " << bits << " channels: " << channels << endl;
  85. if ( !d->_attached )
  86. {
  87. d->_bs2a.samplingRate( samplingRate );
  88. d->_bs2a.channels( channels );
  89. d->_bs2a.bits( bits );
  90. d->_sender = new KByteSoundProducer( this, d->_server->server().minStreamBufferTime(), samplingRate, bits, channels, "PS" );
  91. d->_artssender = Arts::ByteSoundProducerV2::_from_base( d->_sender );
  92. Arts::connect( d->_artssender, "outdata", d->_bs2a, "indata" );
  93. d->_bs2a.start();
  94. d->_artssender.start();
  95. // // Needed?
  96. Arts::Dispatcher::the()->ioManager()->processOneEvent( false );
  97. d->_attached = true;
  98. emit running( d->_attached );
  99. }
  100. }
  101. void KAudioPlayStream::stop()
  102. {
  103. kdDebug( 400 ) << k_funcinfo << endl;
  104. if ( d->_attached )
  105. {
  106. d->_attached = false;
  107. d->_bs2a.stop();
  108. d->_artssender.stop();
  109. // Shortly stop the play so we dont get clicks and artefacts
  110. d->_play->stop();
  111. d->_play->start();
  112. Arts::disconnect( d->_artssender, d->_bs2a );
  113. d->_artssender = Arts::ByteSoundProducerV2::null();
  114. d->_sender = 0;
  115. emit running( d->_attached );
  116. }
  117. }
  118. void KAudioPlayStream::write( TQByteArray& )
  119. {
  120. }
  121. void KAudioPlayStream::fillData( Arts::DataPacket<Arts::mcopbyte> *packet )
  122. {
  123. //kdDebug( 400 ) << k_funcinfo << "packet->size=" << packet->size << endl;
  124. if ( d->_polling )
  125. {
  126. TQByteArray bytearray( packet->size );
  127. bytearray.setRawData( ( char* )packet->contents, packet->size );
  128. bytearray.fill( 0 );
  129. emit requestData( bytearray );
  130. bytearray.resetRawData( ( char* )packet->contents, packet->size );
  131. //for ( int i=0; i<10; i++ )
  132. // kdDebug() << packet->contents[ i ] << " : " << bytearray.data()[ i ] << endl;
  133. } else {
  134. /// TODO: Implement a queue and fetching from it...
  135. }
  136. }
  137. // * * * KByteSoundProducer * * *
  138. KByteSoundProducer::KByteSoundProducer( KAudioPlayStream* impl, float minBufferTime, int rate, int bits, int channels, const char * title )
  139. : _samplingRate( rate )
  140. , _channels( channels )
  141. , _bits( bits )
  142. , _packets( 7 )
  143. , _title( title )
  144. , _impl( impl )
  145. {
  146. // Calculate packet count (packetsize is fixed to packetCapacity = 4096
  147. float streamBufferTime;
  148. do {
  149. _packets++;
  150. streamBufferTime = ( float )( _packets * packetCapacity * 1000 )
  151. / ( float )( _samplingRate * _channels * 2 );
  152. } while ( streamBufferTime < minBufferTime );
  153. //kdDebug( 400 ) << k_funcinfo << "_packets:" << _packets << " packetCapacity:" << packetCapacity << endl;
  154. }
  155. KByteSoundProducer::~KByteSoundProducer()
  156. {
  157. }
  158. void KByteSoundProducer::streamStart() { outdata.setPull( _packets, packetCapacity ); }
  159. void KByteSoundProducer::streamEnd() { outdata.endPull(); }
  160. void KByteSoundProducer::request_outdata( Arts::DataPacket<Arts::mcopbyte> *packet )
  161. {
  162. if ( _impl->running() ) {
  163. _impl->fillData( packet );
  164. packet->send();
  165. }
  166. }
  167. // vim: sw=4 ts=4 tw=80
  168. #include "kaudioplaystream.moc"
  169. #include "kaudioplaystream_p.moc"