@Override public boolean equals(Object obj) { if (obj instanceof SignalGenerator) { if (getClass() != obj.getClass()) { return false; } SignalGenerator<? extends Quote> sg = (SignalGenerator<?>) obj; Map<String, String> sgparams = sg.getParameters(); if (sgparams == null) { return this.getParameters() == null; } Map<String, String> params = Collections.unmodifiableMap(this.getParameters()); Set<Entry<String, String>> entries = params.entrySet(); Iterator<Entry<String, String>> ei = entries.iterator(); Entry<String, String> en = null; String v, k; while (ei.hasNext()) { en = ei.next(); k = en.getKey(); v = en.getValue(); if (!v.equals(sgparams.get(k))) { return false; } } return true; } else { return false; } }
/** * starts detecting features/gesture depending on the used extractors.<br> * Does not record the data for later analysis */ public void startDetection() { final AudioTrack at = new AudioTrack( android.media.AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM); final AudioRecord ar = new AudioRecord( MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 10 * minSize); final byte[] audio = signalGen.generateAudio(); Thread detectionThread = new Thread( new Runnable() { private int sampleCounter; @Override public void run() { final byte[] buffer = new byte[minSize]; while (detectionRunning) { int samplesRead = ar.read(buffer, 0, minSize); if (samplesRead != minSize) { Log.e( "AUDIO_MANAGER", "Samples read not equal minSize (" + samplesRead + "). Might be loosing data!"); } // size of bufferDouble is buffer.length/2 final double[] bufferDouble = convertToDouble(buffer, samplesRead); if (sampleCounter >= 44100) { if (featureDetector != null) { featureDetector.checkForFeatures(bufferDouble, true); // false } } // omit first second sampleCounter += samplesRead; } ar.stop(); ar.release(); } }); Thread playThread = new Thread( new Runnable() { @Override public void run() { at.play(); while (detectionRunning) { at.write(audio, 0, audio.length); } at.stop(); at.release(); } }); ar.startRecording(); detectionRunning = true; detectionThread.start(); playThread.start(); }
/** * starts recording the received audio. Also saves it to a file for later analysis * * @throws FileNotFoundException */ public void startRecord() throws FileNotFoundException { final AudioTrack at = new AudioTrack( android.media.AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM); final AudioRecord ar = new AudioRecord( MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 10 * minSize); tempFileRec = new File(ctx.getExternalCacheDir().getAbsolutePath() + "/temp_rec.raw"); tempFileSend = new File(ctx.getExternalCacheDir().getAbsolutePath() + "/temp_send.raw"); if (tempFileRec.exists()) tempFileRec.delete(); if (tempFileSend.exists()) tempFileSend.delete(); dosRec = new DataOutputStream(new FileOutputStream(tempFileRec)); dosSend = new DataOutputStream(new FileOutputStream(tempFileSend)); final byte[] audio = signalGen.generateAudio(); Thread recordThread = new Thread( new Runnable() { private int sampleCounter; @Override public void run() { final byte[] buffer = new byte[minSize]; while (recordRunning) { int samplesRead = ar.read(buffer, 0, minSize); if (samplesRead != minSize) { Log.e( "AUDIO_MANAGER", "Samples read not equal minSize (" + samplesRead + "). Might be loosing data!"); } // TODO: consider seperate thread (in case of buffer overflow) writeByteBufferToStream(buffer, dosRec); // size of bufferDouble is buffer.length/2 final double[] bufferDouble = convertToDouble(buffer, samplesRead); if (sampleCounter >= 44100) { if (featureDetector != null) { featureDetector.checkForFeatures(bufferDouble, true); // false } } // omit first second sampleCounter += samplesRead; } ar.stop(); ar.release(); try { dosRec.close(); } catch (IOException e) { e.printStackTrace(); } } }); Thread playThread = new Thread( new Runnable() { @Override public void run() { at.play(); while (recordRunning) { at.write(audio, 0, audio.length); writeByteBufferToStream(audio, dosSend); } at.stop(); at.release(); } }); ar.startRecording(); recordRunning = true; recordThread.start(); playThread.start(); }