audiofile support + improved sample rate support + results of a -Wall -Werr
[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         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];
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                         strcpy(ogg123_dev, "dev:");
80                         if (!strncasecmp("ogg", ext, 2))
81                         useogg123=1;
82                         else
83 #endif
84 #ifdef USE_MPG123_INPUT                 
85                         if (!strncasecmp("mp", ext, 2))                 
86                         usempg123=1;
87                         else
88 #endif
89 #ifdef USE_SOX_INPUT                    
90                         usempg123=0;
91 #else
92                         return;
93 #endif                                          
94                 }
95         }
96         
97         temp=fork();
98
99         if (temp==-1) /* error */
100         {
101                 return;
102         }
103         else if (temp==0) /* CHILD */
104         {       
105                 strcpy(dev, globals.audio_device);
106                 strcat(ogg123_dev, dev);
107                 strcpy(nm, name);
108 #ifdef USE_OGG123_INPUT
109                 if (useogg123)
110                         res=execlp("ogg123", "ogg123", "-q", "-d", "oss", "-o", ogg123_dev,
111                         nm, NULL);
112                 else
113 #endif
114 #ifdef USE_MPG123_INPUT
115                 if (usempg123)
116                         res=execlp("mpg123", "mpg123", "-q", "-o","oss","-a", dev, nm, NULL);
117                 else
118 #endif
119 #ifdef USE_SOX_INPUT            
120                         res=execlp("sox", "sox", nm, "-t", "ossdsp", "-w", "-s", dev, 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 }