aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md27
-rw-r--r--shell.nix2
-rw-r--r--src/main.zig36
3 files changed, 50 insertions, 15 deletions
diff --git a/README.md b/README.md
index 691aec5..0a1c88b 100644
--- a/README.md
+++ b/README.md
@@ -2,16 +2,29 @@
Nome provisório pq é um projeto de áudio e usa zig
-Ideia:
+## Ideia
+
- Tocador de música
- Visualizador de espectro de frequência
-MVP:
-- Toca só WAV
-- Play/pause
-- Barra de progresso
+## MVP
+
+- [X] Toca WAV
+- [X] Play/pause
+- [ ] Barra de progresso
+
+## Próximos passos (em ordem):
+
+- [ ] Interface básica
+ - [ ] Metadados da música: título, álbum, artista
+ - [ ] Duração atualizada a cada segundo (formato: X:XX/Y:YY)
+ - [ ] Mudar o número de dígitos de acordo com duração da música (talvez dê pra fazer de primeira)
+- [ ] Ler arquivos enviados como argumentos pro programa
+- [ ] Ler arquivos .mp3 (talvez seja mais difícil)
+
+## Problemas:
-Problemas:
- [X] SDL espera uma janela pra capturar input. Sem janela, sem input pelo terminal!
- [X] Tentar utilizar funções normais de pegar input
-- [ ] A captura de keypresses da stdin espera um caractere '\n' pra ler. Como não precisar mais disso?
+- [X] A captura de keypresses da stdin espera um caractere '\n' pra ler. Como não precisar mais disso?
+ - Resposta: [Entering raw mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html#a-timeout-for-read)
diff --git a/shell.nix b/shell.nix
index 4e37ca9..8ac1b30 100644
--- a/shell.nix
+++ b/shell.nix
@@ -2,7 +2,7 @@ let
sources = import ./npins;
pkgs = import sources.nixpkgs {};
in
- pkgs.mkShellNoCC {
+ pkgs.mkShell {
packages = with pkgs; [
just
zig
diff --git a/src/main.zig b/src/main.zig
index d826539..0e08def 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,16 +1,16 @@
const sdl3 = @import("sdl3");
const std = @import("std");
+const posix = std.posix;
const io = std.Io;
-/// Frequency of the generated tone, in Hz (440 Hz is concert A).
-const tone_hz = 440;
-/// Amplitude of the generated tone, from 0.0 (silence) to 1.0 (full volume).
-const tone_amplitude = 0.3;
/// How long to sleep between event-loop iterations, in milliseconds.
const poll_interval_ms = 50;
const autumn_trees = "assets//autumn_trees.wav";
+// isso pra sistemas linux
+const STDIN_FD: posix.fd_t = 0;
+
pub fn main(
init: std.process.Init,
) !void {
@@ -18,8 +18,8 @@ pub fn main(
const log_app = sdl3.log.Category.application;
- try sdl3.init(.{ .audio = true, .events = true });
- defer sdl3.quit(.{ .audio = true, .events = true });
+ try sdl3.init(.{ .audio = true });
+ defer sdl3.quit(.{ .audio = true });
try sdl3.mixer.init();
defer sdl3.mixer.quit();
@@ -42,12 +42,31 @@ pub fn main(
try track.play(.{});
+ // estado original do terminal
+ const termios_ro = try posix.tcgetattr(STDIN_FD);
+
+ // ativa modo `raw` do terminal
+ var termios_rw = termios_ro;
+
+ termios_rw.oflag.OPOST = false;
+
+ termios_rw.iflag.IXON = false;
+ termios_rw.iflag.ICRNL = false;
+
+ termios_rw.lflag.ECHO = false;
+ termios_rw.lflag.ICANON = false;
+ termios_rw.lflag.ISIG = false;
+ termios_rw.lflag.IEXTEN = false;
+
+ try posix.tcsetattr(STDIN_FD, .FLUSH, termios_rw);
+
var quit_app = false;
while (!quit_app) {
- var buffer: [1024]u8 = undefined;
+ var buffer: [64]u8 = undefined;
var stdin = io.File.stdin();
var stdin_reader = stdin.reader(init.io, &buffer);
const reader_interface = &stdin_reader.interface;
+ // lê apenas um caractere da stdin
const char = try reader_interface.peek(1);
if (std.mem.eql(u8, char, " ")) {
if (track.isPaused()) {
@@ -62,6 +81,9 @@ pub fn main(
sdl3.timer.delayMilliseconds(poll_interval_ms);
}
+ // restaura terminal pro estado original ao final
+ try posix.tcsetattr(0, .FLUSH, termios_ro);
+
try track.stop(0);
try log_app.logInfo("Quit.", .{});
}