File: Sample.java

package info (click to toggle)
jesd 0.0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 328 kB
  • sloc: java: 1,891; makefile: 10
file content (140 lines) | stat: -rw-r--r-- 3,668 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* JEsd
 * Copyright (C) 1999 JCraft Inc.
 *  
 * Written by: 1999 ymnk
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import com.jcraft.jesd.*;
import java.io.*;

public class Sample{

  Sample(String[] arg) throws java.io.IOException{
    int rate=JEsd.ESD_DEFAULT_RATE;
    int bits=JEsd.ESD_BITS16, channels=JEsd.ESD_STEREO;
    int mode=JEsd.ESD_STREAM, func=JEsd.ESD_PLAY;

    int sample_id=0, confirm_id=0, reget_sample_id=0;
    int cache_mode=0;

    String file=null;
    String host="localhost";
    int i=0;
    for(;i<arg.length;i++){
      if(arg[i].equals("-h")){
	System.out.println("usage: Sample [-s server] [-d [-b] [-m] [-r freq]] file");
	System.exit(0);
      }
      if(arg[i].equals("-s")){
	i++;
	host=arg[i];
	continue;
      }
      if(arg[i].equals("-b")){
	bits=JEsd.ESD_BITS8;
	continue;
      }
      if(arg[i].equals("-m")){
	channels=JEsd.ESD_MONO;
	continue;
      }
      if(arg[i].equals("-d")){
	cache_mode=1;
	continue;
      }
      /*
      if(arg[i].equals("-e")){
	cache_mode=2;
	continue;
      }
      */
      if(arg[i].equals("-r")){
	i++;
	try{rate=Integer.valueOf(arg[i]).intValue();}
	catch(Exception e){}
	continue;
      }
      break;
    }
    file=arg[i];

    JEsd jesd=null;

    if(cache_mode==1){
      FileInputStream fis=new FileInputStream(file);
      int format=bits|channels|mode|func;

      try{ jesd=new JEsd(host);}
      catch(Exception e){
	System.err.println(e);
	System.exit(-1);
      }

      sample_id=jesd.sample_cache(format, rate, 
				  (int)(new File(file).length()), file);
      System.out.println("sample id is "+sample_id);

      byte[] buf=new byte[JEsd.ESD_BUF_SIZE];
      int length;
      int total=0;
      while((length=fis.read(buf,0,JEsd.ESD_BUF_SIZE))>0){
	if(jesd.write(buf, 0, length)<0) return;
	else total+=length;
      }

      confirm_id=jesd.confirm_sample_cache();
      if(confirm_id!=sample_id){
	System.out.println("error while caching sample <"+sample_id+
			   ">: confirm returned "+confirm_id);
	System.exit(-1);
      }
      fis.close();
      System.out.println("sample <"+sample_id+"> upladed, "+total+" bytes");
    }
    else if (cache_mode==0) {
      try{jesd=new JEsd(host);}
      catch(Exception e){
	System.err.println(e);
	System.exit(-1);
      }
      sample_id=jesd.file_cache("sample", file);
      if(sample_id<0){
	System.out.println("error while caching sample <"+sample_id+
			   ">: confirm value != sample_id");
	System.exit(-1);
      }
      System.out.println("sample <"+sample_id+"> upladed: "+file);
    }
    else if (cache_mode==2){
    }
    
    while(true){
      System.out.println("press 'q' <enter> to quit, <enter> to trigger");
      int key=System.in.read();
      if('q'==(byte)key) break;
      jesd.sample_play(sample_id);
    }
    jesd.sample_free(sample_id);
    System.out.println("closing down\n");
    jesd.close();
  }

  public static void main(String[] arg){
    try{ new Sample(arg); }
    catch(Exception e){}
  }
}