More valgrinding and fixing a serious mem leak - Alex
[terminatorX.git] / src / tX_prelis.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2003  Alexander K├Ânig
4  
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9  
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19     File: tX_prelis.cc
20  
21     Description: This takes care of the "pre-listening" of audiofiles
22                  in the load dialog.
23
24 */    
25
26 extern "C" {
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 };
30
31 #include "tX_global.h"
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <signal.h>
37 #include <stdlib.h>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 pid_t player_pid=0;
44
45 void prelis_stop()
46 {
47         int status;
48         
49         if (player_pid)
50         {
51                 kill(player_pid, SIGTERM);
52                 waitpid(player_pid, &status, 0);
53                 player_pid=0;
54                 usleep(200);
55         }       
56 }
57
58 void prelis_start(char *name) {
59         char *ext;
60         pid_t temp;
61         int usempg123=0;
62         int useogg123=0;
63         int res;
64         char dev[PATH_MAX];
65         char ogg123_dev[PATH_MAX]="dev:";
66         char nm[PATH_MAX];
67         
68         if (!globals.prelis) return;
69         
70         if (player_pid) prelis_stop();  
71         
72         ext=strrchr(name, (int) '.');
73         if (ext)
74         {
75                 if (strlen(ext)>3)
76                 {
77                         ext++;
78 #ifdef USE_OGG123_INPUT
79                         if (!strncasecmp("ogg", ext, 2))
80                         useogg123=1;
81                         else
82 #endif
83 #ifdef USE_MPG123_INPUT                 
84                         if (!strncasecmp("mp", ext, 2))                 
85                         usempg123=1;
86                         else
87 #endif
88 #ifdef USE_SOX_INPUT                    
89                         usempg123=0;
90 #else
91                         return;
92 #endif                                          
93                 }
94         }
95         
96         temp=fork();
97
98         if (temp==-1) /* error */
99         {
100                 return;
101         }
102         else if (temp==0) /* CHILD */
103         {       
104                 strcpy(dev, globals.oss_device);
105                 strcat(ogg123_dev, dev);
106                 strcpy(nm, name);
107 #ifdef USE_OGG123_INPUT
108                 if (useogg123)
109                         res=execlp("ogg123", "ogg123", "-q", "-d", "oss", "-o", ogg123_dev,
110                         nm, NULL);
111                 else
112 #endif
113 #ifdef USE_MPG123_INPUT
114                 if (usempg123)
115                         res=execlp("mpg123", "mpg123", "-q", "-o","oss","-a", dev, nm, NULL);
116                 else
117 #endif
118 #ifdef USE_SOX_INPUT            
119                         res=execlp("sox", "sox", nm, "-t", "ossdsp", "-w", "-s", dev, NULL);
120 #else
121                         exit(0);
122 #endif                  
123                         
124                 perror("Pre-Listen Error:");
125                 exit(0);
126         }                               
127         else /* PARENT */
128         {
129                 player_pid=temp;
130         }
131 }