From 5b0ad7fcc126609cdf8dafc00c3210dcf0c0acda Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 16 Oct 2026 04:28:02 +0000 Subject: [PATCH] Probe 0.2: count the channels with sound, try other opens, read the FM-1 directly On an S23 Ultra Android opened the FM-1's 8-channel USB input but passed only T1 and T2 (the rest zero), and test 2 still reported success. Test 2 now counts the channels that carry sound; 2b tries sources x 8/6 channels; 5 reads the stream itself (usbfs isochronous reads in a small JNI library, on the USB permission the user grants). One debug key for every build. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh --- android/probe/README.md | 7 + android/probe/app/build.gradle | 19 +- android/probe/app/probe-debug.keystore | Bin 0 -> 2618 bytes android/probe/app/src/main/cpp/CMakeLists.txt | 4 + android/probe/app/src/main/cpp/usbiso.c | 101 +++++++++ .../java/fm/jiant/probe/MainActivity.java | 193 +++++++++++++++++- 6 files changed, 313 insertions(+), 11 deletions(-) create mode 100644 android/probe/app/probe-debug.keystore create mode 100644 android/probe/app/src/main/cpp/CMakeLists.txt create mode 100644 android/probe/app/src/main/cpp/usbiso.c diff --git a/android/probe/README.md b/android/probe/README.md index 0755fb13..d35f952a 100644 --- a/android/probe/README.md +++ b/android/probe/README.md @@ -10,6 +10,13 @@ Phase 0 of the stems app (the design: one Android screen that answers, on a give USB driver of its own. 3. **The FM-1 and a USB microphone at once** (through a USB-C hub): whether Android opens two USB audio inputs together. 4. **The phone's microphone into the FM-1** with the FM-1 in USB > IN+OUT: 5 s of the mic played to the USB output. +5. **Our own driver** (0.2): Android's framework hands the FM-1's eight channels on as two on some phones (an S23 + Ultra: T1 T2, the rest zero). This reads the stream itself, as the multichannel recorder apps do: asks for the + USB permission, claims the streaming interface and does isochronous reads through usbfs (`app/src/main/cpp/usbiso.c`), + 10 s, with the per-channel levels, the frames a second and a WAV. Test **2b** tries the other ways of opening the + input (sources × 8/6 channels) and shows which channels carry sound. + +Every build signs with `app/probe-debug.keystore` (a debug key, public on purpose) so a new APK installs over the last. Everything goes into a plain-text report (COPY / SHARE). Built by GitHub Actions (.github/workflows/emulator.yml for the site, android.yml on the branches): `gradle -p android/probe assembleDebug`. No dependencies beyond the Android diff --git a/android/probe/app/build.gradle b/android/probe/app/build.gradle index 9a1b2e3c..63851c58 100644 --- a/android/probe/app/build.gradle +++ b/android/probe/app/build.gradle @@ -8,8 +8,23 @@ android { applicationId "fm.jiant.probe" minSdk 29 // (MediaStore.Downloads with no storage permission) targetSdk 34 - versionCode 1 - versionName "0.1" + versionCode 2 + versionName "0.2" + } + signingConfigs { + debug { // (one key for every build: a new APK installs over the last one) + storeFile file("probe-debug.keystore") + storePassword "android" + keyAlias "androiddebugkey" + keyPassword "android" + } + } + buildTypes { + debug { signingConfig signingConfigs.debug } + } + ndkVersion "27.2.12479018" + externalNativeBuild { + cmake { path "src/main/cpp/CMakeLists.txt" } } compileOptions { sourceCompatibility JavaVersion.VERSION_17 diff --git a/android/probe/app/probe-debug.keystore b/android/probe/app/probe-debug.keystore new file mode 100644 index 0000000000000000000000000000000000000000..77e7612f9d15980888953e085e56492f8a2b6cf3 GIT binary patch literal 2618 zcma)8X*d)L7oHij7`sc^t|cP7874z0M22wfYiJl-B7})l8q2k0&%VTzEn^uomuwAU zN|x*f2sEzRYH38v7I3hIu zX*m!IVE$2QU+Fn3u631N{s1+P^BGN=ClW!RGdv)m1cC#~`tOY(I0S$Yg~G4KVSt`2 zV4yshN7r17gv@yJBI0^jVe(GB0+;|E`a%s+zUe;?(U!hNKieSX>CUn_-mYih;*ilp zPm)qD_M$k3Zt;+%?xLijDwb_y5Y^by%-`LfRhxOtwhdetK@f85qj$<5DZbC);cFr+ z9tc1`L|<5R6Sh2uo0+bvnz|xzX<@tXW$~o+)1M!htS}@wUg?6pP;XGz zgl5f|*~Ep|c6lCn8pcz}#XYOI>b6{i(3Tv{TI))uVw?xOKPx+lEooskw97C>y0oW!3R-a&=rLu&$@X^Z#7@N+GBT(j@k&gGlBWe zi0wON05NweH_+HaZCpzmq`NMWIOb(?xD4XA?Vr&-OpL2Coe&CE67xnlnDhKvDsj37 zm%M@V(+mnP_#@er~wH}j?d%W1^&4kNZw)T5YY2j^Nvue@hf= zyZ4L-U-l!7crz^rYtY8{C}c(WXenla7L{qaNd-*e6~^|=MIz@b=z8IYLeaXk_ybtW z2Gr;_sYsH_p4N^WV=AX${g$`}_a9JRW(;*w%$K|TWE zh;AU8d+LpL6aGrr$6zn2)e+tW+`o2}#(2D=A@*G2#2^se;G(12oH zHgn{i9OwBH*Y7X|a=2W=*p^@AZAOYzG-Q&gd0mCo;Y~&M#yH$zQC-UG2{Cco(dBHi z=2THA^>D;)pgw(YG^j4$MXfP0R!>rJ3=uS#dTZC?;x?Dv)94v=mjDl2Xm1~WxqChu z(HkCN0do+JS(d!i_kLU4Ddt&~ep|Cc1FF&@2lD2!%B$>I=*+`9DC-S3QoO1=P3Fe^ zC|46A5v>ml=66rx7GmG}l#OnG(mb0kC|R^7Rvx=Cyi}u34aM1W z_;dI9kX8t#Uv>%SM@oo^0kD7o01glgxB>7z&NzT4z#R|@@CNuDSK)|L|Hw}u`N6;0 z`1*TFAkmttNL3VC1F5coMi9WjUm_T^gut?iAh4`~fPmxl`5y-Ozl__RD>KjSpq8Z5 zLH?;jeL>LyuD<_&jC?%C58o+80)`(BiYD1L&Izj#lc zrGB%0T;%qee~`L@%}1gP?>eX+6dzKnZd!hFmQcbzbt4IuI*AVqeJt-3{1c$soZ;l& zp&HH2DZUtM5We3P*v6H5rD@o5Gv8R0C}#p_KQ(0HXlGq)-GE7XClRo&=+mNf8a~Q2 zksn#@L2kx<0-cnaUKQKJ68ho-yl8j7@~n%3-Z+kXx6+w0;dwR z#X4@Tr96m0wHBGUqEGTgLMeJl&oa}d37UWU1~hIy9)G<_+v2F3cyfie;Uix2VhG2+ z%d$&h@Tk(7bsw|3aG7<-PfL4|I7$nakQbY#yXLE@x?el@;QPuQTf-P%UG}G&Nxg;H2hrhX{9k@+vJ`Bo zbzR!~6UwM6K@8}JWLZbCRG6%Vc^<@wy3Pc$_wl{|YwFrq$Q=kdIgz((Q?ffz@E(>D8cZ=n7mbAcXi7hLHyb1u3M@_DM2rBNObU&EIDU{o&v~v4iIvG{vY(bwIvq)6De{|?r zpk04rFR^T0rTnEbe^Es@v4LyJ>GlJM=g!`~!Tj->I!>rTwCc)pF7qYuGVMBzMZEYj zep}ApNn`Z=tJTBMJ!RKvjuVlN(mQ;0>Rx7;RD>A +#include +#include +#include +#include +#include +#include + +#define NURB 8 /* URBs in flight */ +#define NPK 8 /* packets (1 ms each at full speed) an URB */ + +static long long now_ms(void) +{ + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (long long)t.tv_sec * 1000 + t.tv_nsec / 1000000; +} + +static void setup(struct usbdevfs_urb *u, int ep, int maxp) +{ + u->type = USBDEVFS_URB_TYPE_ISO; + u->endpoint = (unsigned char)ep; + u->flags = USBDEVFS_URB_ISO_ASAP; + u->buffer_length = NPK * maxp; + u->number_of_packets = NPK; + u->status = 0; + u->actual_length = 0; + u->error_count = 0; + for (int k = 0; k < NPK; k++) { + u->iso_frame_desc[k].length = (unsigned)maxp; + u->iso_frame_desc[k].actual_length = 0; + u->iso_frame_desc[k].status = 0; + } +} + +/* fills out (bytes, as the device sends them) until it is full or timeout_ms passes; stats: packets, empty packets, + * packet errors, errno of a failed ioctl (0 none), the largest packet. Returns the bytes read, or -errno */ +JNIEXPORT jint JNICALL Java_fm_jiant_probe_MainActivity_isoRead(JNIEnv *env, jclass cls, jint fd, jint ep, jint maxp, + 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); + struct usbdevfs_urb *u[NURB]; + int got = 0, packets = 0, empty = 0, perr = 0, err = 0, big = 0, live = 0; + for (int i = 0; i < NURB; i++) { + u[i] = calloc(1, sz); + u[i]->buffer = malloc((size_t)NPK * maxp); + setup(u[i], ep, maxp); + if (ioctl(fd, USBDEVFS_SUBMITURB, u[i]) < 0) { err = errno; break; } + 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, 1000000}; nanosleep(&s, NULL); continue; } + if (errno == EINTR) continue; + err = errno; + break; + } + live--; + 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); /* iso packets sit at their requested offsets */ + 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]); + long long stop = now_ms() + 500; + while (live > 0 && now_ms() < stop) { /* every URB back before its memory goes */ + 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]); } /* (else leak: the kernel may still write) */ + (*env)->ReleaseByteArrayElements(env, out, dst, 0); + jint st[6] = {packets, empty, perr, err, big, live}; + (*env)->SetIntArrayRegion(env, stats, 0, 6, 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 1d58084a..82662247 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 @@ -4,18 +4,30 @@ // 44.1 kHz; can it open a USB microphone at the same time as the FM-1; can it play into the FM-1 (USB > IN+OUT); does // it see the FM-1's USB-MIDI. Everything it finds goes into a plain-text report to copy or share, and the 8-channel // test is saved as a WAV in Downloads/JIANT. +// (0.2) Android's audio framework hands an 8-channel USB input on as two channels on some phones (an S23 Ultra: T1 T2, +// the rest zero), so test 2 now counts the channels that carry sound, 2b tries the other ways of opening the input, and +// 5 reads the FM-1's stream itself (usbiso.c: usbfs isochronous reads on the USB permission the user grants). package fm.jiant.probe; import android.Manifest; import android.app.Activity; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; import android.content.ClipData; import android.content.ClipboardManager; import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.PackageManager; import android.graphics.Color; import android.graphics.Typeface; +import android.hardware.usb.UsbConstants; +import android.hardware.usb.UsbDevice; +import android.hardware.usb.UsbDeviceConnection; +import android.hardware.usb.UsbEndpoint; +import android.hardware.usb.UsbInterface; +import android.hardware.usb.UsbManager; import android.media.AudioDeviceInfo; import android.media.AudioFormat; import android.media.AudioManager; @@ -45,6 +57,8 @@ import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Locale; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; public class MainActivity extends Activity { private static final int RATE = 44100; @@ -56,6 +70,10 @@ public class MainActivity extends Activity { private AudioManager am; private volatile boolean busy; + 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); + @Override protected void onCreate(Bundle b) { super.onCreate(b); @@ -84,8 +102,10 @@ public class MainActivity extends Activity { root.addView(button("1 · DISPOSITIVOS", v -> run(this::listDevices))); root.addView(button("2 · GRABAR 10 s DE LOS 8 CANALES", v -> run(this::recordStems))); + root.addView(button("2b · ¿CUÁNTOS CANALES PASA ANDROID? (20 s)", v -> run(this::scanOpens))); root.addView(button("3 · FM-1 + MIC USB A LA VEZ (3 s)", v -> run(this::twoInputs))); 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))); LinearLayout row = new LinearLayout(this); row.setOrientation(LinearLayout.HORIZONTAL); Button copy = button("COPIAR INFORME", v -> copyReport()); @@ -105,7 +125,7 @@ public class MainActivity extends Activity { root.addView(sv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1)); setContentView(root); - say("JIANT Probe 0.1 · " + Build.MANUFACTURER + " " + Build.MODEL + " · Android " + Build.VERSION.RELEASE + say("JIANT Probe 0.2 · " + 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); @@ -237,11 +257,15 @@ public class MainActivity extends Activity { // ------------------------------------------------------------------ 2 · eight channels /** opens an AudioRecord of n channels (index mask) on device d; null when Android refuses it */ private AudioRecord open(AudioDeviceInfo d, int n, boolean indexMask) { + int src = "true".equals(am.getProperty(AudioManager.PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED)) + ? MediaRecorder.AudioSource.UNPROCESSED : MediaRecorder.AudioSource.MIC; + return open(d, n, indexMask, src); + } + + private AudioRecord open(AudioDeviceInfo d, int n, boolean indexMask, int src) { AudioFormat.Builder fb = new AudioFormat.Builder().setEncoding(AudioFormat.ENCODING_PCM_16BIT).setSampleRate(RATE); if (indexMask) fb.setChannelIndexMask((1 << n) - 1); else fb.setChannelMask(n == 1 ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO); - int src = "true".equals(am.getProperty(AudioManager.PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED)) - ? MediaRecorder.AudioSource.UNPROCESSED : MediaRecorder.AudioSource.MIC; try { AudioRecord r = new AudioRecord.Builder().setAudioSource(src).setAudioFormat(fb.build()) .setBufferSizeInBytes(RATE / 4 * n * 2).build(); @@ -291,6 +315,18 @@ public class MainActivity extends Activity { r.stop(); r.release(); say(String.format(Locale.ROOT, "Leí %d frames en %.2f s (%d lecturas, %d cortas).", got / n, secs, reads, shorts)); + int live = levels(all, got, n, true); + say("Canales con señal: " + live + " de " + n + "."); + String name = saveWav(all, got, n); + say(name != null ? "WAV guardado: Descargas/JIANT/" + name : "No pude guardar el WAV."); + say(live >= 6 ? "RESULTADO: este teléfono pasa " + live + " canales con sonido por la API normal." + : live > 0 ? "RESULTADO: Android abrió " + n + " canales pero solo " + live + " traen sonido (si sonaban más pistas," + + " Android pasa solo los primeros). Probá 2b y 5." + : "RESULTADO: no llegó sonido. ¿Sonaba el FM-1?"); + } + + /** per-channel peaks and rms of interleaved pcm (count samples, n channels); the channels that carry sound */ + private int levels(short[] all, int got, int n, boolean print) { int live = 0; for (int c = 0; c < n; c++) { int pk = 0; @@ -303,13 +339,53 @@ public class MainActivity extends Activity { double rms = Math.sqrt(e / Math.max(1, got / n)); double db = pk > 0 ? 20 * Math.log10(pk / 32768.0) : -120; if (pk > 64) live++; - say(String.format(Locale.ROOT, " canal %d: pico %6.1f dBFS rms %7.1f%s", c + 1, db, rms, pk > 64 ? "" : " (silencio)")); + if (print) + say(String.format(Locale.ROOT, " canal %d: pico %6.1f dBFS rms %7.1f%s%s", c + 1, db, rms, + pk > 64 ? "" : " (silencio)", pk >= 32767 ? " (¡satura!)" : "")); } - say("Canales con señal: " + live + " de " + n + "."); - String name = saveWav(all, got, n); - say(name != null ? "WAV guardado: Descargas/JIANT/" + name : "No pude guardar el WAV."); - say(n >= 8 ? "RESULTADO: este teléfono abre los 8 canales por la API normal. La app puede usar Oboe/AAudio." - : "RESULTADO: Android dio solo " + n + " canales. La app necesita su propio driver USB para los 8."); + return live; + } + + // ------------------------------------------------------------------ 2b · the ways of opening the input + private void scanOpens() { + say("\n== 2b · ¿CUÁNTOS CANALES PASA ANDROID?"); + AudioDeviceInfo d = findUsbInput(true); + if (d == null) { say("No veo ninguna entrada USB. ¿El FM-1 está enchufado y en USB › STEMS?"); return; } + say("Dejá sonando las 6 pistas. Cada prueba graba 2 s."); + say("Máscaras índice que declara: " + hexes(d.getChannelIndexMasks()) + " canales: " + Arrays.toString(d.getChannelCounts())); + int[] srcs = {MediaRecorder.AudioSource.UNPROCESSED, MediaRecorder.AudioSource.MIC, MediaRecorder.AudioSource.CAMCORDER, + MediaRecorder.AudioSource.VOICE_RECOGNITION}; + String[] names = {"UNPROCESSED", "MIC", "CAMCORDER", "VOICE_RECOG"}; + int best = 0; + String bestHow = ""; + for (int si = 0; si < srcs.length; si++) + for (int want : new int[] {8, 6}) { + AudioRecord r = open(d, want, true, srcs[si]); + if (r == null) { say(String.format(Locale.ROOT, " %-11s %d ch: no abre", names[si], want)); continue; } + int n = r.getChannelCount(); + short[] all = new short[RATE * 2 * n]; + r.startRecording(); + int got = 0; + long t0 = System.nanoTime(); + while (got < all.length && System.nanoTime() - t0 < 5_000_000_000L) { + int k = r.read(all, got, Math.min(RATE / 20 * n, all.length - got)); + if (k <= 0) break; + got += k; + } + r.stop(); + r.release(); + int live = levels(all, got, n, false); + StringBuilder which = new StringBuilder(); + for (int c = 0; c < n; c++) { + int pk = 0; + for (int i = c; i < got; i += n) pk = Math.max(pk, Math.abs(all[i])); + which.append(pk > 64 ? (char) ('1' + c) : '·'); + } + say(String.format(Locale.ROOT, " %-11s %d ch: %d con sonido [%s]", names[si], want, live, which)); + if (live > best) { best = live; bestHow = names[si] + " " + want + " ch"; } + } + say(best >= 6 ? "RESULTADO: " + bestHow + " pasa " + best + " canales: la app puede usar la API normal así." + : "RESULTADO: ninguna forma pasa más de " + best + " canales con sonido. Para los 8 hace falta el driver propio (5)."); } private String saveWav(short[] pcm, int count, int ch) { @@ -344,6 +420,105 @@ public class MainActivity extends Activity { private static void le16(byte[] h, int o, int v) { h[o] = (byte) v; h[o + 1] = (byte) (v >> 8); } private static void le32(byte[] h, int o, int v) { le16(h, o, v); le16(h, o + 2, v >> 16); } + // ------------------------------------------------------------------ 5 · our own driver + private static final String USB_PERM = "fm.jiant.probe.USB"; + + /** asks the user for the device (Android's dialog); true when granted */ + private boolean permit(UsbManager um, UsbDevice dev) { + if (um.hasPermission(dev)) return true; + CountDownLatch done = new CountDownLatch(1); + BroadcastReceiver rx = new BroadcastReceiver() { + @Override public void onReceive(Context c, Intent i) { done.countDown(); } + }; + IntentFilter f = new IntentFilter(USB_PERM); + if (Build.VERSION.SDK_INT >= 33) registerReceiver(rx, f, Context.RECEIVER_NOT_EXPORTED); + else registerReceiver(rx, f); + PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(USB_PERM).setPackage(getPackageName()), + PendingIntent.FLAG_MUTABLE); + um.requestPermission(dev, pi); + say("Aceptá el permiso USB que muestra Android…"); + try { done.await(60, TimeUnit.SECONDS); } catch (InterruptedException ignored) { } + unregisterReceiver(rx); + return um.hasPermission(dev); + } + + private void directStems() { + say("\n== 5 · DRIVER PROPIO (8 CANALES DIRECTO)"); + UsbManager um = (UsbManager) getSystemService(Context.USB_SERVICE); + UsbDevice dev = null; + UsbInterface on = null, off = null; + UsbEndpoint ep = null; + for (UsbDevice dv : um.getDeviceList().values()) { + 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() == UsbConstants.USB_DIR_IN + && on == null) { on = it; ep = x; dev = dv; } + } + } + if (dev != null) { + for (int i = 0; i < dv.getInterfaceCount(); i++) { + UsbInterface it = dv.getInterface(i); + if (it.getId() == on.getId() && it.getAlternateSetting() == 0) off = it; + } + break; + } + } + if (dev == null) { say("No veo un dispositivo USB que grabe audio. ¿El FM-1 está enchufado y en USB › STEMS?"); return; } + say(String.format(Locale.ROOT, "Dispositivo: %s (%04x:%04x), interfaz %d alt %d, EP 0x%02x, paquete máx %d bytes", + dev.getProductName(), dev.getVendorId(), dev.getProductId(), on.getId(), on.getAlternateSetting(), + ep.getAddress(), ep.getMaxPacketSize())); + if (!permit(um, dev)) { say("RESULTADO: sin permiso USB no puedo leerlo."); return; } + UsbDeviceConnection conn = um.openDevice(dev); + if (conn == null) { say("RESULTADO: Android no me dejó abrir el dispositivo."); return; } + int ch = 0, sub = 2; + byte[] raw = conn.getRawDescriptors(); /* the format of our interface: type I, channels, bytes a sample */ + int curIf = -1, curAlt = -1; + for (int i = 0; raw != null && i + 1 < raw.length && raw[i] > 0; i += raw[i] & 0xFF) { + int len = raw[i] & 0xFF, type = raw[i + 1] & 0xFF; + if (type == 4 && len >= 9) { curIf = raw[i + 2] & 0xFF; curAlt = raw[i + 3] & 0xFF; } + else if (type == 0x24 && len >= 8 && raw[i + 2] == 2 && curIf == on.getId() && curAlt == on.getAlternateSetting()) { + ch = raw[i + 4] & 0xFF; + sub = raw[i + 5] & 0xFF; + } + } + say("Formato: " + ch + " canales, " + (sub * 8) + " bit."); + if (ch == 0 || sub != 2) { say("RESULTADO: no entiendo el formato."); conn.close(); return; } + if (!conn.claimInterface(on, true)) { say("RESULTADO: no pude tomar la interfaz de audio."); conn.close(); return; } + try { + if (!conn.setInterface(on)) { say("RESULTADO: el FM-1 no aceptó arrancar el audio (SET_INTERFACE)."); return; } + conn.controlTransfer(0x22, 0x01, 0x0100, ep.getAddress(), new byte[] {0x44, (byte) 0xAC, 0x00}, 3, 500); + int secs = 10; + byte[] bytes = new byte[RATE * secs * ch * 2]; + int[] st = new int[6]; + say("Grabando " + secs + " s directo del FM-1…"); + long t0 = System.nanoTime(); + int got = isoRead(conn.getFileDescriptor(), ep.getAddress(), ep.getMaxPacketSize(), bytes, (secs + 5) * 1000, st); + double el = (System.nanoTime() - t0) / 1e9; + if (off != null) conn.setInterface(off); + if (got <= 0) { say("RESULTADO: no llegó nada (error " + (-got) + ", paquetes " + st[0] + ")."); return; } + int frames = got / (ch * 2); + say(String.format(Locale.ROOT, "Leí %d frames en %.2f s (%.0f frames/s). Paquetes %d, vacíos %d, con error %d, el más grande %d bytes%s.", + frames, el, frames / el, st[0], st[1], st[2], st[4], 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)); + int live = levels(all, all.length, ch, true); + say("Canales con señal: " + live + " de " + ch + "."); + String name = saveWav(all, all.length, ch); + say(name != null ? "WAV guardado: Descargas/JIANT/" + name : "No pude guardar el WAV."); + boolean steady = Math.abs(frames / el - RATE) < RATE * 0.02 && st[2] == 0; + say(live >= 6 && steady ? "RESULTADO: el driver propio lee los " + ch + " canales completos y estables. Es el camino para la app." + : live >= 6 ? "RESULTADO: llegan los " + ch + " canales, pero el flujo no fue parejo (mirá frames/s y errores)." + : "RESULTADO: llegaron " + live + " canales con sonido. ¿Sonaban las 6 pistas?"); + } finally { + conn.releaseInterface(on); + 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");