diff --git a/android/probe/README.md b/android/probe/README.md index 006695bb..41d68dee 100644 --- a/android/probe/README.md +++ b/android/probe/README.md @@ -17,6 +17,11 @@ Phase 0 of the stems app (the design: one Android screen that answers, on a give input (sources × 8/6 channels) and shows which channels carry sound. 6. **The stems and the phone's microphone together** (0.3): test 5 with the built-in mic recorded at the same time (a mono WAV beside the 8-channel one): the mic as one more track in the app. +7. **The mic through the FM-1 and back** (0.4, firmware 0.8.0, MENU > SYSTEM > USB > STEMS+IN): the phone's mic goes + out to the FM-1 by our own driver (isochronous OUT, `usbiso.c` duplex) into the last live track, its sends and + FX; 10 channels come back (T1–T6, FX L R, the mic as mixed, the mic as it came). With the phone near the FM-1's + speaker the mic hears the FM-1: the lag between the stems and the raw mic is the whole latency, and a second WAV + has the mic channels moved earlier by it. Every build signs with `app/probe-debug.keystore` (a debug key, public on purpose) so a new APK installs over the last. diff --git a/android/probe/app/build.gradle b/android/probe/app/build.gradle index 0ebe81bc..2b730f1c 100644 --- a/android/probe/app/build.gradle +++ b/android/probe/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "fm.jiant.probe" minSdk 29 // (MediaStore.Downloads with no storage permission) targetSdk 34 - versionCode 3 - versionName "0.3" + versionCode 4 + versionName "0.4" } signingConfigs { debug { // (one key for every build: a new APK installs over the last one) diff --git a/android/probe/app/src/main/cpp/usbiso.c b/android/probe/app/src/main/cpp/usbiso.c index 9fa0620c..6e739623 100644 --- a/android/probe/app/src/main/cpp/usbiso.c +++ b/android/probe/app/src/main/cpp/usbiso.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #define NURB 8 /* URBs in flight */ @@ -99,3 +100,137 @@ JNIEXPORT jint JNICALL Java_fm_jiant_probe_MainActivity_isoRead(JNIEnv *env, jcl (*env)->SetIntArrayRegion(env, stats, 0, 6, st); return got ? got : -err; } + +/* ---- (0.4) both ways: the phone's mic out to the FM-1 (EP OUT, isochronous adaptive: 44 / 45 stereo frames a ms, + * the mono mic on both sides) while its stems come in (MENU > USB STEMS+IN). The mic thread pushes into a ring here + * (pushMic); each OUT packet takes what it holds, kept near 10 ms (more is dropped, less sends silence) */ +#define MR_N 32768u +static int16_t mring[MR_N]; +static volatile uint32_t mr_w, mr_r; + +JNIEXPORT void JNICALL Java_fm_jiant_probe_MainActivity_pushMic(JNIEnv *env, jclass cls, jshortArray a, jint n) +{ + (void)cls; + jshort *p = (*env)->GetShortArrayElements(env, a, NULL); + uint32_t w = __atomic_load_n(&mr_w, __ATOMIC_RELAXED); + for (jint i = 0; i < n; i++) + mring[(w + (uint32_t)i) & (MR_N - 1u)] = p[i]; + __atomic_store_n(&mr_w, w + (uint32_t)n, __ATOMIC_RELEASE); + (*env)->ReleaseShortArrayElements(env, a, p, JNI_ABORT); +} + +#define NURB_O 4 +#define OPK 8 +#define OMAXP 184 /* 46 stereo frames of 16 bit */ +static uint32_t o_acc; /* 44.1 frames a ms: a 45 every tenth */ +static int o_go, o_under, o_drop, o_sent; +static void fill_out(struct usbdevfs_urb *u, int ep) +{ + int16_t *d = u->buffer; + int total = 0; + uint32_t r = mr_r, w = __atomic_load_n(&mr_w, __ATOMIC_ACQUIRE); + if (w - r > 2048u) { o_drop += (int)(w - r - 441u); r = w - 441u; } /* > 46 ms in hand: back to 10 */ + if (!o_go && w - r >= 441u) o_go = 1; /* primed: 10 ms in hand */ + u->type = USBDEVFS_URB_TYPE_ISO; + u->endpoint = (unsigned char)ep; + u->flags = USBDEVFS_URB_ISO_ASAP; + u->number_of_packets = OPK; + u->status = 0; + u->actual_length = 0; + u->error_count = 0; + for (int k = 0; k < OPK; k++) { + int f = 44; + o_acc += 100u; + if (o_acc >= 1000u) { o_acc -= 1000u; f = 45; } + for (int i = 0; i < f; i++) { + int16_t v = 0; + if (o_go && r != w) v = mring[r++ & (MR_N - 1u)]; + else if (o_go) o_under++; + d[2 * (total + i)] = d[2 * (total + i) + 1] = v; + } + u->iso_frame_desc[k].length = (unsigned)(f * 4); + u->iso_frame_desc[k].actual_length = 0; + u->iso_frame_desc[k].status = 0; + total += f; + o_sent += f; + } + u->buffer_length = total * 4; + mr_r = r; +} + +/* the stems in (as isoRead) and the mic out together; stats: packets in, empty, errors, errno, largest, left, frames + * out, out frames short of mic (silence sent), mic frames dropped */ +JNIEXPORT jint JNICALL Java_fm_jiant_probe_MainActivity_duplex(JNIEnv *env, jclass cls, jint fd, jint ep, jint maxp, jint epo, + jbyteArray out, jint timeout_ms, jintArray stats) +{ + (void)cls; + jsize cap = (*env)->GetArrayLength(env, out); + jbyte *dst = (*env)->GetByteArrayElements(env, out, NULL); + size_t sz = sizeof(struct usbdevfs_urb) + NPK * sizeof(struct usbdevfs_iso_packet_desc); + size_t szo = sizeof(struct usbdevfs_urb) + OPK * sizeof(struct usbdevfs_iso_packet_desc); + struct usbdevfs_urb *u[NURB], *uo[NURB_O]; + int got = 0, packets = 0, empty = 0, perr = 0, err = 0, big = 0, live = 0; + o_acc = 0; o_go = 0; o_under = 0; o_drop = 0; o_sent = 0; + mr_r = __atomic_load_n(&mr_w, __ATOMIC_ACQUIRE); /* (nothing from before) */ + for (int i = 0; i < NURB; i++) { u[i] = calloc(1, sz); u[i]->buffer = malloc((size_t)NPK * maxp); } + for (int i = 0; i < NURB_O; i++) { uo[i] = calloc(1, szo); uo[i]->buffer = malloc((size_t)OPK * OMAXP); } + for (int i = 0; i < NURB && !err; i++) { + setup(u[i], ep, maxp); + if (ioctl(fd, USBDEVFS_SUBMITURB, u[i]) < 0) err = errno; else live++; + } + for (int i = 0; i < NURB_O && !err; i++) { + fill_out(uo[i], epo); + if (ioctl(fd, USBDEVFS_SUBMITURB, uo[i]) < 0) err = errno; else live++; + } + long long end = now_ms() + timeout_ms; + while (!err && got < cap && now_ms() < end) { + struct usbdevfs_urb *r = NULL; + if (ioctl(fd, USBDEVFS_REAPURBNDELAY, &r) < 0) { + if (errno == EAGAIN) { struct timespec s = {0, 500000}; nanosleep(&s, NULL); continue; } + if (errno == EINTR) continue; + err = errno; + break; + } + live--; + if (r->endpoint == (unsigned char)epo) { /* an OUT URB back: the next from the ring */ + fill_out(r, epo); + if (ioctl(fd, USBDEVFS_SUBMITURB, r) < 0) { err = errno; break; } + live++; + continue; + } + const char *b = r->buffer; + for (int k = 0; k < NPK; k++) { + int n = (int)r->iso_frame_desc[k].actual_length; + packets++; + if (r->iso_frame_desc[k].status) perr++; + if (!n) { empty++; continue; } + if (n > big) big = n; + if (n > cap - got) n = cap - got; + memcpy(dst + got, b + k * maxp, (size_t)n); + got += n; + } + setup(r, ep, maxp); + if (got < cap) { + if (ioctl(fd, USBDEVFS_SUBMITURB, r) < 0) { err = errno; break; } + live++; + } + } + for (int i = 0; i < NURB; i++) ioctl(fd, USBDEVFS_DISCARDURB, u[i]); + for (int i = 0; i < NURB_O; i++) ioctl(fd, USBDEVFS_DISCARDURB, uo[i]); + long long stop = now_ms() + 500; + while (live > 0 && now_ms() < stop) { + struct usbdevfs_urb *r = NULL; + if (ioctl(fd, USBDEVFS_REAPURBNDELAY, &r) == 0) { live--; continue; } + if (errno != EAGAIN && errno != EINTR) break; + struct timespec s = {0, 1000000}; + nanosleep(&s, NULL); + } + if (live == 0) { + for (int i = 0; i < NURB; i++) { free(u[i]->buffer); free(u[i]); } + for (int i = 0; i < NURB_O; i++) { free(uo[i]->buffer); free(uo[i]); } + } + (*env)->ReleaseByteArrayElements(env, out, dst, 0); + jint st[9] = {packets, empty, perr, err, big, live, o_sent, o_under, o_drop}; + (*env)->SetIntArrayRegion(env, stats, 0, 9, st); + return got ? got : -err; +} diff --git a/android/probe/app/src/main/java/fm/jiant/probe/MainActivity.java b/android/probe/app/src/main/java/fm/jiant/probe/MainActivity.java index 83b52aa9..314fb399 100644 --- a/android/probe/app/src/main/java/fm/jiant/probe/MainActivity.java +++ b/android/probe/app/src/main/java/fm/jiant/probe/MainActivity.java @@ -73,6 +73,9 @@ public class MainActivity extends Activity { static { System.loadLibrary("jiantusb"); } /** usbiso.c: isochronous IN reads on a usbfs descriptor; stats = packets, empty, errors, errno, largest, left */ private static native int isoRead(int fd, int ep, int maxp, byte[] out, int timeoutMs, int[] stats); + /** (0.4) usbiso.c: the stems in and the pushed mic out together (MENU > USB STEMS+IN) */ + private static native int duplex(int fd, int ep, int maxp, int epOut, byte[] out, int timeoutMs, int[] stats); + private static native void pushMic(short[] pcm, int n); @Override protected void onCreate(Bundle b) { @@ -107,6 +110,7 @@ public class MainActivity extends Activity { root.addView(button("4 · ENVIAR EL MIC AL FM-1 (5 s, modo IN+OUT)", v -> run(this::sendMic))); root.addView(button("5 · DRIVER PROPIO: 8 CANALES DIRECTO (10 s)", v -> run(this::directStems))); root.addView(button("6 · FM-1 DIRECTO + MIC DEL CELU (10 s)", v -> run(this::stemsAndMic))); + root.addView(button("7 · MIC AL FM-1 Y VUELTA + LATENCIA (STEMS+IN, 10 s)", v -> run(this::stemsIn))); LinearLayout row = new LinearLayout(this); row.setOrientation(LinearLayout.HORIZONTAL); Button copy = button("COPIAR INFORME", v -> copyReport()); @@ -126,7 +130,7 @@ public class MainActivity extends Activity { root.addView(sv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1)); setContentView(root); - say("JIANT Probe 0.3 · " + Build.MANUFACTURER + " " + Build.MODEL + " · Android " + Build.VERSION.RELEASE + say("JIANT Probe 0.4 · " + Build.MANUFACTURER + " " + Build.MODEL + " · Android " + Build.VERSION.RELEASE + " (API " + Build.VERSION.SDK_INT + ")"); if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) requestPermissions(new String[] {Manifest.permission.RECORD_AUDIO}, 1); @@ -557,6 +561,162 @@ public class MainActivity extends Activity { : "RESULTADO: el mic no trajo sonido mientras leía el FM-1."); } + // ------------------------------------------------------------------ 7 · the mic through the FM-1 and back + /** the streaming interface (alt 1) with an isochronous endpoint going dir, its alt 0, the endpoint; null if none */ + private static Object[] stream(UsbDevice dv, int dir) { + for (int i = 0; i < dv.getInterfaceCount(); i++) { + UsbInterface it = dv.getInterface(i); + if (it.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO || it.getInterfaceSubclass() != 2) continue; + for (int e = 0; e < it.getEndpointCount(); e++) { + UsbEndpoint x = it.getEndpoint(e); + if (x.getType() != UsbConstants.USB_ENDPOINT_XFER_ISOC || x.getDirection() != dir) continue; + UsbInterface off = null; + for (int j = 0; j < dv.getInterfaceCount(); j++) + if (dv.getInterface(j).getId() == it.getId() && dv.getInterface(j).getAlternateSetting() == 0) off = dv.getInterface(j); + return new Object[] {it, off, x}; + } + } + return null; + } + + /** the lag (frames, 0..max) at which b best follows a, and the peak's share of the correlation (0..1) */ + private static double[] lag(float[] a, float[] b, int max) { + int d = 4, n = Math.min(a.length, b.length) / d - max / d - 1; + if (n < 1000) return new double[] {-1, 0}; + float[] x = new float[n + max / d + 1], y = new float[n + max / d + 1]; + for (int i = 0; i < x.length; i++) { /* 11 kHz, the slope: the onsets */ + float sa = 0, sb = 0; + for (int k = 0; k < d; k++) { sa += a[i * d + k]; sb += b[i * d + k]; } + x[i] = sa; y[i] = sb; + } + for (int i = x.length - 1; i > 0; i--) { x[i] -= x[i - 1]; y[i] -= y[i - 1]; } + int best = 0; + double bv = -1e30, sum = 0; + double[] c = new double[max / d + 1]; + for (int l = 0; l <= max / d; l++) { + double v = 0; + for (int i = 1; i < n; i++) v += x[i] * y[i + l]; + c[l] = v; + sum += Math.abs(v); + if (v > bv) { bv = v; best = l; } + } + int fb = best * d; /* then the full rate, +-d */ + double fv = -1e30; + for (int l = Math.max(0, best * d - d); l <= best * d + d; l++) { + double v = 0; + for (int i = d; i + l < b.length && i < a.length - 1 && i < n * d; i++) + v += (a[i] - a[i - 1]) * (b[i + l] - b[i + l - 1]); + if (v > fv) { fv = v; fb = l; } + } + return new double[] {fb, sum > 0 ? bv / sum * (max / d + 1) / 8.0 : 0}; + } + + private void stemsIn() { + say("\n== 7 · MIC AL FM-1 Y VUELTA (STEMS+IN)"); + say("Para medir la latencia: FM-1 en MENU › SYSTEM › USB › STEMS+IN (firmware 0.8.0), sin auriculares, el celu a" + + " unos 20 cm del parlante del FM-1 y un tema con batería sonando. El mic oye al FM-1 y la app mide cuánto tarda en volver." + + " Bajale el LEVEL a la última pista activa (o muteala) para que el mic no acople por el parlante."); + UsbManager um = (UsbManager) getSystemService(Context.USB_SERVICE); + UsbDevice dev = null; + Object[] in = null, out = null; + for (UsbDevice dv : um.getDeviceList().values()) { + in = stream(dv, UsbConstants.USB_DIR_IN); + out = stream(dv, UsbConstants.USB_DIR_OUT); + if (in != null) { dev = dv; break; } + } + if (dev == null) { say("No veo el FM-1 grabando por USB."); return; } + if (out == null) { say("RESULTADO: el FM-1 no tiene salida USB en este modo. Poné MENU › SYSTEM › USB › STEMS+IN y reenchufá."); return; } + UsbInterface inOn = (UsbInterface) in[0], inOff = (UsbInterface) in[1], outOn = (UsbInterface) out[0], outOff = (UsbInterface) out[1]; + UsbEndpoint ep = (UsbEndpoint) in[2], epo = (UsbEndpoint) out[2]; + if (!permit(um, dev)) { say("RESULTADO: sin permiso USB no puedo."); return; } + UsbDeviceConnection conn = um.openDevice(dev); + if (conn == null) { say("RESULTADO: Android no me dejó abrir el dispositivo."); return; } + int ch = ep.getMaxPacketSize() / (46 * 2); + say("Entrada: " + ch + " canales (paquete " + ep.getMaxPacketSize() + " bytes); salida: EP 0x" + Integer.toHexString(epo.getAddress())); + if (ch != 10) { say("RESULTADO: espero 10 canales (STEMS+IN). ¿El FM-1 tiene el firmware 0.8.0 y está en STEMS+IN?"); conn.close(); return; } + AudioDeviceInfo micDev = null; + for (AudioDeviceInfo d : am.getDevices(AudioManager.GET_DEVICES_INPUTS)) + if (d.getType() == AudioDeviceInfo.TYPE_BUILTIN_MIC) { micDev = d; break; } + AudioRecord r = open(micDev, 1, false, MediaRecorder.AudioSource.UNPROCESSED); + if (r == null) r = open(micDev, 1, false, MediaRecorder.AudioSource.MIC); + if (r == null) { say("RESULTADO: no pude abrir el mic del celu."); conn.close(); return; } + final AudioRecord mic = r; + if (!conn.claimInterface(inOn, true) || !conn.claimInterface(outOn, true)) { + say("RESULTADO: no pude tomar las interfaces de audio."); mic.release(); conn.close(); return; + } + java.util.concurrent.atomic.AtomicBoolean stop = new java.util.concurrent.atomic.AtomicBoolean(); + Thread t = new Thread(() -> { + short[] b = new short[128]; + mic.startRecording(); + while (!stop.get()) { + int k = mic.read(b, 0, b.length); + if (k <= 0) break; + pushMic(b, k); + } + mic.stop(); + }); + try { + if (!conn.setInterface(inOn) || !conn.setInterface(outOn)) { say("RESULTADO: el FM-1 no aceptó arrancar el audio."); return; } + int secs = 10; + byte[] bytes = new byte[RATE * secs * ch * 2]; + int[] st = new int[9]; + t.start(); + say("Grabando " + secs + " s: el mic va al FM-1 (última pista activa) y vuelven los 10 canales…"); + long t0 = System.nanoTime(); + int got = duplex(conn.getFileDescriptor(), ep.getAddress(), ep.getMaxPacketSize(), epo.getAddress(), bytes, (secs + 5) * 1000, st); + double el = (System.nanoTime() - t0) / 1e9; + stop.set(true); + try { t.join(1000); } catch (InterruptedException ignored) { } + conn.setInterface(outOff); + conn.setInterface(inOff); + if (got <= 0) { say("RESULTADO: no llegó nada (error " + (-got) + ")."); return; } + int frames = got / (ch * 2); + say(String.format(Locale.ROOT, "Entrada: %d frames en %.2f s (%.0f/s), paquetes %d, vacíos %d, error %d.", + frames, el, frames / el, st[0], st[1], st[2])); + say(String.format(Locale.ROOT, "Salida: %d frames enviados (%.0f/s), %d en silencio por falta de mic, %d de mic descartados%s.", + st[6], st[6] / el, st[7], st[8], st[3] != 0 ? ", errno " + st[3] : "")); + short[] all = new short[frames * ch]; + for (int i = 0; i < all.length; i++) all[i] = (short) ((bytes[2 * i] & 0xFF) | (bytes[2 * i + 1] << 8)); + String[] names = {"T1", "T2", "T3", "T4", "T5", "T6", "FX L", "FX R", "MIC (mezclado)", "MIC (crudo)"}; + for (int c = 0; c < ch; c++) { + int pk = 0; + for (int i = c; i < all.length; i += ch) pk = Math.max(pk, Math.abs(all[i])); + say(String.format(Locale.ROOT, " %-15s pico %6.1f dBFS%s", names[c], pk > 0 ? 20 * Math.log10(pk / 32768.0) : -120, + pk > 64 ? "" : " (silencio)")); + } + float[] mix = new float[frames], raw = new float[frames]; + for (int i = 0; i < frames; i++) { + float m = 0; + for (int c = 0; c < 8; c++) m += all[i * ch + c]; + mix[i] = m; + raw[i] = all[i * ch + 9]; + } + double[] lg = lag(mix, raw, RATE / 5); + int L = (int) lg[0]; + say(String.format(Locale.ROOT, "LATENCIA: el sonido del FM-1 vuelve por el mic %d frames después = %.1f ms (confianza %.1f; > 3 es clara).", + L, L * 1000.0 / RATE, lg[1])); + String name = saveWav(all, all.length, ch); + say(name != null ? "WAV 10 canales: Descargas/JIANT/" + name : "No pude guardar el WAV."); + if (L > 0 && lg[1] > 3) { + short[] al = all.clone(); /* the two mic channels moved earlier by the latency */ + for (int i = 0; i < frames; i++) + for (int c = 8; c < 10; c++) al[i * ch + c] = i + L < frames ? all[(i + L) * ch + c] : 0; + String n2 = saveWav(al, al.length, ch); + say(n2 != null ? "WAV alineado (el mic corrido " + L + " frames): Descargas/JIANT/" + n2 : ""); + } + say(st[7] < RATE / 10 && st[2] == 0 && Math.abs(frames / el - RATE) < RATE * 0.02 + ? "RESULTADO: ida y vuelta estable. Escuchá el WAV alineado: el mic tiene que caer justo con el tema." + : "RESULTADO: anduvo con cortes (mirá silencios y errores). Mandame el informe."); + } finally { + stop.set(true); + mic.release(); + conn.releaseInterface(outOn); + conn.releaseInterface(inOn); + conn.close(); + say("(Si después el audio USB normal no anda, desenchufá y volvé a enchufar el FM-1.)"); + } + } + // ------------------------------------------------------------------ 3 · two USB inputs at once private void twoInputs() { say("\n== 3 · FM-1 + MIC USB A LA VEZ");