Search in sources :

Example 41 with AudioFormat

use of javax.sound.sampled.AudioFormat in project Minim by ddf.

the class Minim method loadFile.

/**
	 * Loads the requested file into an {@link AudioPlayer} with the request
	 * buffer size.
	 * 
	 * @param filename
	 *            the file or URL you want to load
	 * @param bufferSize
	 *            int: the sample buffer size you want, which determines the 
	 *            size of the left, right, and mix AudioBuffer fields of the 
	 *            returned AudioPlayer.
	 * 
	 * @return an <code>AudioPlayer</code> with a sample buffer of the requested
	 *         size, or null if we were unable to load the file
	 */
public AudioPlayer loadFile(String filename, int bufferSize) {
    AudioPlayer player = null;
    AudioRecordingStream rec = mimp.getAudioRecordingStream(filename, bufferSize, false);
    if (rec != null) {
        AudioFormat format = rec.getFormat();
        AudioOut out = mimp.getAudioOutput(format.getChannels(), bufferSize, format.getSampleRate(), format.getSampleSizeInBits());
        if (out != null) {
            player = new AudioPlayer(rec, out);
        } else {
            rec.close();
        }
    }
    if (player != null) {
        addSource(player);
    } else {
        error("Couldn't load the file " + filename);
    }
    return player;
}
Also used : AudioRecordingStream(ddf.minim.spi.AudioRecordingStream) AudioFormat(javax.sound.sampled.AudioFormat) AudioOut(ddf.minim.spi.AudioOut)

Example 42 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jdk8u_jdk by JetBrains.

the class NoteOverFlowTest method main.

public static void main(String[] args) throws Exception {
    AudioSynthesizer synth = new SoftSynthesizer();
    AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
    AudioInputStream stream = synth.openStream(format, null);
    // Make all voices busy, e.g.
    // send midi on and midi off on all available voices
    MidiChannel ch1 = synth.getChannels()[0];
    // Use contionus instrument like string ensemble
    ch1.programChange(48);
    for (int i = 0; i < synth.getMaxPolyphony(); i++) {
        ch1.noteOn(64, 64);
        ch1.noteOff(64);
    }
    // Now send single midi on, and midi off message
    ch1.noteOn(64, 64);
    ch1.noteOff(64);
    // Read 10 sec from stream, by this time all voices should be inactvie
    stream.skip(format.getFrameSize() * ((int) (format.getFrameRate() * 20)));
    // If no voice are active, then this test will pass
    VoiceStatus[] v = synth.getVoiceStatus();
    for (int i = 0; i < v.length; i++) {
        if (v[i].active) {
            throw new RuntimeException("Not all voices are inactive!");
        }
    }
    // Close the synthesizer after use
    synth.close();
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) MidiChannel(javax.sound.midi.MidiChannel) SoftSynthesizer(com.sun.media.sound.SoftSynthesizer) AudioFormat(javax.sound.sampled.AudioFormat) AudioSynthesizer(com.sun.media.sound.AudioSynthesizer) VoiceStatus(javax.sound.midi.VoiceStatus)

Example 43 with AudioFormat

use of javax.sound.sampled.AudioFormat in project jersey by jersey.

the class ToneGenerator method writeWav.

/**
     * Writes the temporary file with the generated audio.
     *
     * @param inputStream input stream with the waveform
     * @param length      length of the waveform
     * @return name of the generated temporary file
     * @throws IOException
     */
private static String writeWav(InputStream inputStream, int length) throws IOException {
    AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLE_RATE, 8, 1, 1, SAMPLE_RATE, false);
    File file = File.createTempFile("wav", ".");
    AudioSystem.write(new AudioInputStream(inputStream, format, length), AudioFileFormat.Type.WAVE, file);
    return file.getAbsolutePath();
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File)

Example 44 with AudioFormat

use of javax.sound.sampled.AudioFormat in project bigbluebutton by bigbluebutton.

the class AudioReceiver method main.

