当前位置: 首页>>代码示例>>Java>>正文


Java AudioSystem.write方法代码示例

本文整理汇总了Java中javax.sound.sampled.AudioSystem.write方法的典型用法代码示例。如果您正苦于以下问题:Java AudioSystem.write方法的具体用法?Java AudioSystem.write怎么用?Java AudioSystem.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.sound.sampled.AudioSystem的用法示例。


在下文中一共展示了AudioSystem.write方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testSampleRate

import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
private static boolean testSampleRate(float sampleRate) {
    boolean result = true;

    try {
        // create AudioInputStream with sample rate of 10000 Hz
        ByteArrayInputStream data = new ByteArrayInputStream(new byte[1]);
        AudioFormat format = new AudioFormat(sampleRate, 8, 1, true, true);
        AudioInputStream stream = new AudioInputStream(data, format, 1);

        // write to AIFF file
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        AudioSystem.write(stream, AudioFileFormat.Type.AIFF, outputStream);
        byte[] fileData = outputStream.toByteArray();
        InputStream inputStream = new ByteArrayInputStream(fileData);
        AudioFileFormat aff = AudioSystem.getAudioFileFormat(inputStream);
        if (! equals(sampleRate, aff.getFormat().getFrameRate())) {
            out("error for sample rate " + sampleRate);
            result = false;
        }
    } catch (Exception e) {
        out(e);
        out("Test NOT FAILED");
    }
    return result;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:AiffSampleRate.java

示例2: save

import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/**
 * Saves the double array as an audio file (using .wav or .au format).
 *
 * @param filename
 *            the name of the audio file
 * @param samples
 *            the array of samples
 * @throws IllegalArgumentException
 *             if unable to save {@code filename}
 * @throws IllegalArgumentException
 *             if {@code samples} is {@code null}
 */
public static void save(String filename, double[] samples) {
	if (samples == null) {
		throw new IllegalArgumentException("samples[] is null");
	}

	// assumes 44,100 samples per second
	// use 16-bit audio, mono, signed PCM, little Endian
	AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
	byte[] data = new byte[2 * samples.length];
	for (int i = 0; i < samples.length; i++) {
		int temp = (short) (samples[i] * MAX_16_BIT);
		data[2 * i + 0] = (byte) temp;
		data[2 * i + 1] = (byte) (temp >> 8);
	}

	// now save the file
	try {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		AudioInputStream ais = new AudioInputStream(bais, format, samples.length);
		if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
			AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
		} else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
			AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
		} else {
			throw new IllegalArgumentException("unsupported audio format: '" + filename + "'");
		}
	} catch (IOException ioe) {
		throw new IllegalArgumentException("unable to save file '" + filename + "'", ioe);
	}
}
 
开发者ID:Scoutdrago3,项目名称:MusicToGraph,代码行数:43,代码来源:StdAudio.java

示例3: glitchPixels

