| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package uk.co.javagear; |
| 22 |
|
|
| 23 |
|
import java.io.File; |
| 24 |
|
import java.io.FileInputStream; |
| 25 |
|
import java.io.FileWriter; |
| 26 |
|
import java.io.IOException; |
| 27 |
|
import javax.sound.sampled.AudioFileFormat; |
| 28 |
|
import javax.sound.sampled.AudioFormat; |
| 29 |
|
import javax.sound.sampled.AudioInputStream; |
| 30 |
|
import javax.sound.sampled.AudioSystem; |
| 31 |
|
import javax.sound.sampled.DataLine; |
| 32 |
|
import javax.sound.sampled.LineUnavailableException; |
| 33 |
|
import javax.sound.sampled.SourceDataLine; |
| 34 |
|
import org.apache.log4j.Logger; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
public final class SN76496 { |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
private ToneGenerator chan0; |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
private ToneGenerator chan1; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
private ToneGenerator chan2; |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
private NoiseGenerator chan3; |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
private ToneGenerator currentGenerator; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private byte [] buffer; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
private FileWriter fileWriter; |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
private AudioFormat audioFormat; |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
private SourceDataLine line; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
private double clockSpeed; |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
private int sampleRate; |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
private int samplesPerFrame; |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
private boolean enabled; |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
private boolean recording; |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
0 |
public SN76496(double c, int s) { |
| 123 |
0 |
clockSpeed = c; |
| 124 |
0 |
sampleRate = s; |
| 125 |
|
|
| 126 |
0 |
chan0 = new ToneGenerator(clockSpeed, sampleRate); |
| 127 |
0 |
chan1 = new ToneGenerator(clockSpeed, sampleRate); |
| 128 |
0 |
chan2 = new ToneGenerator(clockSpeed, sampleRate); |
| 129 |
0 |
chan3 = new NoiseGenerator(clockSpeed, sampleRate, chan2); |
| 130 |
|
|
| 131 |
|
|
| 132 |
0 |
audioFormat = new AudioFormat(sampleRate, 8, 1, true, false); |
| 133 |
0 |
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); |
| 134 |
|
|
| 135 |
0 |
if (!AudioSystem.isLineSupported(info)) { |
| 136 |
0 |
System.out.print("Audio not supported..."); |
| 137 |
|
} else { |
| 138 |
|
try { |
| 139 |
0 |
line = (SourceDataLine) AudioSystem.getLine(info); |
| 140 |
0 |
line.open(audioFormat); |
| 141 |
0 |
line.start(); |
| 142 |
0 |
} catch (LineUnavailableException lue) { |
| 143 |
0 |
Logger.getLogger(this.getClass()).error("Unable to open audio line.", lue); |
| 144 |
0 |
System.out.print("Audio Line Unavailable..."); |
| 145 |
0 |
} |
| 146 |
|
} |
| 147 |
|
|
| 148 |
0 |
recording = false; |
| 149 |
|
|
| 150 |
|
|
| 151 |
0 |
setFPS(60); |
| 152 |
|
|
| 153 |
|
|
| 154 |
0 |
reset(); |
| 155 |
|
|
| 156 |
|
|
| 157 |
0 |
setEnabled(); |
| 158 |
0 |
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
public void reset() { |
| 165 |
0 |
currentGenerator = chan0; |
| 166 |
0 |
chan0.reset(); |
| 167 |
0 |
chan1.reset(); |
| 168 |
0 |
chan2.reset(); |
| 169 |
0 |
chan3.reset(); |
| 170 |
0 |
} |
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
public boolean isEnabled() { |
| 180 |
0 |
return enabled; |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
public void setEnabled() { |
| 188 |
0 |
if (enabled) { |
| 189 |
0 |
stopSound(); |
| 190 |
|
} else { |
| 191 |
0 |
startSound(); |
| 192 |
|
} |
| 193 |
0 |
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
public void setChannelEnabled(int channel) throws IllegalArgumentException { |
| 203 |
0 |
switch (channel) { |
| 204 |
|
case 0: |
| 205 |
0 |
chan0.setEnabled(); |
| 206 |
0 |
break; |
| 207 |
|
case 1: |
| 208 |
0 |
chan1.setEnabled(); |
| 209 |
0 |
break; |
| 210 |
|
case 2: |
| 211 |
0 |
chan2.setEnabled(); |
| 212 |
0 |
break; |
| 213 |
|
case 3: |
| 214 |
0 |
chan3.setEnabled(); |
| 215 |
0 |
break; |
| 216 |
|
default: |
| 217 |
0 |
throw new IllegalArgumentException("Invalid Channel: " + channel); |
| 218 |
|
} |
| 219 |
0 |
} |
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
public void setFPS(int v) { |
| 228 |
0 |
samplesPerFrame = (sampleRate / v); |
| 229 |
0 |
buffer = new byte[samplesPerFrame]; |
| 230 |
|
|
| 231 |
0 |
} |
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
public void write(int value) { |
| 240 |
0 |
if ((!enabled) && (!recording)) { |
| 241 |
0 |
return; |
| 242 |
|
} |
| 243 |
|
|
| 244 |
0 |
if ((value & 0x80) == 0x80) { |
| 245 |
0 |
switch((value >> 4) & 0x07) { |
| 246 |
|
case 0x00: |
| 247 |
0 |
chan0.setFirstByte(value & 0x0F); |
| 248 |
0 |
currentGenerator = chan0; |
| 249 |
0 |
break; |
| 250 |
|
case 0x01: |
| 251 |
0 |
chan0.setVolume(value & 0x0F); |
| 252 |
0 |
currentGenerator = chan0; |
| 253 |
0 |
break; |
| 254 |
|
case 0x02: |
| 255 |
0 |
chan1.setFirstByte(value & 0x0F); |
| 256 |
0 |
currentGenerator = chan1; |
| 257 |
0 |
break; |
| 258 |
|
case 0x03: |
| 259 |
0 |
chan1.setVolume(value & 0x0F); |
| 260 |
0 |
currentGenerator = chan1; |
| 261 |
0 |
break; |
| 262 |
|
case 0x04: |
| 263 |
0 |
chan2.setFirstByte(value & 0x0F); |
| 264 |
0 |
currentGenerator = chan2; |
| 265 |
0 |
break; |
| 266 |
|
case 0x05: |
| 267 |
0 |
chan2.setVolume(value & 0x0F); |
| 268 |
0 |
currentGenerator = chan2; |
| 269 |
0 |
break; |
| 270 |
|
case 0x06: |
| 271 |
0 |
chan3.setFrequency(value & 0x07); |
| 272 |
0 |
currentGenerator = null; |
| 273 |
0 |
break; |
| 274 |
|
case 0x07: |
| 275 |
0 |
chan3.setVolume(value & 0x0F); |
| 276 |
0 |
currentGenerator = null; |
| 277 |
0 |
break; |
| 278 |
|
default: |
| 279 |
0 |
break; |
| 280 |
|
} |
| 281 |
0 |
} else if (currentGenerator != null) { |
| 282 |
0 |
currentGenerator.setFreqSigf(value); |
| 283 |
|
} else { |
| 284 |
0 |
chan3.setFrequency(value & 0x07); |
| 285 |
|
} |
| 286 |
0 |
} |
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
|
| 291 |
|
public synchronized void startSound() { |
| 292 |
0 |
reset(); |
| 293 |
0 |
enabled = true; |
| 294 |
0 |
line.start(); |
| 295 |
0 |
} |
| 296 |
|
|
| 297 |
|
|
| 298 |
|
|
| 299 |
|
|
| 300 |
|
public synchronized void stopSound() { |
| 301 |
0 |
enabled = false; |
| 302 |
0 |
line.stop(); |
| 303 |
0 |
} |
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
|
| 308 |
|
|
| 309 |
|
public void output() { |
| 310 |
0 |
if (!enabled && !recording) { |
| 311 |
0 |
return; |
| 312 |
|
} |
| 313 |
|
|
| 314 |
|
|
| 315 |
0 |
for (int i = 0; i < samplesPerFrame; i++) { |
| 316 |
|
|
| 317 |
0 |
int join = 0; |
| 318 |
0 |
join += chan0.getSample(); |
| 319 |
0 |
join += chan1.getSample(); |
| 320 |
0 |
join += chan2.getSample(); |
| 321 |
0 |
join += chan3.getSample(); |
| 322 |
|
|
| 323 |
|
|
| 324 |
0 |
join <<= 1; |
| 325 |
|
|
| 326 |
|
|
| 327 |
0 |
if (join > 0x7F) { |
| 328 |
0 |
join = 0x7F; |
| 329 |
0 |
} else if (join < -0x80) { |
| 330 |
0 |
join = -0x80; |
| 331 |
|
} |
| 332 |
|
|
| 333 |
0 |
buffer[i] = (byte) join; |
| 334 |
|
|
| 335 |
0 |
if (recording) { |
| 336 |
|
try { |
| 337 |
0 |
fileWriter.write(join & 0xff); |
| 338 |
0 |
} catch (IOException ioe) { |
| 339 |
0 |
Logger.getLogger(this.getClass()).error("An error occurred while writing the" |
| 340 |
|
+ " sound file.", ioe); |
| 341 |
0 |
} |
| 342 |
|
} |
| 343 |
|
} |
| 344 |
|
|
| 345 |
|
|
| 346 |
0 |
if (enabled) { |
| 347 |
|
try { |
| 348 |
|
|
| 349 |
|
|
| 350 |
0 |
line.write(buffer, 0, samplesPerFrame); |
| 351 |
0 |
} catch (IllegalArgumentException iae) { |
| 352 |
0 |
Logger.getLogger(this.getClass()).error("Error writing to the audio line. " |
| 353 |
|
+ "The bytes do not represent complete frames.", iae); |
| 354 |
0 |
} catch (ArrayIndexOutOfBoundsException aiobe) { |
| 355 |
0 |
Logger.getLogger(this.getClass()).error("Error writing to the audio line. " |
| 356 |
|
+ "The buffer does not contain the number of bytes specified.", aiobe); |
| 357 |
0 |
} |
| 358 |
|
} |
| 359 |
0 |
} |
| 360 |
|
|
| 361 |
|
|
| 362 |
|
|
| 363 |
|
|
| 364 |
|
|
| 365 |
|
public void setRecord() { |
| 366 |
0 |
if (recording) { |
| 367 |
0 |
stopRecording(); |
| 368 |
|
} else { |
| 369 |
0 |
startRecording(); |
| 370 |
|
} |
| 371 |
0 |
} |
| 372 |
|
|
| 373 |
|
|
| 374 |
|
|
| 375 |
|
|
| 376 |
|
|
| 377 |
|
private void startRecording() { |
| 378 |
0 |
if (!recording) { |
| 379 |
|
try { |
| 380 |
0 |
fileWriter = new FileWriter("output.raw"); |
| 381 |
0 |
} catch (IOException ioe) { |
| 382 |
0 |
Logger.getLogger(this.getClass()).error("Could not open file for recording.", ioe); |
| 383 |
0 |
System.out.println("Could not open file for recording"); |
| 384 |
0 |
} |
| 385 |
0 |
recording = true; |
| 386 |
|
} |
| 387 |
0 |
} |
| 388 |
|
|
| 389 |
|
|
| 390 |
|
|
| 391 |
|
|
| 392 |
|
|
| 393 |
|
public void stopRecording() { |
| 394 |
0 |
if (recording) { |
| 395 |
|
try { |
| 396 |
0 |
fileWriter.close(); |
| 397 |
0 |
convertToWav(); |
| 398 |
0 |
} catch (IOException ioe) { |
| 399 |
0 |
Logger.getLogger(this.getClass()).error("Failed whilst closing output.raw", ioe); |
| 400 |
0 |
System.out.println("Failed whilst closing output.raw"); |
| 401 |
0 |
} |
| 402 |
0 |
recording = false; |
| 403 |
|
} |
| 404 |
0 |
} |
| 405 |
|
|
| 406 |
|
|
| 407 |
|
|
| 408 |
|
|
| 409 |
|
private void convertToWav() { |
| 410 |
0 |
File input = new File("output.raw"); |
| 411 |
0 |
File output = new File("output.wav"); |
| 412 |
|
|
| 413 |
|
try { |
| 414 |
0 |
FileInputStream fileInputStream = new FileInputStream(input); |
| 415 |
|
|
| 416 |
0 |
AudioInputStream audioInputStream = new AudioInputStream(fileInputStream, audioFormat |
| 417 |
|
, input.length()); |
| 418 |
0 |
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, output); |
| 419 |
0 |
audioInputStream.close(); |
| 420 |
0 |
input.delete(); |
| 421 |
0 |
System.out.println("OUTPUT.WAV recorded"); |
| 422 |
0 |
} catch (IOException ioe) { |
| 423 |
0 |
Logger.getLogger(this.getClass()).error("Error writing WAV file.", ioe); |
| 424 |
0 |
System.out.println("Error writing WAV file"); |
| 425 |
0 |
} |
| 426 |
0 |
} |
| 427 |
|
|
| 428 |
|
} |