// ******************************* MAIN *******************************
/** The main method. */
public static void main(String[] args) {
    int port = 0;
    int sample_rate = 8000;
    int sample_size = 1;
    boolean linear_signed = false;
    boolean pcmu = false;
    boolean big_endian = false;
    String filename = null;
    boolean sound = true;
    boolean help = true;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-h")) {
            break;
        }
        if (i == 0) {
            port = Integer.parseInt(args[i]);
            help = false;
            continue;
        }
        if (args[i].equals("-F") && args.length > (i + 1)) {
            sound = false;
            filename = args[++i];
            continue;
        }
        if (args[i].equals("-S") && args.length > (i + 2)) {
            sample_rate = Integer.parseInt(args[++i]);
            sample_size = Integer.parseInt(args[++i]);
            continue;
        }
        if (args[i].equals("-Z")) {
            linear_signed = true;
            continue;
        }
        if (args[i].equals("-U")) {
            pcmu = true;
            continue;
        }
        if (args[i].equals("-E")) {
            big_endian = true;
            continue;
        }
        // else, do:
        System.out.println("unrecognized param '" + args[i] + "'\n");
        help = true;
    }
    if (help) {
        System.out.println("usage:\n  java AudioReceiver <local_port> [options]");
        System.out.println("   options:");
        System.out.println("   -h               this help");
        System.out.println("   -F <audio_file>  records to audio file");
        System.out.println("   -S <rate> <size> sample rate [B/s], and size [B]");
        System.out.println("   -Z               uses PCM linear signed format (linear unsigned is used as default)");
        System.out.println("   -U               uses PCMU format");
        System.out.println("   -E               uses big endian format");
        System.exit(0);
    }
    AudioFormat.Encoding codec;
    if (pcmu)
        codec = AudioFormat.Encoding.ULAW;
    else if (linear_signed)
        codec = AudioFormat.Encoding.PCM_SIGNED;
    else
        codec = AudioFormat.Encoding.PCM_UNSIGNED;
    try {
        RtpStreamReceiver receiver;
        AudioOutput audio_output = null;
        if (sound)
            AudioOutput.initAudioLine();
        if (sound) {
            AudioFormat format = new AudioFormat(codec, sample_rate, 8 * sample_size, 1, sample_size, sample_rate, big_endian);
            audio_output = new AudioOutput(format);
            receiver = new RtpStreamReceiver(audio_output.getOuputStream(), port);
        } else //if (filename!=null)
        {
            File file = new File(filename);
            /*
            AudioFileFormat format=AudioSystem.getAudioFileFormat(file);
            System.out.println("File audio format: "+format);
            OutputStream output_stream=new OutputStream() { public void write(int b) {} };
            receiver=new RtpStreamReceiver(output_stream,port);
            */
            FileOutputStream output_stream = new FileOutputStream(file);
            receiver = new RtpStreamReceiver(output_stream, port);
        }
        receiver.start();
        if (sound)
            audio_output.play();
        System.out.println("Press 'Return' to stop");
        System.in.read();
        receiver.halt();
        if (sound)
            audio_output.stop();
        if (sound)
            AudioOutput.closeAudioLine();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) AudioFormat(javax.sound.sampled.AudioFormat) File(java.io.File)

Example 45 with AudioFormat

use of javax.sound.sampled.AudioFormat in project playn by threerings.

the class BigClip method open.

@Override
public void open(AudioInputStream stream) throws IOException, LineUnavailableException {
    AudioInputStream is1;
    format = stream.getFormat();
    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
        is1 = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, stream);
    } else {
        is1 = stream;
    }
    format = is1.getFormat();
    InputStream is2 = is1;
    byte[] buf = new byte[1 << 16];
    int numRead = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    numRead = is2.read(buf);
    while (numRead > -1) {
        baos.write(buf, 0, numRead);
        numRead = is2.read(buf, 0, buf.length);
    }
    is2.close();
    audioData = baos.toByteArray();
    AudioFormat afTemp;
    if (format.getChannels() < 2) {
        int frameSize = format.getSampleSizeInBits() * 2 / 8;
        afTemp = new AudioFormat(format.getEncoding(), format.getSampleRate(), format.getSampleSizeInBits(), 2, frameSize, format.getFrameRate(), format.isBigEndian());
    } else {
        afTemp = format;
    }
    setLoopPoints(0, audioData.length);
    dataLine = AudioSystem.getSourceDataLine(afTemp);
    dataLine.open();
    inputStream = new ByteArrayInputStream(audioData);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AudioFormat(javax.sound.sampled.AudioFormat)

Aggregations

AudioFormat (javax.sound.sampled.AudioFormat)74 AudioInputStream (javax.sound.sampled.AudioInputStream)32 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)11 AudioFileFormat (javax.sound.sampled.AudioFileFormat)10 SourceDataLine (javax.sound.sampled.SourceDataLine)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 MpegAudioFormat (javazoom.spi.mpeg.sampled.file.MpegAudioFormat)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DataInputStream (java.io.DataInputStream)6 File (java.io.File)6 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 Vector (java.util.Vector)5 DataLine (javax.sound.sampled.DataLine)5 LineUnavailableException (javax.sound.sampled.LineUnavailableException)5 SequenceInputStream (java.io.SequenceInputStream)4 AudioMetaData (ddf.minim.AudioMetaData)3 DataOutputStream (java.io.DataOutputStream)3