import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
@Override
public byte[] glitchPixels(byte[] inputImageBytes) throws Exception 
{
	int audioBitRate = ((Integer) getPixelGlitchParameters().get("bitRateBlend")).intValue();
	float bitRateBlend = (float) audioBitRate / 10;
	if(bitRateBlend < 0.1F || bitRateBlend > 0.9F)
	{
		return null;
	}
	
	BufferedImage inputImage = ImageUtil.getImageFromBytes(inputImageBytes);
	InputStream imageInputStream = new ByteArrayInputStream(inputImageBytes);
	AudioInputStream distortionAudioStream = new AudioInputStream(imageInputStream, new AudioFormat(AudioFormat.Encoding.ULAW, ThreadLocalRandom.current().nextInt(8000,  20000), 8, 5, 9, ThreadLocalRandom.current().nextInt(8000,  20000), true), inputImageBytes.length);
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	AudioSystem.write(distortionAudioStream, Type.WAVE, outputStream);
	BufferedImage outputImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
	byte[] imageData = ((DataBufferByte) outputImage.getRaster().getDataBuffer()).getData();
	System.arraycopy(outputStream.toByteArray(),0,imageData,0,outputStream.toByteArray().length);
	int[] abgrOffsets = {3, 2, 1, 0}; 
	DataBuffer outputBuffer = new DataBufferByte(imageData, imageData.length);
    WritableRaster raster = Raster.createInterleavedRaster(outputBuffer, inputImage.getWidth(), inputImage.getHeight(), 4 * inputImage.getWidth(), 4, abgrOffsets, null);
    ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    BufferedImage rasterizedImage = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    rasterizedImage = resizeImage(rasterizedImage, inputImage.getWidth() * 4, inputImage.getHeight() * 4);
    Graphics2D g2d = rasterizedImage.createGraphics();
    g2d.setComposite(AlphaComposite.SrcOver.derive(bitRateBlend));
    g2d.drawImage(inputImage, 0, 0, null);
    g2d.dispose();
    rasterizedImage = rasterizedImage.getSubimage(0, 0, inputImage.getWidth(), inputImage.getHeight());
	return ImageUtil.getImageBytes(rasterizedImage);
}
 
开发者ID:scriptkittie,项目名称:GlitchKernel,代码行数:32,代码来源:DataAsSound.java

示例4: stopRecord

import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/** Stop recoding and save if requested */
public void stopRecord() {
	RecordDialog dialog = new RecordDialog(window, 420, 150);

	if (dialog.getAcceptRecord()) {
		Record newRecord = new Record(dialog.getName(), References.CHRONOMETER.getMinute(),
				References.CHRONOMETER.getSecond(), References.CHRONOMETER.getHundredths());
		References.RECORD_PANEL.getRecordModel().addElement(newRecord);
		References.RECORD_PANEL.setUIStatus("stop");
		References.CHRONOMETER.stop();

		recordData = byteArrayOutputStream.toByteArray();
		recordIS = new ByteArrayInputStream(recordData);
		recordAIS = new AudioInputStream(recordIS, audioFormat, recordData.length / audioFormat.getFrameSize());
		File wavFile = new File(newRecord.getRelativePath());
		try {
			AudioSystem.write(recordAIS, fileType, wavFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} else {
		if (transmissionON) {
			References.RECORD_PANEL.setUIStatus("transmissionON");
		}
	}

	if (References.KEYLISTENER_PANEL.isKeyIsDown()) {
		References.KEYLISTENER_PANEL.setKeyIsDown();
		References.KEYLISTENER_PANEL.getKeyReleasedAction().actionPerformed(null);
	}
}
 
开发者ID:AitorB,项目名称:POPBL_V,代码行数:32,代码来源:CommunicationHandler.java

示例5: main

import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
public static void main(String argv[]) throws Exception {
    AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
    InputStream is = new ByteArrayInputStream(new byte[1000]);
    AudioInputStream ais = new AudioInputStream(is, format, AudioSystem.NOT_SPECIFIED);
    AudioSystem.write(ais, AudioFileFormat.Type.AU, new ByteArrayOutputStream());
    System.out.println("Test passed.");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:WriteAuUnspecifiedLength.java

示例6: save

import javax.sound.sampled.AudioSystem; //导入方法依赖的package包/类
/**
 * Save recorded sound data into a .wav file format.
 * @param wavFile The file to be saved.
 * @throws IOException if any I/O error occurs.
 */
public void save(File wavFile) throws IOException {
    byte[] audioData = recordBytes.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
    AudioInputStream audioInputStream = new AudioInputStream(bais, format,
            audioData.length / format.getFrameSize());

    AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, wavFile);

    audioInputStream.close();
    recordBytes.close();
}
 
开发者ID:ksg14,项目名称:duncan,代码行数:17,代码来源:SoundRecordingUtil.java


注:本文中的javax.sound.sampled.AudioSystem.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。