Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ releasing.
is said out loud; nothing is lost. At most one quiz a minute (adjustable), never during an item
challenge. Works on any server.
- New settings: "Quizzes while playing" and "Time between quizzes".
- **Linux: the microphone chosen in PipeWire or PulseAudio** is the one that listens. With
"Microphone" left empty, the mod follows the system's default input, and `/langmod mics` and the
settings list the inputs by the names your desktop shows (accents don't matter when typing one).
Before, the mod only saw the raw sound cards and often listened to an input with nothing plugged
in. A "Microphone" saved from the old list (such as `PCH`) now falls back to the default: pick it
again. Names said out loud would go the same way, to the output chosen in Minecraft's sound
settings (no voices on Linux for the moment). Windows and macOS are unchanged.

## 0.2.0

Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ also work on servers without the mod.
- Windows x64, Linux x64 or macOS. On Windows: a CPU with AVX2 (Intel Core from 2013 on, any AMD
Ryzen; some budget Pentium/Celeron chips lack it) and the Microsoft Visual C++ 2015-2022 runtime, which almost every gaming PC already has
(otherwise: <https://aka.ms/vs/17/release/vc_redist.x64.exe>)
- On Linux: PipeWire (with pipewire-pulse or on its own) or PulseAudio, which nearly every desktop
runs. The mod reaches them through OpenAL, the sound library that comes with Minecraft. Without
either, it falls back to the raw ALSA sound cards.
- About 0.5 GB of disk space for the Whisper model (downloaded separately, see below)
- For hearing names said out loud: Windows 10 or 11 with a voice for the language (see
[Hearing the name](#hearing-the-name))
Expand Down Expand Up @@ -296,8 +299,10 @@ the `.en` ones are English only.

In a world, run `/langmod test`, then say something in the language you are learning (Mandarin
until you choose another one). Chat shows what Whisper heard, its pinyin or romanization and how
long it took. If nothing is detected, `/langmod mics` lists the microphones Java can
see; pick one in the settings ("Microphone").
long it took. If nothing is detected, `/langmod mics` lists the microphones the mod can use; pick one
in the settings ("Microphone"). Left empty, it uses the system's default microphone: on Linux, the
default input chosen in PipeWire or PulseAudio (your desktop's sound settings, `pavucontrol` or
`wpctl set-default`), listed under the names they show there.

### Using it in your normal Minecraft

Expand All @@ -312,7 +317,7 @@ default launcher: `%APPDATA%\.minecraft\config\langmod\models`).
| Command | What it does |
|---|---|
| `/langmod test` | Records one utterance and prints what Whisper heard (text, pinyin or romanization, timing). |
| `/langmod mics` | Lists the microphones. |
| `/langmod mics` | Lists the microphones (on Linux, the PipeWire or PulseAudio inputs). |
| `/langmod language` | Opens the menu for choosing the language to learn (also **Practice Language** in the game menu). |
| `/langmod items` | Opens the items list, where items can be turned off (also **Items** in the game menu). |
| `/langmod voices` | Looks for the voices installed in Windows again and lists the one that says each language's names. |
Expand Down Expand Up @@ -360,7 +365,7 @@ screen. The file can also be edited by hand (then run `/langmod reload`).
| Use GPU (`useGpu`) | on | Only used by the `server` engine. |
| Whisper window (`audioContext`) | 512 = 10 s | Audio Whisper processes per answer, in 20 ms frames: `0` = its full 30 s window, `-1` = just the clip (fastest, much less accurate). See the measurements below. |
| CPU threads (`whisperThreads`) | 0 = auto | Auto uses half your cores (at most 8), so the game keeps running smoothly. |
| Microphone (`micDevice`) | system default | Part of a device name from `/langmod mics`. |
| Microphone (`micDevice`) | system default | Part of a device name from `/langmod mics` (any case, accents don't matter). Empty: the system's default microphone, on Linux the default input in PipeWire or PulseAudio. |
| Speech threshold (`vadThresholdDb`) | 12 | How much louder than background noise (dB) counts as speech. |
| Tone colors (`toneColors`) | red, green, blue, purple, grey | Mandarin tones 1-4 and neutral, as `#RRGGBB` (shown in the settings when learning Mandarin). |
| Name size (`hanziScale`) | 3 | Size of the name in the popup; long names are shrunk to fit. |
Expand Down Expand Up @@ -507,6 +512,15 @@ popup shows when it is. Audio is recorded as 16 kHz mono and split into utteranc
voice activity detector (it adapts to background noise; 0.8 s of silence ends a recording, 5 s
maximum).

On Windows and macOS the microphone is opened with Java Sound. On Linux, Java Sound only sees the raw
ALSA sound cards: not PipeWire or PulseAudio, not the default input chosen there, and not a card
they are using. So on Linux the mod records through OpenAL Soft, the sound library Minecraft
already uses, which talks to PipeWire or PulseAudio. Their "Monitor of ..." inputs (which record
what is playing) are left out of the list. If OpenAL can't open a microphone, Java Sound is tried.
The log says which one was used: `Microphone open: <name> @ 16000 Hz (OpenAL)` or `(Java Sound)`.
A voice saying a name would play the same way, on the output device chosen in Minecraft's sound
settings, in its own OpenAL context that never touches the game's sound.

Whisper gets these hints by default, chosen from the measurements below:

- A **10 second window** instead of Whisper's usual 30 seconds (it always processes a fixed window,
Expand Down Expand Up @@ -767,6 +781,12 @@ the WAV files; the runner only has English voices, and others cannot be added to
"Speech threshold" if you speak quietly, raise it if background noise starts recordings. Windows
must allow desktop apps to use the microphone (Settings > Privacy (& security) > Microphone >
"Let desktop apps access your microphone").
- **Linux: the wrong microphone, or none**: `/langmod mics` lists the PipeWire or PulseAudio inputs.
Leave "Microphone" empty to use the default input (change it in your desktop's sound settings,
`pavucontrol` or with `wpctl set-default`), or pick one. A "Microphone" saved by an older version
(a sound card name such as `PCH`) no longer matches: the log says so and the default is used, so
pick it again. If the log says `Microphone open: ... (Java Sound)`, OpenAL could not reach the
sound server: check that PipeWire or PulseAudio is running.
- **Recordings start but are cut off**: raise `vadSilenceMs`.
- **"Transcription took longer than 10 s"** (not counted as an attempt): use a smaller model
(`base`), give Whisper more threads, use the `server` engine with a GPU, or as a last resort set
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package io.github.dylantopic.langmod.client.audio;

import java.util.ArrayList;
import java.util.List;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;

import io.github.dylantopic.langmod.LangMod;
import io.github.dylantopic.langmod.core.audio.AudioDevices;

/** A microphone through Java Sound: the system's devices on Windows and macOS, raw ALSA cards on Linux. */
final class JavaSoundSource implements MicSource {
private static final AudioFormat[] FORMATS = {
new AudioFormat(16_000f, 16, 1, true, false),
new AudioFormat(48_000f, 16, 1, true, false),
new AudioFormat(44_100f, 16, 1, true, false),
new AudioFormat(48_000f, 16, 2, true, false),
new AudioFormat(44_100f, 16, 2, true, false),
};

private final TargetDataLine line;
private final String name;

private JavaSoundSource(TargetDataLine line, String name) {
this.line = line;
this.name = name;
}

/** @param preferred part of a device name, or empty for the system default */
static JavaSoundSource open(String preferred) throws LineUnavailableException {
LineUnavailableException last = null;

for (Mixer.Info mixer : candidateMixers(preferred)) {
for (AudioFormat format : FORMATS) {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

try {
TargetDataLine line;
String name;

if (mixer == null) {
if (!AudioSystem.isLineSupported(info)) {
continue;
}

line = (TargetDataLine) AudioSystem.getLine(info);
name = "system default";
} else {
Mixer m = AudioSystem.getMixer(mixer);

if (!m.isLineSupported(info)) {
continue;
}

line = (TargetDataLine) m.getLine(info);
name = mixer.getName();
}

// About 100 ms of buffer; we read in 20 ms chunks.
int frameBytes = format.getFrameSize();
line.open(format, Math.max(frameBytes * (int) (format.getSampleRate() / 10), 4096));
line.start();
return new JavaSoundSource(line, name);
} catch (LineUnavailableException | IllegalArgumentException | SecurityException e) {
last = e instanceof LineUnavailableException lue ? lue : new LineUnavailableException(e.getMessage());
}
}
}

throw last != null ? last : new LineUnavailableException("No microphone found");
}

/** Names of all devices Java Sound can record from. */
static List<String> deviceNames() {
List<String> names = new ArrayList<>();

for (Mixer.Info info : AudioSystem.getMixerInfo()) {
if (supportsCapture(info)) {
names.add(info.getName());
}
}

return names;
}

@Override
public String name() {
return name;
}

@Override
public int sampleRate() {
return (int) line.getFormat().getSampleRate();
}

@Override
public int channels() {
return line.getFormat().getChannels();
}

@Override
public int read(byte[] buffer) {
return line.isOpen() ? line.read(buffer, 0, buffer.length) : -1;
}

@Override
public void close() {
line.stop();
line.close();
}

private static List<Mixer.Info> candidateMixers(String preferred) {
List<Mixer.Info> result = new ArrayList<>();

if (preferred != null && !preferred.isBlank()) {
for (Mixer.Info info : AudioSystem.getMixerInfo()) {
if (AudioDevices.matches(preferred, info.getName() + " " + info.getDescription()) && supportsCapture(info)) {
result.add(info);
}
}

if (result.isEmpty()) {
LangMod.LOGGER.warn("No microphone matching '{}' (have: {}); using the default", preferred, deviceNames());
}
}

result.add(null); // system default
return result;
}

private static boolean supportsCapture(Mixer.Info info) {
try {
return AudioSystem.getMixer(info).isLineSupported(new Line.Info(TargetDataLine.class));
} catch (RuntimeException e) {
return false;
}
}
}
Loading