當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。