Added a parameter to semaphore constructor to avoid ambiguity
[pwlib.git] / tools / plugintest / main.cxx
blobaf487de828406868d78e4d47bbd26837e0cd76ef
1 /*
2 * main.cxx
4 * PWLib application source file for PluginTest
6 * Main program entry point.
8 * Copyright 2003 Equivalence
10 * $Log$
11 * Revision 1.3 2003/11/12 07:00:09 csoutheren
12 * Changed make compile under Windows
14 * Revision 1.2 2003/11/12 06:35:12 csoutheren
15 * Initial main version
17 * Revision 1.1.2.6 2003/10/20 21:15:33 dereksmithies
18 * Tidy up text output. Fix calls to Usage() function.
20 * Revision 1.1.2.5 2003/10/20 03:22:46 dereksmithies
21 * Add checks on validity of function returned.
23 * Revision 1.1.2.4 2003/10/13 02:46:02 dereksmithies
24 * Now generates sound through dynamically loaded OSS sound channel.
26 * Revision 1.1.2.3 2003/10/12 21:22:12 dereksmithies
27 * Add ability to play sample sound out PSoundChannel - illustrating operation of plugins.
29 * Revision 1.1.2.2 2003/10/08 03:55:54 dereksmithies
30 * Add lots of debug statements, fix option parsing, improve Usage() function.
32 * Revision 1.1.2.1 2003/10/07 01:52:39 csoutheren
33 * Test program for plugins
35 * Revision 1.3 2003/04/22 23:25:13 craigs
36 * Changed help message for SRV records
38 * Revision 1.2 2003/04/15 08:15:16 craigs
39 * Added single string form of GetSRVRecords
41 * Revision 1.1 2003/04/15 04:12:38 craigs
42 * Initial version
46 #include <ptlib.h>
47 #include <ptlib/pluginmgr.h>
48 #include <ptlib/sound.h>
49 #include "main.h"
51 #include <math.h>
53 #ifndef M_PI
54 #define M_PI 3.1415926
55 #endif
57 PCREATE_PROCESS(PluginTest);
59 #define SAMPLES 64000
61 PluginTest::PluginTest()
62 : PProcess("Equivalence", "PluginTest", 1, 0, AlphaCode, 1)
66 void Usage()
68 PError << "usage: plugintest dir\n \n"
69 << "-l List ALL plugins regardless of type\n"
70 << "-s Show the list of loaded PSoundChannel drivers\n"
71 << "-d dir Set the directory from which plugins are loaded\n"
72 << "-x Attempt to load the OSS sound plugin\n"
73 << "-t (more t's for more detail) logging on\n"
74 << "-o output file for logging \n"
75 << "-p play a beep beep beep sound, and test created PSoundChannel\n"
76 << " Requires that you have specified -x also\n"
77 << "-h print this help\n";
81 void PluginTest::Main()
83 PArgList & args = GetArguments();
85 args.Parse(
86 "t-trace."
87 "o-output:"
88 "h-help."
89 "l-list."
90 "s-service:"
91 "a-audio:"
92 "d-directory:"
94 //"x-xamineOSS."
95 //"s-soundPlugins."
96 //"p-play."
99 PTrace::Initialise(args.GetOptionCount('t'),
100 args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL,
101 PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
103 if (args.HasOption('d')) {
104 PPluginManager & pluginMgr = PPluginManager::GetPluginManager();
105 pluginMgr.LoadPluginDirectory(args.GetOptionString('d'));
108 if (args.HasOption('h')) {
109 Usage();
110 return;
113 if (args.HasOption('l')) {
114 cout << "List available plugin types" << endl;
115 PPluginManager & pluginMgr = PPluginManager::GetPluginManager();
116 PStringList plugins = pluginMgr.GetPluginTypes();
117 if (plugins.GetSize() == 0)
118 cout << "No plugins loaded" << endl;
119 else {
120 cout << plugins.GetSize() << " plugin types available:" << endl;
121 for (int i = 0; i < plugins.GetSize(); i++) {
122 cout << " " << plugins[i] << " : ";
123 PStringList services = pluginMgr.GetPluginsProviding(plugins[i]);
124 if (services.GetSize() == 0)
125 cout << "None available" << endl;
126 else
127 cout << setfill(',') << services << setfill(' ') << endl;
130 return;
133 if (args.HasOption('s')) {
134 cout << "Available " << args.GetOptionString('s') << " :" <<endl;
135 cout << "Sound plugin names = " << setfill(',') << PSoundChannel::GetDriverNames() << setfill(' ') << endl;
137 //cout << "Default device names = " << setfill(',') << PSoundChannel::GetDeviceNames(PSoundChannel::Player) << setfill(' ') << endl;
138 //PSoundChannel * snd = new PSoundChannel();
139 //cout << "PSoundChannel has a name of \"" << snd->GetClass() << "\"" << endl
140 // << endl;
144 if (args.HasOption('a')) {
145 PString service = args.GetOptionString('a');
146 PString device;
147 if (args.GetCount() > 0)
148 device = args[0];
149 else if (service != "default") {
150 PStringList deviceList = PSoundChannel::GetDeviceNames(service, PSoundChannel::Player);
151 if (deviceList.GetSize() == 0) {
152 cout << "No devices for sound service " << service << endl;
153 return;
155 device = deviceList[0];
158 cout << "Using sound service " << service << " with device " << device << endl;
160 PSoundChannel * snd;
161 if (service == "default") {
162 snd = new PSoundChannel();
163 device = PSoundChannel::GetDefaultDevice(PSoundChannel::Player);
165 else {
166 snd = PSoundChannel::CreateChannel(service);
167 if (snd == NULL) {
168 cout << "Failed to create sound service " << service << " with device " << device << endl;
169 return;
173 cout << "Opening sound service " << service << " with device " << device << endl;
175 if (!snd->Open(device, PSoundChannel::Player)) {
176 cout << "Failed to open sound service " << service << " with device " << device << endl;
177 return;
180 if (!snd->IsOpen()) {
181 cout << "Sound device " << device << " not open" << endl;
182 return;
185 if (!snd->SetBuffers(SAMPLES, 2)) {
186 cout << "Failed to set samples to " << SAMPLES << " and 2 buffers. End program now." << endl;
187 return;
190 snd->SetVolume(100);
192 PWORDArray audio(SAMPLES);
193 int i, pointsPerCycle = 8;
194 int volume = 80;
195 double angle;
197 for (i = 0; i < SAMPLES; i++) {
198 angle = M_PI * 2 * (double)(i % pointsPerCycle)/pointsPerCycle;
199 if ((i % 4000) < 3000)
200 audio[i] = (unsigned short) ((16384 * cos(angle) * volume)/100);
201 else
202 audio[i] = 0;
205 if (!snd->Write((unsigned char *)audio.GetPointer(), SAMPLES * 2)) {
206 cout << "Failed to write " << SAMPLES/8000 << " seconds of beep beep. End program now." << endl;
207 return;
210 snd->WaitForPlayCompletion();
214 // End of File ///////////////////////////////////////////////////////////////