It's 2020...
[terminatorX.git] / src / tX_prelis.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999-2020  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, see <http://www.gnu.org/licenses/>.
17  
18     File: tX_prelis.cc
19  
20     Description: This takes care of the "pre-listening" of audiofiles
21                  in the load dialog.
22
23 */    
24
25 extern "C" {
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 };
29
30 #include "tX_global.h"
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include <stdlib.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 pid_t player_pid=0;
43
44 void prelis_stop()
45 {
46         int status;
47         
48         if (player_pid)
49         {
50                 kill(player_pid, SIGTERM);
51                 waitpid(player_pid, &status, 0);
52                 player_pid=0;
53                 usleep(200);
54         }       
55 }
56
57 void prelis_start(char *name) {
58         char *ext;
59         pid_t temp;
60 #ifdef USE_MPG123_INPUT 
61         int usempg123=0;
62 #endif
63 #ifdef USE_OGG123_INPUT
64         int useogg123=0;
65 #endif
66         char dev[PATH_MAX];
67         char ogg123_dev[PATH_MAX]="dev:";
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                         if (!strncasecmp("ogg", ext, 2))
82                         useogg123=1;
83                         else
84 #endif
85 #ifdef USE_MPG123_INPUT                 
86                         if (!strncasecmp("mp", ext, 2))                 
87                         usempg123=1;
88                         else
89 #endif
90 #if defined(USE_SOX_INPUT) && defined(USE_MPG123_INPUT)
91                         usempg123=0;
92 #else
93                         return;
94 #endif                                          
95                 }
96         }
97         
98         temp=fork();
99
100         if (temp==-1) /* error */
101         {
102                 return;
103         }
104         else if (temp==0) /* CHILD */
105         {       
106                 strcpy(dev, globals.oss_device);
107                 strcat(ogg123_dev, dev);
108                 strcpy(nm, name);
109 #ifdef USE_OGG123_INPUT
110                 if (useogg123)
111                         execlp("ogg123", "ogg123", "-q", "-d", "oss", "-o", ogg123_dev, nm, NULL);
112                 else
113 #endif
114 #ifdef USE_MPG123_INPUT
115                 if (usempg123)
116                         execlp("mpg123", "mpg123", "-q", "-o","oss","-a", dev, nm, NULL);
117                 else
118 #endif
119 #ifdef USE_SOX_INPUT            
120                         execlp("play", "play", "-q", nm, NULL);
121 #else
122                         exit(0);
123 #endif                  
124                         
125                 perror("Pre-Listen Error:");
126                 exit(0);
127         }                               
128         else /* PARENT */
129         {
130                 player_pid=temp;
131         }
132 }