diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 97 | ||||
-rw-r--r-- | examples/aggregation.idl | 27 | ||||
-rw-r--r-- | examples/artsccat.c | 115 | ||||
-rw-r--r-- | examples/artscmt.c | 143 | ||||
-rw-r--r-- | examples/artscrec.c | 115 | ||||
-rw-r--r-- | examples/artsctest.c | 72 | ||||
-rw-r--r-- | examples/catfile.cc | 55 | ||||
-rw-r--r-- | examples/dcasttest.cc | 72 | ||||
-rw-r--r-- | examples/flow.cc | 56 | ||||
-rw-r--r-- | examples/hello.idl | 41 | ||||
-rw-r--r-- | examples/hello_impl.cc | 76 | ||||
-rw-r--r-- | examples/hello_impl.h | 51 | ||||
-rw-r--r-- | examples/helloclient.cc | 76 | ||||
-rw-r--r-- | examples/hellodynamic.cc | 94 | ||||
-rw-r--r-- | examples/hellodynamic2.cc | 124 | ||||
-rw-r--r-- | examples/hellomain.cc | 77 | ||||
-rw-r--r-- | examples/helloserver.cc | 52 | ||||
-rw-r--r-- | examples/irdemo.cc | 155 | ||||
-rw-r--r-- | examples/playtofile.idl | 41 | ||||
-rw-r--r-- | examples/playtofile_impl.cc | 99 | ||||
-rw-r--r-- | examples/playtofile_main.cc | 147 | ||||
-rw-r--r-- | examples/referenceinfo.cc | 57 | ||||
-rw-r--r-- | examples/streamsound.cc | 106 | ||||
-rw-r--r-- | examples/testaggregation.cc | 73 | ||||
-rw-r--r-- | examples/testasubsys.cc | 142 | ||||
-rw-r--r-- | examples/testdhandle.cc | 115 | ||||
-rw-r--r-- | examples/testthreads.cc | 64 | ||||
-rw-r--r-- | examples/x11commtest.cc | 76 |
28 files changed, 2418 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..53c455c --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,97 @@ +INCLUDES = -I$(top_srcdir)/mcop -I$(top_srcdir)/flow \ + -I$(top_builddir)/flow -I$(top_srcdir)/soundserver \ + -I$(top_builddir)/soundserver -I$(top_srcdir)/artsc \ + -I$(top_builddir)/mcop -I$(top_builddir)/artsc \ + $(all_includes) + +AM_LDFLAGS = $(all_libraries) +LDADD = $(top_builddir)/mcop/libmcop.la +FLOWLIBS = $(top_builddir)/flow/libartsflow.la + +####### Files + +check_PROGRAMS = helloserver helloclient referenceinfo \ + irdemo x11commtest streamsound hellomain flow artsctest \ + artscrec artsccat artscmt \ + dcasttest hellodynamic hellodynamic2 testasubsys testaggregation \ + playtofile testthreads testnothreads catfile testdhandle +helloserver_SOURCES = hello.cc helloserver.cc hello_impl.cc +helloclient_SOURCES = hello.cc helloclient.cc +hellodynamic_SOURCES = hello.cc hellodynamic.cc hello_impl.cc +hellodynamic2_SOURCES = hello.cc hellodynamic2.cc +referenceinfo_SOURCES = referenceinfo.cc + +testdhandle_SOURCES = testdhandle.cc +testdhandle.o: ../soundserver/soundserver.h ../flow/artsflow.h +testdhandle_LDADD = $(FLOWLIBS) \ + $(top_builddir)/soundserver/libsoundserver_idl.la \ + $(top_builddir)/flow/gslpp/libgslpp.la + +irdemo_SOURCES = irdemo.cc +x11commtest_SOURCES = x11commtest.cc +hellomain_SOURCES = hellomain.cc hello.cc hello_impl.cc + +testasubsys_SOURCES = testasubsys.cc +testasubsys_LDADD = $(FLOWLIBS) + +## testthreads/testnothreads: +## if we link testthreads.cc against libmcop_mt, it will be able to use +## real threading, if we don't, we won't +testthreads_SOURCES = testthreads.cc +testthreads_LDFLAGS = $(LDFLAGS) +testthreads_LDADD = $(top_builddir)/mcop_mt/libmcop_mt.la +testnothreads_SOURCES = testthreads.cc + +testaggregation_SOURCES = aggregation.cc testaggregation.cc + +flow_LDADD = $(FLOWLIBS) +flow_SOURCES = flow.cc + +streamsound_LDADD = $(FLOWLIBS) \ + $(top_builddir)/soundserver/libsoundserver_idl.la +streamsound_SOURCES = streamsound.cc + +dcasttest_LDADD = $(FLOWLIBS) \ + $(top_builddir)/soundserver/libsoundserver_idl.la +dcasttest_SOURCES = dcasttest.cc + +artsctest_LDADD = $(top_builddir)/artsc/libartsc.la +artsctest_SOURCES = artsctest.c + +artsccat_LDADD = $(top_builddir)/artsc/libartsc.la +artsccat_SOURCES = artsccat.c + +artscrec_LDADD = $(top_builddir)/artsc/libartsc.la +artscrec_SOURCES = artscrec.c + +artscmt_LDFLAGS = $(LDFLAGS) $(USE_THREADS) +artscmt_LDADD = $(top_builddir)/artsc/libartsc.la -lm $(LIBPTHREAD) $(LIBPOSIX4) +artscmt_SOURCES = artscmt.c + +playtofile_SOURCES = playtofile.cc playtofile_impl.cc playtofile_main.cc + +playtofile_LDADD = $(FLOWLIBS) \ + $(top_builddir)/soundserver/libsoundserver_idl.la \ + $(top_builddir)/soundserver/libkmedia2_idl.la + +catfile_SOURCES = catfile.cc + +catfile_LDADD = $(top_builddir)/soundserver/libkmedia2.la + +DISTCLEANFILES = hello.cc hello.h aggregation.cc aggregation.h \ + playtofile.cc playtofile.h + +####### Implicit rules + +####### Build rules + +helloclient.o: hello.h +hello.cc hello.h: $(top_srcdir)/examples/hello.idl $(MCOPIDL) + $(MCOPIDL) $(top_srcdir)/examples/hello.idl + +aggregation.cc aggregation.h: $(top_srcdir)/examples/aggregation.idl $(MCOPIDL) + $(MCOPIDL) $(top_srcdir)/examples/aggregation.idl + +playtofile.cc playtofile.h: $(srcdir)/playtofile.idl $(MCOPIDL) + $(MCOPIDL) -I$(top_srcdir)/flow $(srcdir)/playtofile.idl + diff --git a/examples/aggregation.idl b/examples/aggregation.idl new file mode 100644 index 0000000..3c02d31 --- /dev/null +++ b/examples/aggregation.idl @@ -0,0 +1,27 @@ +/* + + Copyright (C) 2000 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + +*/ + +interface String { + attribute string value; + + void constructor(string value); +}; diff --git a/examples/artsccat.c b/examples/artsccat.c new file mode 100644 index 0000000..5f49f6f --- /dev/null +++ b/examples/artsccat.c @@ -0,0 +1,115 @@ + /* + + Copyright (C) 2001 Matthias Kretz + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +/* + * This is another artscat written with aRts C API. It reads data from + * stdin, and plays it via the aRts soundserver. + * + * Compile programs using the aRts C API with + * + * cc -o artscdump artscdump.c `artsc-config --cflags` `artsc-config --libs` + * + * If you are using a makefile, it could look like this: + * + * CFLAGS=`artsc-config --cflags` + * LDFLAGS=`artsc-config --libs` + * + * artscdump: artscdump.c + */ +#include <artsc.h> + +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> + +static void exitUsage(const char *progname) +{ + fprintf(stderr,"usage: %s [ options ] [ <filename> ]\n",progname); + fprintf(stderr,"-r <samplingrate> set samplingrate to use\n"); + fprintf(stderr,"-b <bits> set number of bits (8 or 16)\n"); + fprintf(stderr,"-c <channels> set number of channels (1 or 2)\n"); + fprintf(stderr,"-h display this help and exit\n"); + exit(1); +} + +int main(int argc, char ** argv) +{ + int cfgSamplingRate = 44100; + int cfgBits = 16; + int cfgChannels = 2; + FILE *infile = stdin; + int pfd; + int packetsize; + char *buffer; + int size = 1024; + arts_stream_t stream; + int optch; + while((optch = getopt(argc,argv,"r:b:c:h")) > 0) + { + switch(optch) + { + case 'r': cfgSamplingRate = atoi(optarg); + break; + case 'b': cfgBits = atoi(optarg); + break; + case 'c': cfgChannels = atoi(optarg); + break; + case 'h': + default: + exitUsage(argc?argv[0]:"artsccat"); + break; + } + } + + if (optind < argc) + { + if(argv[optind] != "-") + { + infile = fopen(argv[optind],"w"); + if(!infile) + { + fprintf( stderr, "Can't open file '%s'.\n", argv[optind] ); + exit(1); + } + } + } + + pfd = fileno( infile ); + + arts_init(); + stream = arts_play_stream( cfgSamplingRate, cfgBits, cfgChannels, "artsccat" ); + packetsize = arts_stream_get( stream, ARTS_P_PACKET_SIZE ); + buffer = malloc(packetsize); + + do { + size = read( pfd, buffer, packetsize ); + size = arts_write( stream, buffer, size ); + } while( size > 0 ); + + arts_close_stream( stream ); + arts_free(); + + pclose( infile ); + free(buffer); + + return 0; +} diff --git a/examples/artscmt.c b/examples/artscmt.c new file mode 100644 index 0000000..bc9e98d --- /dev/null +++ b/examples/artscmt.c @@ -0,0 +1,143 @@ + /* + + Copyright (C) 2003 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +/* + * This is a threaded example using the aRts C API. + * + * Compile programs using the aRts C API with + * + * cc -o artscmt artscmt.c `artsc-config --cflags` `artsc-config --libs` + * + * If you are using a makefile, it could look like this: + * + * CFLAGS=`artsc-config --cflags` + * LDFLAGS=`artsc-config --libs` + * + * artscmt: artscmt.c + */ +#include <artsc.h> +#include <pthread.h> +#include <assert.h> +#include <math.h> +#include <unistd.h> +#include <stdio.h> + +pthread_mutex_t arts_mutex; /* pthread mutex */ + +struct Writer +{ + pthread_t thread; + arts_stream_t stream; + float freq; +}; + +#define BUFFER_SIZE 1024 + +static void* writer(void *arg) +{ + struct Writer *self = arg; + int pos = 0; + + while(pos < 44100*10) + { + char buffer[BUFFER_SIZE], *to = buffer; + int i, written; + + for(i=0;i<BUFFER_SIZE/4;i++) + { + /* generate two sin waves */ + float fpos = ((float)pos)/44100.0; + long sample = (long)(sin(fpos*6.28*self->freq)*15000.0); + pos++; + + /* put the samples in the packet */ + *to++ = sample & 0xff; + *to++ = (sample >> 8) & 0xff; + *to++ = sample & 0xff; + *to++ = (sample >> 8) & 0xff; + } + + written = 0; + do + { + int space; + + /* + * Since there is more than one thread, it is important not to keep the lock + * for a long time. We definitely don't want arts_write to block, while we + * keep the lock, to ensure that other threads can do something as well. So + * we check the available buffer space before writing to avoid blocking. + */ + pthread_mutex_lock(&arts_mutex); + space = arts_stream_get(self->stream, ARTS_P_BUFFER_SPACE); + if (space >= BUFFER_SIZE) + { + written = arts_write(self->stream, buffer, BUFFER_SIZE); + assert(written == BUFFER_SIZE); /* should handle errors here */ + } + pthread_mutex_unlock(&arts_mutex); + + /* + * If the buffer is full, wait some time to get free space again. The amout of + * time to wait needs to correspond to the buffer size we use for refilling. + */ + if (!written) + usleep(10000); /* 10ms */ + } while(!written); + } + return 0; +} + +int main() +{ + struct Writer writer1, writer2; + int error; + + error = arts_init(); + if(error < 0) + { + fprintf(stderr, "error initializing aRts driver: %s\n", arts_error_text(error)); + return 1; + } + + pthread_mutex_init(&arts_mutex, 0); + + writer1.stream = arts_play_stream(44100, 16, 2, "artscmt1"); + writer1.freq = 440; + pthread_create(&writer1.thread, NULL, writer, &writer1); + + writer2.stream = arts_play_stream(44100, 16, 2, "artscmt2"); + writer2.freq = 880; + pthread_create(&writer2.thread, NULL, writer, &writer2); + + pthread_join(writer1.thread, NULL); + pthread_join(writer2.thread, NULL); + + arts_close_stream(writer1.stream); + arts_close_stream(writer2.stream); + + pthread_mutex_destroy(&arts_mutex); + + arts_free(); + + return 0; +} diff --git a/examples/artscrec.c b/examples/artscrec.c new file mode 100644 index 0000000..80e91ae --- /dev/null +++ b/examples/artscrec.c @@ -0,0 +1,115 @@ + /* + + Copyright (C) 2001 Matthias Kretz + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +/* + * This is another artsdump written with aRts C API. It reads data from + * the aRts soundserver and plays it via stdout. + * + * Compile programs using the aRts C API with + * + * cc -o artscdump artscdump.c `artsc-config --cflags` `artsc-config --libs` + * + * If you are using a makefile, it could look like this: + * + * CFLAGS=`artsc-config --cflags` + * LDFLAGS=`artsc-config --libs` + * + * artscdump: artscdump.c + */ +#include <artsc.h> + +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> + +static void exitUsage(const char *progname) +{ + fprintf(stderr,"usage: %s [ options ] [ <filename> ]\n",progname); + fprintf(stderr,"-r <samplingrate> set samplingrate to use\n"); + fprintf(stderr,"-b <bits> set number of bits (8 or 16)\n"); + fprintf(stderr,"-c <channels> set number of channels (1 or 2)\n"); + fprintf(stderr,"-h display this help and exit\n"); + exit(1); +} + +int main(int argc, char ** argv) +{ + int cfgSamplingRate = 44100; + int cfgBits = 16; + int cfgChannels = 2; + FILE *outfile = stdout; + int pfd; + int packetsize; + char *buffer; + int size; + arts_stream_t stream; + int optch; + while((optch = getopt(argc,argv,"r:b:c:h")) > 0) + { + switch(optch) + { + case 'r': cfgSamplingRate = atoi(optarg); + break; + case 'b': cfgBits = atoi(optarg); + break; + case 'c': cfgChannels = atoi(optarg); + break; + case 'h': + default: + exitUsage(argc?argv[0]:"artscdump"); + break; + } + } + + if (optind < argc) + { + if(argv[optind] != "-") + { + outfile = fopen(argv[optind],"w"); + if(!outfile) + { + fprintf( stderr, "Can't open file '%s'.\n", argv[optind] ); + exit(1); + } + } + } + + pfd = fileno( outfile ); + + arts_init(); + stream = arts_record_stream( cfgSamplingRate, cfgBits, cfgChannels, "artscdump" ); + packetsize = arts_stream_get( stream, ARTS_P_PACKET_SIZE ); + buffer = malloc(packetsize); + + do { + size = arts_read( stream, buffer, packetsize ); + size = write( pfd, buffer, size ); + } while( size > 0 ); + + arts_close_stream( stream ); + arts_free(); + + pclose( outfile ); + free(buffer); + + return 0; +} diff --git a/examples/artsctest.c b/examples/artsctest.c new file mode 100644 index 0000000..9eb5a9b --- /dev/null +++ b/examples/artsctest.c @@ -0,0 +1,72 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +/* + * This is another artscat written with aRts C API. It reads data from + * stdin, and plays it via the aRts soundserver. + * + * Compile programs using the aRts C API with + * + * cc -o artsctest artsctest.c `artsc-config --cflags` `artsc-config --libs` + * + * If you are using a makefile, it could look like this: + * + * CFLAGS=`artsc-config --cflags` + * LDFLAGS=`artsc-config --libs` + * + * artsctest: artsctest.c + */ + +#include <artsc.h> +#include <stdio.h> + +int main() +{ + arts_stream_t stream; + char buffer[8192]; + int bytes; + int errorcode; + + errorcode = arts_init(); + if(errorcode < 0) + { + fprintf(stderr,"arts_init error: %s\n", arts_error_text(errorcode)); + return 1; + } + + stream = arts_play_stream(44100,16,2,"artsctest"); + + while((bytes = fread(buffer,1,8192,stdin)) > 0) + { + errorcode = arts_write(stream,buffer,bytes); + if(errorcode < 0) + { + fprintf(stderr,"arts_write error: %s\n",arts_error_text(errorcode)); + return 1; + } + } + + arts_close_stream(stream); + arts_free(); + + return 0; +} diff --git a/examples/catfile.cc b/examples/catfile.cc new file mode 100644 index 0000000..2b34342 --- /dev/null +++ b/examples/catfile.cc @@ -0,0 +1,55 @@ +/* + + Copyright (C) 2001 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + +*/ + +#include <stdio.h> +#include <kmedia2.h> +#include <connect.h> + +using namespace std; +using namespace Arts; + +int main(int argc, char **argv) +{ + if(argc != 2) + { + fprintf(stderr, "usage: catfile <file>\n"); + exit(1); + } + + Dispatcher dispatcher; + FileInputStream file; + StdoutWriter writer; + + if(!file.open(argv[1])) + { + printf("can't open file %s\n",argv[1]); + exit(1); + } + + connect(file, writer); + + file.start(); + writer.start(); + + while(!file.eof()) + dispatcher.ioManager()->processOneEvent(false); +} diff --git a/examples/dcasttest.cc b/examples/dcasttest.cc new file mode 100644 index 0000000..1d4127b --- /dev/null +++ b/examples/dcasttest.cc @@ -0,0 +1,72 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld [email protected] + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include <stdio.h> +#include <soundserver.h> +#include <iostream> + +using namespace std; +using namespace Arts; + +/* + * This is a little test program that illustrates remote dynamic casting. + * Arts_SimpleSoundServer is a global object that is a SimpleSoundServer, + * so the first dynamic cast should succeed, while it is no Synth_PLAY, + * so the second dynamic cast should fail. + */ + +int main() +{ + Dispatcher dispatcher; + Object obj = Reference("global:Arts_SimpleSoundServer"); + + if(obj.isNull()) + { + cerr << "this test will only work if artsd is running" << endl; + exit(1); + } + + /* + * TODO: fix this. This fails currently as in the first line only an + * Object_stub will get created, no SimpleSoundServer_stub, and _cast + * which is used by DynamicCast wil only try to cast the _stub object, + * not do any further tricks. + */ + SimpleSoundServer server = DynamicCast(obj); + if(server.isNull()) + { + cerr << "remote dynamic casting is too restrictive" << endl; + exit(1); + } + + Synth_PLAY play = DynamicCast(obj); + if(!play.isNull()) + { + cerr << "remote dynamic casting doesn't do proper checks" << endl; + exit(1); + } + + cout << "remote dynamic casting seems to be fine" << endl; + return 0; +} diff --git a/examples/flow.cc b/examples/flow.cc new file mode 100644 index 0000000..116c21b --- /dev/null +++ b/examples/flow.cc @@ -0,0 +1,56 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld, [email protected] + Nicolas Brodu, [email protected] + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "artsflow.h" +#include "connect.h" + +using namespace Arts; + +int main() +{ + Dispatcher dispatcher; + + // object creation + Synth_FREQUENCY freq; + Synth_WAVE_SIN sin; + Synth_PLAY play; + + // object initialization + setValue(freq, 440.0); + + // object connection + connect(freq, sin); + connect(sin, play, "invalue_left"); + connect(sin, play, "invalue_right"); + + // start all objects (maybe we should group objects like with QWidget + // parents and such?) + freq.start(); + sin.start(); + play.start(); + + // go + dispatcher.run(); +} diff --git a/examples/hello.idl b/examples/hello.idl new file mode 100644 index 0000000..8446500 --- /dev/null +++ b/examples/hello.idl @@ -0,0 +1,41 @@ +/* + Copyright (C) 1999 Stefan Westerfeld + Modified by Nicolas Brodu, [email protected] + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. +*/ + +// Test stuff: +interface HelloBase { + void hellobase(string s); +}; + +interface Hello : HelloBase { + /* + * counter like functionality (init, adding, getting total value) + */ + attribute long myValue; + void constructor(long i); + void add(long l); + void printTotal(); + + /* + * some hello world like stuff, string and number addition + */ + void hello(string s); + long sum(long a,long b); + string concat(string s1, string s2); +}; diff --git a/examples/hello_impl.cc b/examples/hello_impl.cc new file mode 100644 index 0000000..a3be704 --- /dev/null +++ b/examples/hello_impl.cc @@ -0,0 +1,76 @@ +/* + + Copyright (C) 1999 Stefan Westerfeld + Modified by Nicolas Brodu, [email protected] + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. +*/ + +#include "hello_impl.h" +#include <stdio.h> + +using namespace std; + +void Hello_impl::hellobase(const string& s) +{ + printf("Hellobase '%s'!\n",s.c_str()); +} + +void Hello_impl::hello(const string& s) +{ + printf("Hello '%s'!\n",s.c_str()); +} + +void Hello_impl::constructor(long i) +{ + myvalue=i; +} + +long Hello_impl::myValue() +{ + return myvalue; +} + +void Hello_impl::myValue(long newValue) +{ + myvalue=newValue; +} + +void Hello_impl::add(long a) +{ + myvalue += a; +} + +void Hello_impl::printTotal() +{ + printf("Total=%ld\n",myvalue); +} + +long Hello_impl::sum(long a, long b) +{ + return a+b; +} + +string Hello_impl::concat(const string& s1, const string& s2) +{ + return s1+s2; +} + +REGISTER_IMPLEMENTATION(Hello_impl); diff --git a/examples/hello_impl.h b/examples/hello_impl.h new file mode 100644 index 0000000..484a890 --- /dev/null +++ b/examples/hello_impl.h @@ -0,0 +1,51 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld + Modified by Nicolas Brodu, [email protected] + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#ifndef HELLO_IMPL_H +#define HELLO_IMPL_H + +#include "hello.h" + +using namespace std; + +class Hello_impl : virtual public Hello_skel { +private: + long myvalue; +public: + long myValue(); + void myValue(long newValue); + void constructor(long i); + void add(long increment); + void printTotal(); + + void hello(const string& s); + long sum(long a, long b); + string concat(const string& s1, const string& s2); + + void hellobase(const string& s); +}; + +#endif /* HELLO_IMPL_H */ diff --git a/examples/helloclient.cc b/examples/helloclient.cc new file mode 100644 index 0000000..c7d3022 --- /dev/null +++ b/examples/helloclient.cc @@ -0,0 +1,76 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "hello.h" + +#include <stdio.h> +#include <stdlib.h> + +using namespace Arts; + +int main(int argc, char **argv) +{ + if(argc != 2) + { + fprintf(stderr,"usage: %s <mcop reference>\n",argv[0]); + exit(1); + } + + Dispatcher dispatcher; + + Hello h = Reference(argv[1]); + if(h.isNull()) + { + fprintf(stderr,"can't connect to the object\n"); + exit(1); + } + + const char *who = getenv("LOGNAME"); + if(!who) who = "stefan"; + + printf("calling h.hello(\"%s\")\n", who); + h.hello(who); +/* + printf("h->sum(2,4) is %ld\n", + h->sum2(2,4)); + + printf("h->sum(2,4,6) is %ld\n", + h->sum3(2,4,6)); +*/ + h.myValue(6); + printf("h.myValue(6), h.myValue() is %ld\n", + h.myValue()); + + printf("h.concat(\"MCOP \",\"is great!\") is \"%s\"\n", + h.concat("MCOP ","is great!").c_str()); + + /* + int i,j = 0; + for(i=0;i<100000;i++) + j += h->sum2(2,4); + printf("%d\n",j); + */ + return 0; +} diff --git a/examples/hellodynamic.cc b/examples/hellodynamic.cc new file mode 100644 index 0000000..5dabec0 --- /dev/null +++ b/examples/hellodynamic.cc @@ -0,0 +1,94 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "hello.h" +#include "dynamicrequest.h" + +#include <stdio.h> +#include <stdlib.h> +#include <iostream> + +using namespace std; +using namespace Arts; + +/* + * Hellodynamic illustrates the usage of the DynamicRequest class to perform + * dynamic requests. There are two things you can try + * + * hellodynamic + * + * which will use a local implementation of a hello object, and + * + * hellodynamic <mcop reference> + * + * which will use a remote implementation of a hello object. + */ +int main(int argc, char **argv) +{ + Dispatcher dispatcher; + Hello h; + + // Check if we should connect to a remote server: + if(argc == 2) + { + h = Reference(argv[1]); + if(h.isNull()) + { + fprintf(stderr,"can't connect to the object\n"); + exit(1); + } + } + + // find out logname + const char *who = getenv("LOGNAME"); + if(!who) who = "stefan"; + + printf("calling h.hello(\"%s\")\n", who); + + // the dynamic equivalent to h.hello(who) + DynamicRequest hellorequest(h); + + if(!hellorequest.method("hello").param(who).invoke()) + cout << "dynamic invocation to h.hello(...) failed" << endl; + + /* + Note: you could reuse hellorequest here, and indeed, if you + call the _same_ method over and over again, you'll get quite + some speed gain as the signature is only looked up once. + */ + + // set a value (no error checking here, I am lazy ;) + DynamicRequest(h).method("_set_myValue").param(42).invoke(); + + // get a value via "normal" interface + printf("h.myValue() is %ld (after setting it to 42)\n", h.myValue()); + + // call h.concat("MCOP ","rules.") dynamically + string s; + DynamicRequest(h).method("concat").param("MCOP ").param("rules.").invoke(s); + cout << "Conclusion of this test: " << s << endl; + + return 0; +} diff --git a/examples/hellodynamic2.cc b/examples/hellodynamic2.cc new file mode 100644 index 0000000..8f8a1e0 --- /dev/null +++ b/examples/hellodynamic2.cc @@ -0,0 +1,124 @@ + /* + + Copyright (C) 2001 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +#include "hello.h" +#include "dynamicskeleton.h" +#include "debug.h" +#include "stdio.h" + +using namespace Arts; +using namespace std; + +namespace Arts { typedef DynamicSkeleton<Arts::Object_skel> Object_dskel; } + +class HelloDynamic : public Arts::Object_dskel { +protected: + long myValue; + +public: + HelloDynamic() : Arts::Object_dskel("Hello") { + } + void process(long methodID, Arts::Buffer *request, Arts::Buffer *result) + { + const Arts::MethodDef& methodDef = getMethodDef(methodID); + + if(methodDef.name == "hello") // void hello(string); + { + string s; request->readString(s); + printf("Hello '%s'!\n",s.c_str()); + } + else if(methodDef.name == "_set_myValue") // attribute long myValue; + { + myValue = request->readLong(); + } + else if(methodDef.name == "_get_myValue") + { + result->writeLong(myValue); + } + else if(methodDef.name == "concat") + { + string s1; request->readString(s1); + string s2; request->readString(s2); + result->writeString(s1+s2); + } + else if(methodDef.name == "sum") + { + long a = request->readLong(); + long b = request->readLong(); + result->writeLong(a+b); + } + else + { + arts_fatal("method %s unimplemented", methodDef.name.c_str()); + } + } +}; + +/* + * this program illustrates that you /can/ implement an interface without + * using the IDL compiler skeleton do to so - this is useful if you want + * to create a language or object system binding, where you don't know + * all interfaces that are present at compile time + * + * however, it's definitely advanced stuff, and not for daily use ;) + */ +int main() +{ + Dispatcher dispatcher; + + Object obj = Object::_from_base(new HelloDynamic()); + if(obj.isNull()) + arts_fatal("dimpl is no object?"); + + HelloBase b = DynamicCast(obj); + if(b.isNull()) + arts_fatal("can't cast the object to HelloBase"); + + + Hello h = DynamicCast(obj); + /*arts_info("%d",obj.isCompatibleWith("Hello"));*/ + + if(h.isNull()) + arts_fatal("can't destringify to the object"); + + const char *who = getenv("LOGNAME"); + if(!who) who = "stefan"; + + arts_info("calling h.hello(\"%s\")", who); + h.hello(who); + + h.myValue(6); + arts_info("h.myValue(6), h.myValue() is %ld", + h.myValue()); + + arts_info("h.concat(\"MCOP \",\"is great!\") is \"%s\"", + h.concat("MCOP ","is great!").c_str()); + + /* + int i,j = 0; + for(i=0;i<100000;i++) + j += h.sum(2,4); + printf("%d\n",j); + */ + + return 0; +} diff --git a/examples/hellomain.cc b/examples/hellomain.cc new file mode 100644 index 0000000..f194456 --- /dev/null +++ b/examples/hellomain.cc @@ -0,0 +1,77 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld [email protected] + Nicolas Brodu, [email protected] + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "hello.h" +#include <iostream> + +using namespace std; +using namespace Arts; + +int main() +{ + Dispatcher dispatcher; + + // Just use plain C++! + Hello server; + server.hello("server"); + + // creation from MCOP reference + Hello client( Reference( server.toString() ) ); + if (!client.isNull()) client.hello("local test"); + else cout << "fatal: a valid reference can't be decoded" << endl; + + // creation from wrong reference + Hello xclient( Reference("fdshjkhdsf") ); + + // check result => should be null + if (xclient.isNull()) cout << "passed invalid reference test" << endl; + else cout << "fatal: an invalid reference can be decoded" << endl; + + // using a constructor + Hello hint(3); + cout << hint.myValue() << endl; + + + Hello castsrc; + HelloBase casthint = castsrc; + casthint.hellobase("cast test passed"); + if(castsrc.isNull()) cout << "cast problem with auto creation" << endl; + castsrc.hello("second cast test passed"); + + // Can copy (or here copy constructor) the objects + Hello hcopy = hint; + hcopy.add(5); + hcopy.printTotal(); + + // dynamic creation is OK + Hello* dyn = new Hello; + cout << dyn->concat("I am"," a dynamic hello") << endl; + dyn->hellobase("dynamic hellobase hello"); + delete dyn; + +// dispatcher.run(); + return 0; + +} diff --git a/examples/helloserver.cc b/examples/helloserver.cc new file mode 100644 index 0000000..2e37de5 --- /dev/null +++ b/examples/helloserver.cc @@ -0,0 +1,52 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "common.h" +#include "hello_impl.h" + +#include <stdio.h> +#include <vector> +#include <string> + +using namespace std; +using namespace Arts; + +int main() +{ + Dispatcher dispatcher(0,Dispatcher::startUnixServer); + Hello server; + + string reference = server.toString(); + printf("%s\n",reference.c_str()); + + Hello h = Reference(reference); + if(!h.isNull()) + h.hello("local test"); + else + printf("Local access to the Hello_impl object failed?\n"); + + dispatcher.run(); + return 0; +} diff --git a/examples/irdemo.cc b/examples/irdemo.cc new file mode 100644 index 0000000..4345235 --- /dev/null +++ b/examples/irdemo.cc @@ -0,0 +1,155 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "common.h" + +#include <stdio.h> +#include <stdlib.h> +#include <vector> +#include <string> + +using namespace std; +using namespace Arts; + +static void printInterface(InterfaceDef id) +{ + string inherit; + if(id.inheritedInterfaces.size()) + { + vector<string>::iterator ii; + bool first = true; + inherit = ": "; + for(ii = id.inheritedInterfaces.begin(); ii != id.inheritedInterfaces.end(); ii++) + { + if(!first) inherit +=","; + first = false; + inherit += (*ii)+" "; + } + } + printf("interface %s %s{\n",id.name.c_str(),inherit.c_str()); + // attributes, streams + vector<AttributeDef>::iterator ai; + for(ai = id.attributes.begin(); ai != id.attributes.end(); ai++) + { + const AttributeDef& ad = *ai; + if(ad.flags & attributeAttribute) + { + /* readwrite */ + if(ad.flags & (streamOut|streamIn) == (streamOut|streamIn)) + { + printf(" attribute %s %s;\n",ad.type.c_str(), ad.name.c_str()); + } + else + { + if(ad.flags & streamOut) /* readable from outside */ + { + printf(" readonly attribute %s %s;\n", + ad.type.c_str(), ad.name.c_str()); + } + if(ad.flags & streamIn) /* writeable from outside */ + { + printf(" ?writeonly? attribute %s %s;\n", + ad.type.c_str(), ad.name.c_str()); + } + } + } + if(ad.flags & attributeStream) + { + const char *dir = (ad.flags & streamOut)?"out":"in"; + const char *async = (ad.flags & streamAsync)?"async ":""; + string type = ad.type; + if(type == "float" && !(ad.flags & streamAsync)) type = "audio"; + + printf(" %s%s %s stream %s;\n",async,dir, + type.c_str(),ad.name.c_str()); + } + } + + // methods + vector<MethodDef>::iterator mi; + for(mi = id.methods.begin();mi != id.methods.end(); mi++) + { + MethodDef& md = *mi; + printf(" %s %s(",md.type.c_str(),md.name.c_str()); + + bool first = true; + vector<ParamDef>::iterator pi; + for(pi = md.signature.begin(); pi != md.signature.end(); pi++) + { + ParamDef& pd = *pi; + if(!first) printf(", "); + printf("%s %s",pd.type.c_str(),pd.name.c_str()); + first = false; + } + printf(");\n"); + } + printf("}\n"); +} + +/* + * This demo shows that you can find out what interface an object has and + * what types it needs to work without knowing anything but the object + * reference. + * + * The reason for that is that every object offers the _interfaceName + * _queryInterface and _queryType methods, which you can use to find out + * anything you need to know to talk to that object. + * + * Just pass this programm an MCOP object reference, and it will print out + * the corresponding interface. + * + * (TODO: one could make it print out all inherited interfaces and the + * types that are used in the interface, too. Ports, etc could be shown + * as well). + */ + +/** + * Interface repository demo - reasonable testcase: + * + * - make sure artsd is running + * - irdemo global:Arts_SimpleSoundServer + */ +int main(int argc, char **argv) +{ + if(argc != 2) + { + fprintf(stderr,"usage: %s <mcop reference>\n",argv[0]); + exit(1); + } + + Dispatcher dispatcher; + Object o = Reference(argv[1]); + if(o.isNull()) + { + fprintf(stderr,"can't connect to the object\n"); + exit(1); + } + + string iname = o._interfaceName(); + InterfaceDef idef = o._queryInterface(iname); + printInterface(idef); + + return 0; +} diff --git a/examples/playtofile.idl b/examples/playtofile.idl new file mode 100644 index 0000000..6aa341e --- /dev/null +++ b/examples/playtofile.idl @@ -0,0 +1,41 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +/* + * this is an example interface which dumps things to a file + */ +#include <artsflow.idl> + +module Arts { + interface PlayToFile : SynthModule { + /** + * filename, - = default = stdout + */ + attribute string filename; + + /** + * write more data to file ;) + */ + void goOn(); + default in audio stream left, right; + }; +}; diff --git a/examples/playtofile_impl.cc b/examples/playtofile_impl.cc new file mode 100644 index 0000000..63a5708 --- /dev/null +++ b/examples/playtofile_impl.cc @@ -0,0 +1,99 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +#include "playtofile.h" +#include "flowsystem.h" +#include "stdsynthmodule.h" +#include "convert.h" +#include "stdio.h" + +using namespace Arts; +using namespace std; + +namespace Arts { + class PlayToFile_impl : virtual public PlayToFile_skel, + virtual public StdSynthModule + { + typedef unsigned char uchar; + unsigned char *outblock; + unsigned long maxsamples; + string _filename; + FILE *out; + public: + PlayToFile_impl() : _filename("-") + { + } + void filename(const string& newfilename) { + _filename = newfilename; + filename_changed(_filename); + } + + string filename() { return _filename; } + + void streamInit() { + maxsamples = 0; + outblock = 0; + if(_filename == "-") + out = stdout; + else + out = fopen(_filename.c_str(),"w"); + } + void streamEnd() { + if(outblock) + { + delete[] outblock; + outblock = 0; + } + if(out != stdout) + { + fclose(out); + } + } + + void calculateBlock(unsigned long samples) + { + if(samples > maxsamples) + { + maxsamples = samples; + + if(outblock) delete[] outblock; + outblock = new uchar[maxsamples * 4]; // 2 channels, 16 bit + } + + convert_stereo_2float_i16le(samples,left,right, outblock); + fwrite(outblock, 1, 4 * samples,out); + } + + /* + * this is the most tricky part here - since we will run in a context + * where no audio hardware will play the "give me more data role", + * we'll have to request things ourselves (requireFlow() tells the + * flowsystem that more signal flow should happen, so that + * calculateBlock will get called + */ + void goOn() + { + _node()->requireFlow(); + } + }; + REGISTER_IMPLEMENTATION(PlayToFile_impl); +}; diff --git a/examples/playtofile_main.cc b/examples/playtofile_main.cc new file mode 100644 index 0000000..c7f85fd --- /dev/null +++ b/examples/playtofile_main.cc @@ -0,0 +1,147 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + + */ + +#include "playtofile.h" +#include "kmedia2.h" +#include "debug.h" +#include "dynamicrequest.h" +#include "connect.h" +#include "flowsystem.h" +#include <ctype.h> +#include <iostream> + +using namespace Arts; +using namespace std; + +/* copypasted from simplesoundserver_impl.cc */ +PlayObject createPlayObject(const string& filename) +{ + string objectType = ""; + + /* + * figure out extension (as lowercased letters) + */ + string extension = ""; + bool extensionok = false; + string::const_reverse_iterator i; + for(i = filename.rbegin(); i != filename.rend() && !extensionok; i++) + { + if(*i == '.') + extensionok = true; + else + extension = (char)tolower(*i) + extension; + } + + /* + * query trader for PlayObjects which support this + */ + if(extensionok) + { + arts_debug("search playobject, extension = %s",extension.c_str()); + + TraderQuery query; + query.supports("Interface","Arts::PlayObject"); + query.supports("Extension",extension); + + vector<TraderOffer> *offers = query.query(); + if(!offers->empty()) + objectType = offers->front().interfaceName(); // first offer + + delete offers; + } + + /* + * create a PlayObject and connect it + */ + if(!objectType.empty()) + { + arts_debug("creating %s to play file", objectType.c_str()); + + PlayObject result = SubClass(objectType); + if(result.loadMedia(filename)) + { + result._node()->start(); + return result; + } + else arts_warning("couldn't load file %s", filename.c_str()); + } + else arts_warning("file format extension %s unsupported",extension.c_str()); + + return PlayObject::null(); +} + +/* + * This is an example for dumping things to a file. It demonstates, that + * + * a) you can use all aRts objects in a non-realtime environment (i.e. + * the time in the PlayObject will pass much faster than the outside + * time when dumping things to a file) + * + * b) complayObjectnents will be dynamically loaded as needed + * + * c) you need to do small tricks to get the signal flow going when you are + * not having audio modules around + * + * d) by implementing aRts modules, you can easily grab and insert data from/to + * arbitary non-aRts sources - for instance, a wave editor might implement + * own modules to get the wave from/to memory + */ +int main(int argc, char **argv) +{ + Dispatcher d; + + if(argc != 2 && argc != 3) + { + cerr << "usage: playtofile <inputfile> [ <outputfile> ]" << endl; + exit(1); + } + PlayObject playObject = createPlayObject(argv[1]); + if(playObject.isNull()) + { + cerr << "can't read inputfile " << argv[1] << endl; + exit(1); + } + + playObject.play(); + + PlayToFile playToFile; + if(argc == 3) playToFile.filename(argv[2]); + + connect(playObject, "left", playToFile,"left"); + connect(playObject, "right", playToFile,"right"); + + // <ignore the following lines> + // special case when using mpeglib, do tell it not to block (internal + // interface) - we also put an usleep here to ensure that the threads + // and everything is fully initialized as soon as we start + usleep(100000); + if(playObject._base()->_isCompatibleWith("DecoderBaseObject")) + if(!DynamicRequest(playObject).method("_set_blocking").param(true).invoke()) + cerr << "mpeglib, and blocking attribute can't be changed?" << endl; + // </ignore> + + playToFile.start(); + while(playObject.state() != posIdle) + playToFile.goOn(); + + return 0; +} diff --git a/examples/referenceinfo.cc b/examples/referenceinfo.cc new file mode 100644 index 0000000..d73776c --- /dev/null +++ b/examples/referenceinfo.cc @@ -0,0 +1,57 @@ + /* + + Copyright (C) 1999 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "common.h" +#include <stdio.h> + +using namespace Arts; + +int main(int argc, char **argv) +{ + Dispatcher dispatcher; + ObjectReference r; + + if(argc != 2) + { + fprintf(stderr,"usage: %s <mcop reference>\n",argv[0]); + exit(1); + } + + if(!dispatcher.stringToObjectReference(r,argv[1])) + { + fprintf(stderr, + "Can't read this object reference (invalid?)\n"); + exit(1); + } + printf("serverID: %s\n",r.serverID.c_str()); + printf("objectID: %ld\n\n",r.objectID); + + printf("URLs:\n"); + std::vector<std::string>::iterator ui; + for(ui = r.urls.begin(); ui != r.urls.end();ui++) + printf(" - %s\n",ui->c_str()); + + return 0; +} diff --git a/examples/streamsound.cc b/examples/streamsound.cc new file mode 100644 index 0000000..a19bd27 --- /dev/null +++ b/examples/streamsound.cc @@ -0,0 +1,106 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "soundserver.h" +#include "stdsynthmodule.h" + +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> +#include <iostream> +#include <math.h> + +using namespace std; +using namespace Arts; + +class Sender : public ByteSoundProducer_skel, + public StdSynthModule +{ + bool _finished; + int pos; +public: + Sender() : _finished(false), pos(0) + { + } + long samplingRate() { return 44100; } + long channels() { return 2; } + long bits() { return 16; } + bool finished() { return _finished; } + + static const int packetCount = 10, packetCapacity = 1024; + void streamStart() + { + /* + * start streaming + */ + outdata.setPull(packetCount, packetCapacity); + } + + void request_outdata(DataPacket<mcopbyte> *packet) + { + /* + * fill the packet with new data + */ + // ----------------- audio fill up start ----------------- + + int j; + mcopbyte *to = &packet->contents[0]; + for(j=0;j<packetCapacity/4;j++) + { + // generate two sin waves + float fpos = ((float)pos)/44100.0; + long left = (long)(sin(fpos*6.28*440.0)*30000.0); + long right = (long)(sin(fpos*6.28*880.0)*30000.0); + pos++; + + // put the samples in the packet + *to++ = left & 0xff; + *to++ = (left >> 8) & 0xff; + *to++ = right & 0xff; + *to++ = (right >> 8) & 0xff; + } + + // ------------------ audio fill up end ------------------ + packet->send(); + } +}; + +int main(int argc, char **argv) +{ + Dispatcher dispatcher; + SimpleSoundServer server = Reference("global:Arts_SimpleSoundServer"); + + if(server.isNull()) + { + cerr << "Can't connect to sound server" << endl; + return 1; + } + + ByteSoundProducer sender = ByteSoundProducer::_from_base(new Sender()); + server.attach(sender); + sender.start(); + dispatcher.run(); + server.detach(sender); // in this example: not reached +} diff --git a/examples/testaggregation.cc b/examples/testaggregation.cc new file mode 100644 index 0000000..76c2773 --- /dev/null +++ b/examples/testaggregation.cc @@ -0,0 +1,73 @@ +/* + + Copyright (C) 2000 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + +*/ + +#include "aggregation.h" +#include <iostream> + +using namespace std; +using namespace Arts; + +class String_impl : virtual public String_skel { + string _value; +public: + void constructor(const std::string& value) { _value = value; } + string value() { return _value; } + void value(const std::string& newValue) { _value = newValue; } +}; + +REGISTER_IMPLEMENTATION(String_impl); + +void printall(Arts::Object o, std::string pre = "") +{ + String s; + s = DynamicCast(o); + if(!s.isNull()) + cout << pre << " - " << s.value() << endl; + + vector<string> *children = s._queryChildren(); + vector<string>::iterator i; + for(i = children->begin(); i != children->end(); i++) + printall(s._getChild(*i), pre+" "); +} + +/* + * Small test case for aggregation + * + * The idea behind all this is that you can add children to objects, building + * a tree (or if you like graph) of objects without the objects knowing anything + * about this. Aggregation has strong references, that means, the objects you + * add together in a tree will stay this way, as long as you keep a reference + * to the root. + */ +int main() +{ + Dispatcher d; + + String s("spreadsheet"); + s._addChild(String("chart"),"child1"); + s._addChild(String("image"),"child2"); + printall(s); + + cout << "deleting \"child1\"" << endl; + if(!s._removeChild("child1")) cout << "FAIL" << endl; + printall(s); +} diff --git a/examples/testasubsys.cc b/examples/testasubsys.cc new file mode 100644 index 0000000..e0480ed --- /dev/null +++ b/examples/testasubsys.cc @@ -0,0 +1,142 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include <iostream> +#include <math.h> +#include "audiosubsys.h" +#include "convert.h" +#include "dispatcher.h" + +using namespace std; +using namespace Arts; + +class SinTest : public ASProducer { +protected: + int audiofd; + int done; + AudioSubSystem *as; + bool attached; + +public: + SinTest(); + virtual ~SinTest(); + void needMore(); + void run(long samples); +}; + +SinTest::SinTest() : done(0), as(AudioSubSystem::the()) +{ + // initialize and open audio subsystem (defaults to 44kHz, 16bit, stereo) + attached = as->attachProducer(this); + if(!attached) + { + cout << "audio subsystem is already used?" << endl; + } + else + { + if(as->open() == false) + { + cout << "audio subsystem init failed (" + << as->error() << ")" << endl; + audiofd = -1; + } + else + audiofd = as->selectWriteFD(); + } +} + +SinTest::~SinTest() +{ + // deinitialize the audio subsystem + if(attached) + as->detachProducer(); +} + +void SinTest::run(long samples) +{ + // run this loop until enough samples are produced + while(attached && audiofd >= 0 && (done < samples)) + { + fd_set wfds, efds; + FD_ZERO(&wfds); + FD_ZERO(&efds); + FD_SET(audiofd, &wfds); + FD_SET(audiofd, &efds); + + // look if the audio filedescriptor becomes writeable + int result = select(audiofd+1,0,&wfds,&efds,0); + if(result > 0) + { + // if yes, tell the audio subsystem to do something about it + if(FD_ISSET(audiofd, &wfds)) + as->handleIO(AudioSubSystem::ioWrite); + if(FD_ISSET(audiofd, &efds)) + as->handleIO(AudioSubSystem::ioExcept); + } + else + { + cout << "select returned " << result << endl; + } + } +} + +/* + * needMore is a callback by the audio subsystem, which is made as soon as + * the internal buffers need a refill + */ +void SinTest::needMore() +{ + float left[100], right[100]; + unsigned char out[400]; + + // generate two waves, one 440 Hz (left), one 880Hz (right) + for(int i=0;i<100;i++) + { + left[i] = sin((float)i*6.28/100.0); + right[i] = sin((float)i*6.28/50.0); + } + convert_stereo_2float_i16le(100, left, right, out); + + // write them to the audio subsystem + as->write(out,400); + done += 100; +} + +/* + * This is intended to help testing the audio subsystem with different + * hardware without needing to use the whole server. + * + * Warning: this is *not* an example how to use aRts (there are others), + * but a simple test program for debugging purposes. + */ +int main() +{ + Dispatcher d; // to get startup classes executed + + // the test, generate 88100 samples (2 seconds) of pure sine wave test data + SinTest s; + s.run(88100); + return 0; +} diff --git a/examples/testdhandle.cc b/examples/testdhandle.cc new file mode 100644 index 0000000..a42ad46 --- /dev/null +++ b/examples/testdhandle.cc @@ -0,0 +1,115 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include "gslpp/datahandle.h" +#include "soundserver.h" +#include "stdsynthmodule.h" +#include "artsflow.h" +#include "connect.h" + +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> +#include <iostream> +#include <math.h> + +using namespace std; +using namespace Arts; + +int main(int argc, char **argv) +{ + if(argc<2) + { + cerr << "USAGE: " << argv[0] << " [filename]\n"; + exit(1); + } + + Dispatcher dispatcher; + SoundServer server = Reference("global:Arts_SoundServer"); + + cout << "opening '" << argv[1] << "' locally...\n"; + + GSL::WaveFileInfo info(argv[1]); + if(info.error()) + { + cerr << "GSL error " << info.error() << + " (" << gsl_strerror(info.error()) << ") while loading info.\n"; + exit(1); + } + cout << "file contains " << info.waveCount() << " waves:\n"; + for(int i=0; i<info.waveCount(); i++) + { + GSL::WaveDescription desc= info.waveDescription(i); + cout << " wave " << i << ": " << desc.name() << + " (" << desc.channelCount() << " channels):\n"; + for(int ci=0; ci<desc.chunkCount(); ci++) + { + GSL::WaveChunkDescription cDesc= desc.chunkDescription(ci); + cout << " chunk " << ci << ": " + << cDesc.oscillatorFrequency() + << "hz, recorded with " << cDesc.mixerFrequency() + << ", loop type: " << cDesc.loopType() << "\n"; + } + } + + cout << "opening '" << argv[1] << "' in soundserver for playback...\n"; + + if(server.isNull()) + { + cerr << "Can't connect to sound server" << endl; + return 1; + } + + WaveDataHandle handle = DynamicCast(server.createObject("Arts::WaveDataHandle")); + if(!handle.load(argv[1])) + { + cerr << "server can't load wave file '" << argv[1] << "'" << endl; + return 1; + } + + printf("channels = %d\n", handle.channelCount()); + //printf("samplingrate = %f\n", handle.samplingRate()); + printf("length[values] = %ld\n", handle.valueCount()); + + DataHandlePlay phandle = handle.createPlayer(); + + /*DataHandlePlay phandle = + DynamicCast(server.createObject("Arts::DataHandlePlay")); + + phandle.handle(handle); + // .createReversed() .createTranslated(0,100000,40000) + */ + + Synth_BUS_UPLINK bu = + DynamicCast(server.createObject("Arts::Synth_BUS_UPLINK")); + connect(phandle,"outvalue",bu,"left"); + connect(phandle,"outvalue",bu,"right"); + bu.busname("out_soundcard"); + phandle.start(); + bu.start(); + + while(!phandle.finished()) + sleep(1); +} diff --git a/examples/testthreads.cc b/examples/testthreads.cc new file mode 100644 index 0000000..0155f90 --- /dev/null +++ b/examples/testthreads.cc @@ -0,0 +1,64 @@ +/* + + Copyright (C) 2001-2002 Stefan Westerfeld + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + +*/ + +#include <stdio.h> +#include <string> +#include <unistd.h> +#include "dispatcher.h" +#include "thread.h" + +using namespace Arts; +using namespace std; + +class Counter : public Arts::Thread +{ + string name; +public: + Counter(const string& name) :name(name) {} + + void run() { + for(int i = 0;i < 10;i++) + { + printf("[%s] %d\n",name.c_str(),i+1); + sleep(1); + } + printf("[%s] terminating.\n", static_cast<Counter *> + (SystemThreads::the()->getCurrentThread())->name.c_str()); + } +}; + +int main() +{ + Dispatcher dispatcher; + Counter c1("counter1"), c2("counter2"); + + if(SystemThreads::supported()) + printf("We have a system threads (counters should count parallel).\n"); + else + printf("No system threads (counters will not count parallel).\n"); + + c1.start(); + c2.start(); + c1.waitDone(); + c2.waitDone(); + return 0; +} diff --git a/examples/x11commtest.cc b/examples/x11commtest.cc new file mode 100644 index 0000000..fa793b4 --- /dev/null +++ b/examples/x11commtest.cc @@ -0,0 +1,76 @@ + /* + + Copyright (C) 2000 Stefan Westerfeld + Modified by Nicolas Brodu, [email protected] + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Permission is also granted to link this program with the Qt + library, treating Qt like a library that normally accompanies the + operating system kernel, whether or not that is in fact the case. + + */ + +#include <iostream> +#include "common.h" + +using namespace std; +using namespace Arts; + +/* + * Test program for X11GlobalComm (communication over X11 root window + * properties), and since X11GlobalComm is an dynamically extension, + * extension loading is tested as well. + */ +int main(int argc, char **argv) +{ + Dispatcher dispatcher; + GlobalComm gcomm( SubClass("Arts::X11GlobalComm") ); + + if (gcomm.isNull()) { + cerr << "Cannot create a X11GlobalComm object" << endl; + return 2; + } + + if(argc == 4) + { + if(string(argv[1]) == "put") + { + gcomm.put(argv[2],argv[3]); + return 0; + } + } + if(argc == 3) + { + if(string(argv[1]) == "get") + { + cout << gcomm.get(argv[2]) << endl; + return 0; + } + if(string(argv[1]) == "erase") + { + gcomm.erase(argv[2]); + return 0; + } + } + + cerr << "This is a test for the X11GlobalComm class. Use" << endl << endl + << " " << argv[0] << " put <variable> <value>" << endl + << " " << argv[0] << " get <variable>" << endl + << " " << argv[0] << " erase <variable>" << endl << endl + << "to test the communication via X11 RootWindow properties." << endl; + return 1; +} |