1 package net.sf.jack4j.examples;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.InputStreamReader;
26 import java.nio.ByteBuffer;
27 import java.util.EnumSet;
28
29 import net.sf.jack4j.AbstractJackClient;
30 import net.sf.jack4j.JackLocalPort;
31 import net.sf.jack4j.JackPortFlag;
32 import net.sf.jack4j.util.file.wav.WavFileReader;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class WavPlayer extends AbstractJackClient {
51
52 private JackLocalPort portL;
53 private JackLocalPort portR;
54 private WavFileReader wavFileReader;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 @Override
74 public int process(int bufferSize) throws Exception {
75
76 ByteBuffer[] buffers = new ByteBuffer[] { portL.getByteBuffer(bufferSize), portR.getByteBuffer(bufferSize) };
77
78
79 for (ByteBuffer buffer : buffers) {
80 buffer.clear();
81 }
82
83 if (wavFileReader != null) {
84
85
86 int samplesRead = wavFileReader.readSamples(buffers, bufferSize);
87
88 if (samplesRead < bufferSize) {
89
90
91 wavFileReader = null;
92 }
93 }
94
95
96
97 for (ByteBuffer buffer : buffers) {
98
99 int samplePosition = buffer.position() / (Float.SIZE / 8);
100
101 for (int i = samplePosition; i < bufferSize; i++) {
102
103 buffer.putFloat(0.0f);
104 }
105 }
106
107
108 return 0;
109 }
110
111
112
113
114
115
116
117 public WavPlayer(String clientName) throws Exception {
118
119 super(clientName, false, true, null);
120
121
122
123
124 setDefaultThreadInitCallback();
125
126
127 setDefaultProcessCallback();
128
129
130 portL = addAudioPort("left", EnumSet.of(JackPortFlag.IS_OUTPUT));
131 portR = addAudioPort("right", EnumSet.of(JackPortFlag.IS_OUTPUT));
132
133
134 }
135
136
137
138
139 public void run() throws Exception {
140
141 activate();
142
143 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
144
145 System.out.println("CONNECT OUTPUT PORTS to audio card, or you'll not hear any sound");
146 System.out.println("(you can use jack_connect or qjackctl to do this)");
147
148 System.out.println("Enter WAV file name to play, or empty line to quit");
149 while (true) {
150 System.out.print("file name> ");
151 String wavFileName = in.readLine();
152
153 if ("".equals(wavFileName)) {
154 break;
155 } else {
156 File wavFile = new File(wavFileName);
157 if (!wavFile.exists()) {
158 System.err.println("File not found");
159 continue;
160 }
161
162 try {
163 wavFileReader = new WavFileReader((new FileInputStream(wavFile)).getChannel());
164 } catch (Exception e) {
165 e.printStackTrace();
166 continue;
167 }
168
169 System.out.println("WAV file: " + wavFileName);
170 System.out.println("Avg bytes per second: " + wavFileReader.getAvgBytesPerSecond());
171 System.out.println("Bits per sample: " + wavFileReader.getBitsPerSample());
172 System.out.println("Block align: " + wavFileReader.getBlockAlign());
173 System.out.println("Channel count: " + wavFileReader.getChannelCount());
174 System.out.println("Samples per second: " + wavFileReader.getSamplesPerSecond());
175 }
176 }
177
178 close();
179 }
180
181 public static void main(String[] args) throws Exception {
182 if (args.length != 1) {
183 System.err.println("Expecting parameter: clientName");
184 return;
185 }
186 WavPlayer wavPlayer = new WavPlayer(args[0]);
187
188 wavPlayer.run();
189 }
190 }