be727a24b67ee4647f4c9e6dca09dcb7679c2896
[terminatorX.git] / src / tX_prelis.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2002  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 {
60         char buffer[PATH_MAX*2];
61         char *ext;
62         pid_t temp;
63         int usempg123=0;
64         int useogg123=0;
65         int res;
66         char dev[PATH_MAX];
67         char ogg123_dev[PATH_MAX];
68         char nm[PATH_MAX];
69         
70         if (!globals.prelis) return;
71         
72         if (player_pid) prelis_stop();  
73         
74         ext=strrchr(name, (int) '.');
75         if (ext)
76         {
77                 if (strlen(ext)>3)
78                 {
79                         ext++;
80 #ifdef USE_OGG123_INPUT
81                         strcpy(ogg123_dev, "dev:");
82                         if (!strncasecmp("ogg", ext, 2))
83                         useogg123=1;
84                         else
85 #endif
86 #ifdef USE_MPG123_INPUT                 
87                         if (!strncasecmp("mp", ext, 2))                 
88                         usempg123=1;
89                         else
90 #endif
91 #ifdef USE_SOX_INPUT                    
92                         usempg123=0;
93 #else
94                         return;
95 #endif                                          
96                 }
97         }
98         
99         temp=fork();
100
101         if (temp==-1) /* error */
102         {
103                 return;
104         }
105         else if (temp==0) /* CHILD */
106         {       
107                 strcpy(dev, globals.audio_device);
108                 strcat(ogg123_dev, dev);
109                 strcpy(nm, name);
110 #ifdef USE_OGG123_INPUT
111                 if (useogg123)
112                         res=execlp("ogg123", "ogg123", "-q", "-d", "oss", "-o", ogg123_dev,
113                         nm, NULL);
114                 else
115 #endif
116 #ifdef USE_MPG123_INPUT
117                 if (usempg123)
118                         res=execlp("mpg123", "mpg123", "-q", "-o","oss","-a", dev, nm, NULL);
119                 else
120 #endif
121 #ifdef USE_SOX_INPUT            
122                         res=execlp("sox", "sox", nm, "-t", "ossdsp", "-w", "-s", dev, NULL);
123 #else
124                         exit(0);
125 #endif                  
126                         
127                 perror("Pre-Listen Error:");
128                 exit(0);
129         }                               
130         else /* PARENT */
131         {
132                 player_pid=temp;
133         }
134 }