Applied the ogg123 patch and some minor fixes. Alex
[terminatorX.git] / src / tX_prelis.cc
1 /*
2     terminatorX - realtime audio scratching software
3     Copyright (C) 1999, 2000  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 #define PL_SOX_STR "sox \"%s\" -t ossdsp -w -s \"%s\""
44 #define PL_MPG123_STR "mpg123 -a \"%s\" \"%s\""
45 #define PL_OGG123_STR "ogg123 -d oss \"%s\" -o \"dsp:%s\""
46
47 //FILE *player_handle=NULL;
48 pid_t player_pid=0;
49
50 void prelis_stop()
51 {
52 /*      if  (player_handle) pclose(player_handle);
53         player_handle=NULL;*/
54         int status;
55         
56 //      printf("killing %i\n", player_pid);
57         if (player_pid)
58         {
59                 kill(player_pid, SIGTERM);
60                 waitpid(player_pid, &status, 0);
61                 player_pid=0;
62                 usleep(200);
63         }       
64 }
65
66 void prelis_start(char *name)
67 {
68         char buffer[PATH_MAX*2];
69         char *ext;
70         pid_t temp;
71         int usempg123=0;
72         int useogg123=0;
73         int res;
74         char dev[PATH_MAX];
75         char ogg123_dev[PATH_MAX];
76         char nm[PATH_MAX];
77         
78         if (!globals.prelis) return;
79         
80         if (player_pid) prelis_stop();  
81         
82         ext=strrchr(name, (int) '.');
83         if (ext)
84         {
85                 if (strlen(ext)>3)
86                 {
87                         ext++;
88 #ifdef USE_OGG123_INPUT
89                         strcpy(ogg123_dev, "dev:");
90                         if (!strncasecmp("ogg", ext, 2))
91 //          sprintf(buffer, PL_OGG123_STR, name, globals.audio_device);
92                         useogg123=1;
93                         else
94 #endif
95 #ifdef USE_MPG123_INPUT                 
96                         if (!strncasecmp("mp", ext, 2))                 
97 //                      sprintf(buffer, PL_MPG123_STR, name, globals.audio_device);
98                         usempg123=1;
99                         else
100 #endif
101 #ifdef USE_SOX_INPUT                    
102 //                      sprintf(buffer, PL_SOX_STR, name, globals.audio_device);
103                         usempg123=0;
104 #else
105                         return;
106 #endif                                          
107                 }
108         }
109         
110         temp=fork();
111
112         if (temp==-1) /* error */
113         {
114                 return;
115         }
116         else if (temp==0) /* CHILD */
117         {       
118                 strcpy(dev, globals.audio_device);
119                 strcat(ogg123_dev, dev);
120                 strcpy(nm, name);
121 #ifdef USE_OGG123_INPUT
122                 if (useogg123)
123                         res=execlp("ogg123", "ogg123", "-q", "-d", "oss", "-o", ogg123_dev,
124                         nm, NULL);
125                 else
126 #endif
127 #ifdef USE_MPG123_INPUT
128                 if (usempg123)
129                         res=execlp("mpg123", "mpg123", "-q", "-a", dev, nm, NULL);
130                 else
131 #endif
132 #ifdef USE_SOX_INPUT            
133                         res=execlp("sox", "sox", nm, "-t", "ossdsp", "-w", "-s", dev, NULL);
134 #else
135                         exit(0);
136 #endif                  
137                         
138                 perror("Pre-Listen Error:");
139                 exit(0);
140         }                               
141         else /* PARENT */
142         {
143                 player_pid=temp;
144         }
145 }