BaseAudioApplication.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "BaseAudioApplication.hxx"
00023 #include "AudioManager.hxx"
00024 #include <pthread.h>
00025 #include <cstdio>
00026 #include <iostream>
00027 #ifdef WIN32
00028 #include "CLAM_windows.h"
00029 #undef GetClassName
00030 #else
00031 #include <unistd.h>
00032 #endif
00033 #include "AudioManager.hxx"
00034 
00035 namespace CLAM {
00036 
00037         typedef void *(*startfn) (void *);
00038         typedef void (*cleanfn) (void *);
00039 
00040         BaseAudioApplication::BaseAudioApplication()
00041         :Application()
00042         {
00043                 cancel = false;
00044         }
00045 
00046         void BaseAudioApplication::Start(void)
00047         {
00048                 cancel = false;
00049                 pthread_create(&thread,NULL,(startfn)SAudioThread,this);
00050         }
00051         
00052         void BaseAudioApplication::Stop(void)
00053         {
00054                 pthread_cleanup_push((cleanfn)SAudioThreadCleanup,this);
00055 
00056                 cancel = true;
00057                 pthread_join(thread,NULL);
00058                                 
00059                 pthread_cleanup_pop(1);
00060                 cancel = false;
00061         }
00062 
00063         void BaseAudioApplication::Run(int argc,char** argv)
00064         {
00065                 Start();
00066                 UserMain();
00067                 Stop();
00068         }
00069 
00070         void BaseAudioApplication::UserMain(void)
00071         {
00072                 printf("Press enter to terminate\n");
00073                 getchar();
00074         }
00075 
00076         void* BaseAudioApplication::SAudioThread(BaseAudioApplication *pThis)
00077         {
00078         #ifdef WIN32
00079                 BOOL res;
00080                 DWORD err;
00081                 
00082                 // Maximum priority for the process: this has the upper hand over
00083                 // everything else, including most essential OS services
00084                 //res = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
00085                 // Toned down priority: this enables cache flush (at least)
00086                 //res = SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS );
00087 
00088                 // With the new AudioIO approach (POLL instead of NOTIFY), it seems that
00089                 // we should NORMAL priority. If not, the system gets very slow.
00090                 // 
00091                 // the other solution (now used) is to have a Sleep in the polling loop
00092                 res = SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS );
00093 
00094                 err = GetLastError();
00095                 
00096                 // Maximum priority for the thread under Win32
00097                 //res = SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
00098                 //Toned down thread priority
00099                 //res = SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST );
00100                 // With the new AudioIO approach (POLL instead of NOTIFY), it seems that
00101                 // we should should NORMAL priority. If not, the system gets very slow
00102                 res = SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST );
00103                 err = GetLastError();
00104         #else
00105                 struct sched_param sched_param;
00106                 int policy;
00107 
00108                 if (pthread_getschedparam(pthread_self(), &policy, &sched_param) < 0) {
00109                         printf("Scheduler getparam failed...\n");
00110                 }
00111                 sched_param.sched_priority = sched_get_priority_max(SCHED_RR)-1;
00112                 if (!pthread_setschedparam(pthread_self(), SCHED_RR, &sched_param)) {
00113                         printf("Scheduler set to Round Robin with priority %i...\n", sched_param.sched_priority);
00114                 }
00115 
00116         #endif
00117         
00118                 pThis->AudioMain();
00119         
00120                 return NULL;
00121         }
00122 
00123         void BaseAudioApplication::SAudioThreadCleanup(BaseAudioApplication *pThis)
00124         {
00125         }
00126 
00127 } // namespace CLAM
00128 
Generated by  doxygen 1.6.3