public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
@Override public void run() { byte[] data = new byte[microphone.getBufferSize()]; out = new ByteArrayOutputStream(); try { microphone.flush(); microphone.start(); while (!done) { int numBytesRead = microphone.read(data, 0, data.length); if (numBytesRead != -1) { out.write(data, 0, numBytesRead); } else { break; } } microphone.stop(); out.flush(); } catch (IOException e) { e.printStackTrace(); } synchronized (lock) { lock.notify(); } } }
out("BufferingRecorder.run(): trying to read: " + bytesToRead); int nBytesRead = m_line.read(abBuffer, 0, bytesToRead); totalBytes += nBytesRead; if (totalBytesToRead > 0 && totalBytes >= totalBytesToRead) {
out("BufferingRecorder.run(): trying to read: " + bytesToRead); int nBytesRead = m_line.read(abBuffer, 0, bytesToRead); totalBytes += nBytesRead; if (totalBytesToRead > 0 && totalBytes >= totalBytesToRead) {
out("BufferingRecorder.run(): trying to read: " + bytesToRead); int nBytesRead = m_line.read(abBuffer, 0, bytesToRead); totalBytes += nBytesRead; if (totalBytesToRead > 0 && totalBytes >= totalBytesToRead) {
out("BufferingRecorder.run(): trying to read: " + bytesToRead); int nBytesRead = m_line.read(abBuffer, 0, bytesToRead); totalBytes += nBytesRead; if (totalBytesToRead > 0 && totalBytes >= totalBytesToRead) {
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
public void read (short[] samples, int offset, int numSamples) { if (buffer.length < numSamples * 2) buffer = new byte[numSamples * 2]; int toRead = numSamples * 2; int read = 0; while (read != toRead) read += line.read(buffer, read, toRead - read); for (int i = 0, j = 0; i < numSamples * 2; i += 2, j++) samples[offset + j] = (short)((buffer[i + 1] << 8) | (buffer[i] & 0xff)); }
/** * @see io.github.amyassist.amy.core.audio.environment.AbstractAudioEnvironment#readFromInput(byte[], * int, int) */ @Override protected int readFromInput(byte[] buffer, int off, int len) { return this.tdl.read(buffer, off, len); }
TargetDataLine dataLine = (TargetDataLine) AudioSystem.getLine(new DataLine.Info(TargetDataLine.class, audioFormat)); dataLine.open(); dataLine.start(); dataLine.read(b, offset, len);
/** * Returns the a byte[] containing the specified number of bytes * @param numOfBytes The length of the returned array. * @return The specified array or null if it cannot. */ private byte[] getBytes(int numOfBytes){ if(getTargetDataLine()!=null){ byte[] data = new byte[numOfBytes]; this.getTargetDataLine().read(data, 0, numOfBytes); return data; } return null;//If data cannot be read, returns a null array. }
@Override public void run () { this.isRecording = true; final byte [] data = new byte [this.dataLine.getBufferSize () / TargetDataLineReaderThread.FIVE]; while (this.isRecording) { // Read the next chunk of data from the TargetDataLine. final int numBytesRead = this.dataLine.read (data, 0, data.length); // Save this chunk of data. this.exporter.export (data, numBytesRead); } }
protected String doInBackground() throws Exception { AudioFormat af = new AudioFormat(8000.0f,8,1,true,false); DataLine.Info info = new DataLine.Info(TargetDataLine.class, af); TargetDataLine microphone = (TargetDataLine)AudioSystem.getLine(info); microphone.open(af); Socket conn = new Socket(SERVER,3000); microphone.start(); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); int bytesRead = 0; byte[] soundData = new byte[1]; Thread inThread = new Thread(new SoundReceiver(conn)); inThread.start(); while(bytesRead != -1) { bytesRead = microphone.read(soundData, 0, soundData.length); if(bytesRead >= 0) { dos.write(soundData, 0, bytesRead); } } // TODO Auto-generated method stub return null; }
@Override public void run() { byte[] data = new byte[microphone.getBufferSize()]; out = new ByteArrayOutputStream(); try { microphone.flush(); microphone.start(); while (!done) { int numBytesRead = microphone.read(data, 0, data.length); if (numBytesRead != -1) { out.write(data, 0, numBytesRead); } else { break; } } microphone.stop(); out.flush(); } catch (IOException e) { e.printStackTrace(); } synchronized (lock) { lock.notify(); } } }
/** * Calculates the frequency based off of the number of bytes. * CAVEAT: The number of Bytes must be a multiple of 2! * @param numOfBytes The number of bytes which must be a multiple of 2!!! * @return The calculated frequency in Hertz. */ public int getFrequency(int numOfBytes) throws Exception{ if(getTargetDataLine() == null){ return -1; } byte[] data = new byte[numOfBytes+1];//One byte is lost during conversion this.getTargetDataLine().read(data, 0, numOfBytes); return getFrequency(data); }
public float[] read() { // TODO: this is sort of terrible, but will do for now. would be much better // to dig the conversion stuff out of FloatSampleBuffer and do this more directly int numSamples = 1; // allocate enough bytes for one sample frame byte[] bytes = new byte[ line.getFormat().getFrameSize() ]; line.read(bytes, 0, bytes.length); buffer.setSamplesFromBytes(bytes, 0, line.getFormat(), 0, numSamples); // allocate enough floats for the number of channels float[] samples = new float[ buffer.getChannelCount() ]; for(int i = 0; i < samples.length; i++) { samples[i] = buffer.getChannel(i)[0]; } return samples; }
public int read(MultiChannelBuffer buffer) { // create our converter object int numChannels = line.getFormat().getChannels(); int numSamples = buffer.getBufferSize(); float sampleRate = line.getFormat().getSampleRate(); FloatSampleBuffer convert = new FloatSampleBuffer( numChannels, numSamples, sampleRate ); // allocate enough bytes for the size of this buffer byte[] bytes = new byte[ convert.getByteArrayBufferSize(line.getFormat()) ]; // read the bytes line.read(bytes, 0, bytes.length); // convert the bytes convert.setSamplesFromBytes(bytes, 0, line.getFormat(), 0, numSamples); // copy the converted floats into the MultiChannelBuffer // make sure it has the correct number of channels first buffer.setChannelCount(numChannels); for(int i = 0; i < convert.getChannelCount(); i++) { buffer.setChannel(i, convert.getChannel(i)); } return numSamples; } }