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


Java TargetDataLine.read方法代码示例

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


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

示例1: record

import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
private byte[] record() throws LineUnavailableException {
    AudioFormat format = AudioUtil.getAudioFormat(audioConf);
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

    // Checks if system supports the data line
    if (!AudioSystem.isLineSupported(info)) {
        LOGGER.error("Line not supported");
        System.exit(0);
    }

    microphone = (TargetDataLine) AudioSystem.getLine(info);
    microphone.open(format);
    microphone.start();

    LOGGER.info("Listening, tap enter to stop ...");

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int numBytesRead;
    byte[] data = new byte[microphone.getBufferSize() / 5];

    // Begin audio capture.
    microphone.start();

    // Here, stopped is a global boolean set by another thread.
    while (!stopped) {
        // Read the next chunk of data from the TargetDataLine.
        numBytesRead = microphone.read(data, 0, data.length);
        // Save this chunk of data.
        byteArrayOutputStream.write(data, 0, numBytesRead);
    }

    return byteArrayOutputStream.toByteArray();
}
 
开发者ID:mautini,项目名称:google-assistant-java-demo,代码行数:34,代码来源:AudioRecorder.java

示例2: run

import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
@Override
public void run() {

    try {
        AudioFormat af = SoundPacket.defaultFormat;
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, null);
        mic = (TargetDataLine) (AudioSystem.getLine(info));
        mic.open(af);
        mic.start();
    } catch (Exception e) {
        System.out.println("Microfone não detectado.");
        JOptionPane.showMessageDialog(rootPane, "Microfone não detectado.", "AVISO: ", JOptionPane.INFORMATION_MESSAGE);
    }
    for (;;) {
        Utils.sleep(10);
        if (mic.available() > 0) {
            byte[] buff = new byte[SoundPacket.defaultDataLenght];
            mic.read(buff, 0, buff.length);
            long tot = 0;
            for (int i = 0; i < buff.length; i++) {
                tot += MicThread.amplification * Math.abs(buff[i]);
            }
            tot *= 2.5;
            tot /= buff.length;
            micLev.setValue((int) tot);
        }
    }
}
 
开发者ID:lucas-dolsan,项目名称:tcc-rpg,代码行数:29,代码来源:TelaConfigurarSom.java

示例3: call

import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
@Override
public void call(final Subscriber<? super byte[]> child) {

	DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

	// checks if system supports the data line
	if (!AudioSystem.isLineSupported(info)) {
		child.onError(new RuntimeException("line not supported for format "
				+ format));
		return;
	}

	try {
		TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
		line.open(format);
		System.out.println("Starting capture...");
		line.start();
		byte[] buffer = new byte[bufferSize];
		while (!child.isUnsubscribed() && line.isOpen()) {
			int count = line.read(buffer, 0, bufferSize);
			if (count > 0)
				child.onNext(Arrays.copyOf(buffer, count));
		}
		child.onCompleted();
		line.close();
	} catch (LineUnavailableException e) {
		child.onError(e);
	}
}
 
开发者ID:davidmoten,项目名称:audio-recognition,代码行数:30,代码来源:MicrophoneOnSubscribe.java

示例4: main

import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
public static void main(String[] args) {
	// Pedimos los mixers de nuestro sistema
	Mixer.Info[] mixersInfo = AudioSystem.getMixerInfo();
	
	for (Mixer.Info info : mixersInfo) {
		System.out.println("Mixer: " + info);
	}

	// Los Line están asociados a los mixers, aca verificamos si el micrófono esta asociado a un mixer 
	if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
		
		// Creamos el socket de envío en cualquier puerto
		try (DatagramSocket socket = new DatagramSocket()) {
			
			// Configuramos el destino del audio
			InetAddress address = InetAddress.getLocalHost();
			
			// Creamos un DataLine el cual agrega funcionalidades sobre el Line (Métodos de transporte), el TargetDataLine permite leer audio
			DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, AudioFormatHelper.getAudioFormat());
			
			// Obtenemos la línea
			TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
			
			// Abrimos la línea
			targetDataLine.open(AudioFormatHelper.getAudioFormat());
			
			// El start permite que la línea empiece a usar I/O
			targetDataLine.start();

			while (isRunning) {
				// Creamos un buffer para leer los datos (Usamos 32000 porque el sample lo pusimos en 8000 y 2 bytes por sample = 16000 por segundo)
				byte[] buffer = new byte[32000];
				
				// Leemos y enviamos el audio
				targetDataLine.read(buffer, 0, buffer.length);
				sendAudio(socket, address, buffer);
			}
		} catch (Exception e) {
			// Log and Handle exception
			e.printStackTrace();
		}
	}
}
 
开发者ID:ldebello,项目名称:javacuriosities,代码行数:44,代码来源:AudioSender.java

示例5: run

import javax.sound.sampled.TargetDataLine; //导入方法依赖的package包/类
public void run() {

        // Ensure we have a compatible line for our format.
        AudioFormat format = Utils.getFormat();
        TargetDataLine line = this.getAudioLine(format);
        if (null == line) {
            System.err.println("Unable to get line");
            return;
        }

        // Record!
        ByteArrayOutputStream first_100ms_bytes = new ByteArrayOutputStream();
        byte[] data = new byte[Utils.CHUNK_OF_10MS];

        int numBytesRead = 0;
        int totalBytesRead = 0;
        boolean first100Analyzed = false;

        line.start();
        while (running) {
            // System.out.println("listening");
            if ((numBytesRead = line.read(data, 0, Utils.CHUNK_OF_10MS)) == -1) {
                break;
            }
            totalBytesRead += numBytesRead;
            System.out.println("totalBytesRead: " + totalBytesRead);

            baos.write(data, 0, numBytesRead);

            // Selectively write the first 100ms to this different buffer
            if (first_100ms_bytes.size() < Utils.CHUNK_OF_100_MS) {
                first_100ms_bytes.write(data, 0, numBytesRead);
            } else {
                if (!first100Analyzed) {
                    processFirst100ms(first_100ms_bytes);
                    first100Analyzed = true;
                }
            }

        }

        // we reached the end of the stream. stop and close the line.
        line.stop();
        line.close();
        line = null;

        System.out.println("Line stopped, closed, and nulled");

        // stop and close the output stream
        try {
            baos.flush();
            baos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        this.thread = null;
    }
 
开发者ID:adveres,项目名称:Speech-Detection,代码行数:59,代码来源:AudioRecorder.java


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