Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.anmipo.nipkow;
- import java.io.BufferedOutputStream;
- import java.io.ByteArrayInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import javax.sound.sampled.AudioFileFormat;
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineUnavailableException;
- import com.sun.media.sound.WaveFileWriter;
- public class NipkowTester {
- //display resolution
- public static final int ROWS = 32;
- public static final int COLS = 22;
- //frames (disk rotations) per second
- public static final float FPS = 12.5f;
- //clip duration
- public static final int PLAY_DURATION_SECONDS = 10 * 60;
- //image data (scanned left-to-right, top-to-bottom)
- //values denote brightness (0 - min, 9 - max)
- public static final byte[] IMAGE = new byte[] {
- // 5 10 15 20
- 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0, //01
- 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0, //02
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,9,9,0,0, 0,0, //03
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,9,9,0,0, 0,0, //04
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,9,9,0,0, 0,0, //05
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,9,9,0,0, 0,0, //06
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,0,0,0,0, 0,0, //07
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,0,0,0,0, 0,0, //08
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,0,0,0,0, 0,0, //09
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,0,0,0,0, 0,0, //10
- 0,0,0,9,9, 0,0,0,0,9, 9,0,0,0,0, 0,9,9,0,0, 0,0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0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0, //31
- 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0, //32
- };
- //sound sampling frequency
- public static final int SAMPLE_RATE = 44100; //Hz
- //career sound wave frequency to be modulated by pixel data
- public static final int CAREER_FREQUENCY = 22050; //Hz
- //each sound sample is multiplied by this number
- public static final int VOLUME = 13;
- //output audio format
- public static final AudioFormat AUDIO_FORMAT =
- new AudioFormat(
- SAMPLE_RATE, //sample rate
- 8, //sample size in bits
- 1, //number of channels
- true, //signed
- false); //bigEndian
- public static void main(String[] args) {
- try {
- byte[] samples = buildFrame(IMAGE, ROWS, COLS, FPS);
- //saveToWav(samples, "d://temp//nipkov.wav");
- playFrame(samples, (int)(PLAY_DURATION_SECONDS * FPS));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Constructs a sequence of raw audio samples representing the given image.
- * @param image the image to convert
- * @param rows vertical image size
- * @param cols horizontal image size
- * @param fps desired output frame rate
- * @return array of raw audio samples in {@link #AUDIO_FORMAT} format
- */
- protected static byte[] buildFrame(byte[] image, int rows, int cols, float fps) {
- if (image.length != (rows * cols))
- throw new java.lang.IllegalArgumentException(
- String.format("The image is not %d by %d", rows, cols));
- float frameDuration = 1/fps; //seconds per frame
- float pixelDuration = frameDuration / (cols * rows); //seconds per pixel
- //number of sound samples per pixel
- int samplesPerPixel = Math.round(
- pixelDuration * AUDIO_FORMAT.getSampleRate());
- //number of samples between career meander change
- int careerPeriod = SAMPLE_RATE / CAREER_FREQUENCY;
- int bufferLength = (int)(samplesPerPixel * cols * rows);
- byte[] buf = new byte[bufferLength];
- int iPixel = 0; //current pixel of the IMAGE
- int careerValue = -1; //current career meander state (+1 or -1)
- int pixelCounter = samplesPerPixel;
- int careerCounter = careerPeriod;
- for (int i = 0; i < bufferLength; i++) {
- buf[i] = (byte)(VOLUME * careerValue * image[iPixel]);
- if (--pixelCounter == 0) {
- pixelCounter = samplesPerPixel;
- iPixel++;
- careerValue = -careerValue;
- } else if (--careerCounter == 0) {
- careerValue = -careerValue;
- careerCounter = careerPeriod;
- }
- }
- return buf;
- }
- /**
- * Outputs the given audio <tt>samples</tt> to sound card.
- * Does not return until playing is done.
- * @param samples audio samples to play, representing
- * one image frame (in {@link #AUDIO_FORMAT}).
- * @param loopCount number of times to repeat the samples
- * @throws LineUnavailableException
- * @throws InterruptedException
- */
- protected static void playFrame(byte[] samples, int loopCount)
- throws LineUnavailableException, InterruptedException {
- Clip clickClip = AudioSystem.getClip();
- clickClip.open(AUDIO_FORMAT, samples, 0, samples.length);
- try {
- System.out.print("Playing... ");
- clickClip.setFramePosition(0);
- clickClip.loop(loopCount);
- clickClip.drain();
- //sleep until all the sound is played
- Thread.sleep((1000 * samples.length/SAMPLE_RATE) * loopCount);
- } finally {
- clickClip.close();
- System.out.println("Done");
- }
- }
- /**
- * Saves raw audio samples (in {@link #AUDIO_FORMAT} format) to a wave file.
- * @param samples audio samples to save
- * @param fileName fully specified output file name
- * @throws IOException
- */
- protected static void saveToWav(byte[] samples, String fileName) throws IOException {
- AudioInputStream in = new AudioInputStream(
- new ByteArrayInputStream(samples),
- AUDIO_FORMAT, samples.length/AUDIO_FORMAT.getFrameSize());
- try {
- BufferedOutputStream out = new BufferedOutputStream(
- new FileOutputStream(fileName));
- try {
- WaveFileWriter writer = new WaveFileWriter();
- writer.write(in, AudioFileFormat.Type.WAVE, out);
- } finally {
- out.close();
- }
- } finally {
- in.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement