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.InputStreamReader;
24 import java.nio.ByteBuffer;
25 import java.util.EnumSet;
26
27 import net.sf.jack4j.AbstractJackClient;
28 import net.sf.jack4j.JackLocalPort;
29 import net.sf.jack4j.JackPortFlag;
30 import net.sf.jack4j.util.file.wav.WavFileWriter;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class WavRecorder extends AbstractJackClient {
49
50 private JackLocalPort portL;
51 private JackLocalPort portR;
52 private WavFileWriter wavFileWriter;
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @Override
67 public int process(int bufferSize) throws Exception {
68
69 ByteBuffer[] buffers = new ByteBuffer[] { portL.getByteBuffer(bufferSize), portR.getByteBuffer(bufferSize) };
70
71
72
73 for (ByteBuffer buffer : buffers) {
74 buffer.clear();
75 }
76
77
78
79 if (wavFileWriter != null) {
80
81 synchronized (wavFileWriter) {
82
83
84 wavFileWriter.writeSamples(buffers, bufferSize);
85 }
86 }
87
88
89 return 0;
90 }
91
92
93
94
95
96
97
98 public WavRecorder(String clientName) throws Exception {
99
100 super(clientName, false, true, null);
101
102
103
104
105 setDefaultThreadInitCallback();
106
107
108 setDefaultProcessCallback();
109
110
111 portL = addAudioPort("left", EnumSet.of(JackPortFlag.IS_INPUT));
112 portR = addAudioPort("right", EnumSet.of(JackPortFlag.IS_INPUT));
113
114
115 }
116
117
118
119
120 public void run() throws Exception {
121
122 activate();
123
124 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
125
126 System.out.println("CONNECT SOMETHING TO THE INPUT PORTS, otherwise you'll be recording silence");
127 System.out.println("(you can use jack_connect or qjackctl to do this)");
128
129 while (true) {
130 System.out.print("Enter name of the file to write to, or empty line to quit:");
131 String wavFileName = in.readLine();
132
133 if ("".equals(wavFileName)) {
134 break;
135 }
136
137 System.out.print("Enter number of channels: ");
138 String channelCountStr = in.readLine();
139 int channelCount;
140 try {
141 channelCount = Integer.parseInt(channelCountStr);
142 } catch (NumberFormatException e) {
143 System.err.println("Not a number");
144 continue;
145 }
146 if (channelCount < 1) {
147 System.err.println("Wrong number of channels");
148 continue;
149 }
150
151 System.out.print("Enter number of bits per sample (1-32): ");
152 String bitsPerSampleStr = in.readLine();
153 int bitsPerSample;
154 try {
155 bitsPerSample = Integer.parseInt(bitsPerSampleStr);
156 } catch (NumberFormatException e) {
157 System.err.println("Not a number");
158 continue;
159 }
160 if ((bitsPerSample < 1) || (bitsPerSample > 32)) {
161 System.err.println("Wrong number of bits");
162 continue;
163 }
164
165 try {
166 wavFileWriter = new WavFileWriter(wavFileName, bitsPerSample, getSampleRate(), channelCount);
167 } catch (Exception e) {
168 e.printStackTrace();
169 continue;
170 }
171
172 System.out.print("Recording, press enter to stop... ");
173 in.readLine();
174 try {
175
176 synchronized (wavFileWriter) {
177 wavFileWriter.close();
178 wavFileWriter = null;
179 }
180 } catch (Exception e) {
181 e.printStackTrace();
182 continue;
183 }
184 }
185
186 close();
187 }
188
189 public static void main(String[] args) throws Exception {
190 if (args.length != 1) {
191 System.err.println("Expecting parameter: clientName");
192 return;
193 }
194 WavRecorder wavRecorder = new WavRecorder(args[0]);
195
196 wavRecorder.run();
197 }
198 }