25 #include "kaudiorecordstream.h"
26 #include "kaudiorecordstream_p.h"
27 #include "kartsserver.h"
30 #include <soundserver.h>
32 #include <tdeglobal.h>
36 #include <tqptrqueue.h>
37 #include <tqcstring.h>
41 struct KAudioRecordStream::Data
43 Arts::Synth_AMAN_RECORD in;
44 Arts::AudioToByteStream convert;
46 Arts::ByteSoundReceiver receiver;
47 KByteSoundReceiver * receiver_base;
53 TQPtrQueue<TQByteArray> inqueue;
58 : TQObject( parent, name )
66 d->inqueue.setAutoDelete(
true );
69 connect( d->kserver, TQ_SIGNAL( restartedServer() ), TQ_SLOT( slotRestartedServer() ) );
71 d->in = Arts::DynamicCast( d->kserver->server().createObject(
"Arts::Synth_AMAN_RECORD" ) );
72 d->effectStack = Arts::DynamicCast( d->kserver->server().createObject(
"Arts::StereoEffectStack" ) );
73 d->convert = Arts::DynamicCast( d->kserver->server().createObject(
"Arts::AudioToByteStream" ) );
75 kdFatal( 400 ) <<
"couldn't create a Synth_AMAN_RECORD on the aRts server\n";
76 if( d->effectStack.isNull() )
77 kdFatal( 400 ) <<
"couldn't create a StereoEffectStack on the aRts server\n";
78 if( d->convert.isNull() )
79 kdFatal( 400 ) <<
"couldn't create a AudioToByteStream on the aRts server\n";
81 d->in.title( (
const char * ) d->title.local8Bit() );
82 Arts::connect( d->in, d->effectStack );
84 d->effectStack.start();
87 KAudioRecordStream::~KAudioRecordStream()
89 d->receiver = Arts::ByteSoundReceiver::null();
99 unsigned int remaining = size;
103 while( d->inqueue.isEmpty() )
104 Arts::Dispatcher::the()->ioManager()->processOneEvent(
true );
107 if( d->inqueue.isEmpty() )
108 Arts::Dispatcher::the()->ioManager()->processOneEvent(
false );
109 if( d->inqueue.isEmpty() )
110 return size - remaining;
112 TQByteArray *
data = d->inqueue.head();
113 unsigned int tocopy = kMin( remaining,
data->size() - d->pos );
114 memcpy( buffer,
data->data() + d->pos, tocopy );
118 if( d->pos ==
data->size() )
129 d->blocking = blocking;
151 return d->effectStack;
167 Arts::disconnect( d->convert, d->receiver );
168 d->receiver = Arts::ByteSoundReceiver::null();
169 d->receiver_base = 0;
171 Arts::disconnect( d->effectStack, d->convert );
180 kdDebug( 400 ) << k_funcinfo <<
"samplingRate: " << samplingRate <<
" bits: " << bits <<
" channels: " << channels <<
endl;
183 assert( d->kserver );
185 if( ( samplingRate < 500 || samplingRate > 2000000 )
186 || ( channels != 1 && channels != 2 ) || ( bits != 8 && bits != 16 ) )
188 kdWarning( 400 ) <<
"invalid stream parameters: rate=" << samplingRate <<
", " << bits <<
" bit, " << channels <<
" channels\n";
192 d->convert.samplingRate( samplingRate );
193 d->convert.channels( channels );
194 d->convert.bits( bits );
195 Arts::connect( d->effectStack, d->convert );
197 d->receiver_base =
new KByteSoundReceiver( samplingRate, bits, channels, d->title.local8Bit() );
198 d->receiver = Arts::ByteSoundReceiver::_from_base( d->receiver_base );
199 connect( d->receiver_base, TQ_SIGNAL(
data(
const char *,
unsigned int ) ),
200 TQ_SLOT( slotData(
const char *,
unsigned int ) ) );
201 Arts::connect( d->convert,
"outdata", d->receiver,
"indata" );
207 Arts::Dispatcher::the()->ioManager()->processOneEvent(
false );
220 void KAudioRecordStream::slotRestartedServer() { }
222 void KAudioRecordStream::slotData(
const char * contents,
unsigned int size )
225 TQByteArray * bytearray =
new TQByteArray( size );
228 bytearray->duplicate( contents, size );
231 kdDebug( 400 ) <<
"enqueue the data\n";
232 d->inqueue.enqueue( bytearray );
237 emit
data( *bytearray );
247 KByteSoundReceiver::KByteSoundReceiver(
int rate,
int bits,
int channels,
const char * title )
248 : _samplingRate( rate )
250 , _channels( channels )
255 KByteSoundReceiver::~KByteSoundReceiver()
259 void KByteSoundReceiver::process_indata( Arts::DataPacket<Arts::mcopbyte> * inpacket )
262 emit data( (
char *)inpacket->contents, inpacket->size );
263 inpacket->processed();
265 #include "kaudiorecordstream.moc"
266 #include "kaudiorecordstream_p.moc"
KArtsServer is a wrapper to conveniently get a reference to a SoundServer, and restart artsd when nec...
bool polling() const
read whether polling is enabled
void flush()
flush input buffer
void setBlockingIO(bool)
If you set blockingIO to true the read method will wait until it has enough data to return exactly wh...
void stop()
Detaches the stream from the soundserver but doesn't remove the Synth_AMAN_RECORD so that the entry i...
void data(TQByteArray &data)
Data from the aRts server has arrived.
bool blockingIO() const
read blocking I/O setting
void usePolling(bool)
If you want to poll for data using read() set this to true.
Arts::StereoEffectStack effectStack() const
KAudioRecordStream(KArtsServer *server, const TQString &title, TQObject *parent=0, const char *name=0)
Construct a KAudioRecordStream.
int read(char *, int size)
You can read size number of bytes.
void start(int samplingRate, int bits, int channels)
Attach to the soundserver and start getting data to read.
kdbgstream kdFatal(int area=0)
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)
kdbgstream kdDebug(int area=0)