Skip to content
Merged
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
6 changes: 5 additions & 1 deletion internal/pipmix/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ func dumpNodes(w io.Writer, s *pw.Snapshot, shown map[uint32]bool, opts options)
fmt.Fprintln(w, " hidden: duplicates a multi-channel stream")
}
for _, ch := range n.Channels {
fmt.Fprintf(w, " %-4s %3.0f%%\n", ch.Name, ch.Volume*100)
if n.JACK {
fmt.Fprintf(w, " %s\n", ch.Name)
} else {
fmt.Fprintf(w, " %-4s %3.0f%%\n", ch.Name, ch.Volume*100)
}
}
for i, r := range n.Routes {
mark := " "
Expand Down
24 changes: 16 additions & 8 deletions internal/pipmix/meters.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ const (

var meterLookFlags = []string{"solid", "shade", "dot"}

type meterMsg struct{}
type meterTickMsg struct{}
type (
meterMsg struct{}
meterTickMsg struct{}
)

func (m *mixer) waitMeter() tea.Cmd {
if m.meter == nil {
Expand Down Expand Up @@ -93,20 +95,24 @@ func (m *mixer) meterTargets() map[uint32]pulse.Target {
}

func meterTarget(n *pw.Node) (pulse.Target, bool) {
if n == nil || n.JACK || len(n.Channels) == 0 {
if n == nil || len(n.Channels) == 0 {
return pulse.Target{}, false
}
var names []string
for _, c := range n.Channels {
names = append(names, c.Name)
}
t := pulse.Target{Name: n.Name, Channels: names}
switch n.Class {
case pw.Sink:
switch {
case n.JACK && n.Class == pw.Playback:
t.Kind, t.Serial, t.Name = pulse.JACK, n.ID, ""
case n.JACK:
return pulse.Target{}, false
case n.Class == pw.Sink:
t.Kind = pulse.Sink
case pw.Source:
case n.Class == pw.Source:
t.Kind = pulse.Source
case pw.Playback:
case n.Class == pw.Playback:
serial, err := strconv.ParseUint(n.Props["object.serial"], 10, 32)
if err != nil {
return pulse.Target{}, false
Expand Down Expand Up @@ -222,7 +228,9 @@ func (m *mixer) setLevels(snap *pulse.Snapshot, now time.Time) (moving bool) {
func (m *mixer) shown(raw map[uint32][]float64) map[uint32][]float64 {
nodes := make(map[uint32]*pw.Node)
for i := range m.snap.Nodes {
nodes[m.snap.Nodes[i].ID] = &m.snap.Nodes[i]
if len(m.snap.Nodes[i].Channels) > 0 {
nodes[m.snap.Nodes[i].ID] = &m.snap.Nodes[i]
}
}
shown := make(map[uint32][]float64)
for id, levels := range raw {
Expand Down
34 changes: 30 additions & 4 deletions internal/pipmix/meters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func meterSnapshot() *pw.Snapshot {
}
s.Nodes = append(s.Nodes,
pw.Node{ID: 40, Class: pw.Recording, Name: "rec", Title: "Recorder", Channels: stereo(1)},
pw.Node{ID: 41, Class: pw.Playback, Name: "jack", Title: "jackd", JACK: true},
pw.Node{ID: 41, Class: pw.Playback, Name: "jack", Title: "jackd", JACK: true, Channels: []pw.Channel{{Name: "out_1", Volume: 1}, {Name: "out_2", Volume: 1}}},
pw.Node{ID: 41, Class: pw.Recording, Name: "jack", Title: "jackd", JACK: true},
pw.Node{ID: 42, Class: pw.Source, Name: "mic", Title: "Mic", Channels: []pw.Channel{{Name: "MONO", Volume: 1}}},
)
return s
Expand All @@ -42,13 +43,23 @@ func TestMeterTargets(t *testing.T) {
m.refocus()

got := m.meterTargets()
if keys := targetKeys(got); !slices.Equal(keys, []uint32{10, 11, 12}) {
t.Fatalf("playback targets %v, want 10 11 12 (no JACK client)", keys)
if keys := targetKeys(got); !slices.Equal(keys, []uint32{10, 11, 12, 41}) {
t.Fatalf("playback targets %v, want 10 11 12 41", keys)
}
want := pulse.Target{Kind: pulse.Stream, Serial: 101, Channels: []string{"FL", "FR"}}
if !got[11].Equal(want) {
t.Errorf("target of 11 = %+v, want %+v", got[11], want)
}
wantJACK := pulse.Target{Kind: pulse.JACK, Serial: 41, Channels: []string{"out_1", "out_2"}}
if !got[41].Equal(wantJACK) {
t.Errorf("target of 41 = %+v, want %+v", got[41], wantJACK)
}

m.opts.jack = false
if keys := targetKeys(m.meterTargets()); !slices.Equal(keys, []uint32{10, 11, 12}) {
t.Fatalf("playback targets without jack %v, want 10 11 12", keys)
}
m.opts.jack = true

m.setTab(tabOutputs)
want = pulse.Target{Kind: pulse.Sink, Name: "sink", Channels: []string{"FL", "FR"}}
Expand Down Expand Up @@ -118,10 +129,11 @@ func TestLevels(t *testing.T) {
10: {0.125, 1}, // a stream: after its volume already
12: {1, 1}, // muted
20: {0.125, 0.125}, // a sink at 100%: before its volume
41: {0.125, 0.512}, // a duplex JACK client: uses its playback channels
99: {1}, // gone
11: {1}, // channels don't match
}), now)
for id, want := range map[uint32][]float64{10: {0.5, 1}, 12: {0, 0}, 20: {0.5, 0.5}, 99: nil, 11: nil} {
for id, want := range map[uint32][]float64{10: {0.5, 1}, 12: {0, 0}, 20: {0.5, 0.5}, 41: {0.5, 0.8}, 99: nil, 11: nil} {
if !near(m.fill[id], want) || !near(m.dot[id], want) {
t.Errorf("node %d: fill %v, peak %v; want %v", id, m.fill[id], m.dot[id], want)
}
Expand Down Expand Up @@ -226,6 +238,20 @@ func TestBarLevel(t *testing.T) {
t.Errorf("bar(%v, %v, %v, 16, %v, look %d) = %q, want %q", tc.v, tc.level, tc.peak, tc.muted, tc.look, got, tc.want)
}
}
for _, tc := range []struct {
level, peak float64
look meterLook
want string
}{
{0, 0, meterSolid, "░░░░░░░░░░░░░░░░"},
{0.3, 0.7, meterSolid, "███░░░█░░░░░░░░░"},
{0.5, 0, meterDot, "░░░░█░░░░░░░░░░░"},
{0.5, 0.7, meterOff, "░░░░░░░░░░░░░░░░"},
} {
if got := ansi.Strip(meterBar(tc.level, tc.peak, 16, 1.5, tc.look)); got != tc.want {
t.Errorf("meterBar(%v, %v, 16, look %d) = %q, want %q", tc.level, tc.peak, tc.look, got, tc.want)
}
}
}

func TestMeterFadeIn(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions internal/pipmix/mixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ func newMixer(c *pw.Client, media *mpris.Watcher, meter *pulse.Meter, quitKeys s
}
}

type snapshotMsg struct{}
type mediaMsg struct{}
type (
snapshotMsg struct{}
mediaMsg struct{}
)

type noticeMsg struct {
text string
Expand Down
48 changes: 34 additions & 14 deletions internal/pipmix/mixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func testSnapshot() *pw.Snapshot {
{ID: 10, Class: pw.Playback, Name: "a", Title: "Player A", Channels: stereo(0.5)},
{ID: 11, Class: pw.Playback, Name: "b", Title: "Player B", Channels: stereo(1)},
{ID: 12, Class: pw.Playback, Name: "c", Title: "Player C", Channels: stereo(1.2), Mute: true},
{ID: 20, Class: pw.Sink, Name: "sink", Title: "Speakers", Channels: stereo(1), Default: true,
Routes: []pw.Route{{Index: 0, Description: "Speakers"}, {Index: 1, Description: "Headphones"}}, Route: 1},
{
ID: 20, Class: pw.Sink, Name: "sink", Title: "Speakers", Channels: stereo(1), Default: true,
Routes: []pw.Route{{Index: 0, Description: "Speakers"}, {Index: 1, Description: "Headphones"}}, Route: 1,
},
},
Cards: []pw.Card{{ID: 30, Title: "Card", Profile: 0, Profiles: []pw.Profile{
{Index: 0, Description: "Duplex"}, {Index: 1, Description: "Off"},
Expand Down Expand Up @@ -419,8 +421,10 @@ func TestCardsSpaced(t *testing.T) {
func TestTitleColumns(t *testing.T) {
m := testMixer()
s := testSnapshot()
s.Nodes = append(s.Nodes, pw.Node{ID: 21, Class: pw.Sink, Name: "hdmi", Title: "HDMI Output With A Long Name", Channels: stereo(1),
Routes: []pw.Route{{Index: 0, Description: "HDMI"}}, Route: 0, Mute: true})
s.Nodes = append(s.Nodes, pw.Node{
ID: 21, Class: pw.Sink, Name: "hdmi", Title: "HDMI Output With A Long Name", Channels: stereo(1),
Routes: []pw.Route{{Index: 0, Description: "HDMI"}}, Route: 0, Mute: true,
})
m.snap = s
m.handleKey("4")
lines := strings.Split(ansi.Strip(m.view()), "\n")
Expand Down Expand Up @@ -513,8 +517,12 @@ func TestTypeStrip(t *testing.T) {
id uint32
strip string
}{
{tabDefault, 20, strip(pw.Sink)}, {tabDefault, 10, strip(pw.Playback)}, {tabPlayback, 10, strip(pw.Playback)},
{tabRecording, 40, strip(pw.Recording)}, {tabOutputs, 20, strip(pw.Sink)}, {tabInputs, 22, strip(pw.Source)},
{tabDefault, 20, strip(pw.Sink)},
{tabDefault, 10, strip(pw.Playback)},
{tabPlayback, 10, strip(pw.Playback)},
{tabRecording, 40, strip(pw.Recording)},
{tabOutputs, 20, strip(pw.Sink)},
{tabInputs, 22, strip(pw.Source)},
{tabCards, 30, card},
} {
m.setTab(tc.tab)
Expand Down Expand Up @@ -550,7 +558,10 @@ func TestTypeStrip(t *testing.T) {

func TestJACKClient(t *testing.T) {
m := testMixer()
jack := pw.Node{ID: 5, Class: pw.Playback, Name: "dtime", Title: "dtime", JACK: true}
jack := pw.Node{
ID: 5, Class: pw.Playback, Name: "dtime", Title: "dtime", JACK: true,
Channels: []pw.Channel{{Name: "out_1", Volume: 1}, {Name: "out_2", Volume: 1}},
}
m.snap.Nodes = append([]pw.Node{jack}, m.snap.Nodes...)
m.refocus()
for _, tb := range []tab{tabDefault, tabPlayback} {
Expand All @@ -564,9 +575,16 @@ func TestJACKClient(t *testing.T) {
t.Fatalf("G: focus %d, want 5", id)
}
view := ansi.Strip(m.view())
if !strings.Contains(view, strings.Repeat(faderEmpty, m.barWidth())) || !strings.Contains(view, symSpeaker) || !strings.Contains(view, "JACK: dtime") {
if !strings.Contains(view, strings.Repeat(faderEmpty, m.barWidth())) || !strings.Contains(view, symSpeaker) || !strings.Contains(view, "JACK: dtime") || !strings.Contains(view, "2ch") {
t.Errorf("view:\n%s", view)
}
// Space unfolds individual JACK output ports to inspect their levels.
m.handleKey("space")
view = ansi.Strip(m.view())
if !strings.Contains(view, "┌ out_1") || !strings.Contains(view, "└ out_2") {
t.Errorf("unfolded JACK view:\n%s", view)
}
m.handleKey("space")
for _, key := range []string{"l", "m"} {
cmd := m.handleKey(key)
if cmd == nil {
Expand All @@ -576,12 +594,14 @@ func TestJACKClient(t *testing.T) {
t.Errorf("%s: %+v, want a JACK notice", key, msg)
}
}
cmd := m.click(tea.Mouse{X: muteX, Y: 7, Button: tea.MouseLeft})
if cmd == nil {
t.Fatal("click on the speaker: no notice")
}
if msg, ok := cmd().(noticeMsg); !ok || !strings.Contains(msg.text, "JACK client") {
t.Errorf("click on the speaker: %+v, want a JACK notice", msg)
for _, pt := range []struct{ x, y int }{{muteX, 7}, {barX + 2, 8}} {
cmd := m.click(tea.Mouse{X: pt.x, Y: pt.y, Button: tea.MouseLeft})
if cmd == nil {
t.Fatalf("click at (%d,%d): no notice", pt.x, pt.y)
}
if msg, ok := cmd().(noticeMsg); !ok || !strings.Contains(msg.text, "JACK client") {
t.Errorf("click at (%d,%d): %+v, want a JACK notice", pt.x, pt.y, msg)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions internal/pipmix/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func (m *mixer) click(mouse tea.Mouse) tea.Cmd {
if mouse.X < barX-2 || mouse.X >= barX+m.barWidth() {
return nil
}
if it.node.JACK {
return m.setVolume100()
}
channel := -1
if m.isUnlocked(it) {
channel = line.channel
Expand Down Expand Up @@ -91,6 +94,9 @@ func (m *mixer) click(mouse tea.Mouse) tea.Cmd {
if mouse.X < barX-2 || mouse.X >= barX+m.barWidth() {
return nil
}
if it.node.JACK {
return m.setVolume100()
}
channel := -1
if m.isUnlocked(it) {
channel = line.channel
Expand Down
9 changes: 7 additions & 2 deletions internal/pipmix/pipmix.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ func Run(args []string) error {
go http.Serve(ln, nil)
}

client := pw.NewClient(clientLog)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go client.Run(ctx)

var media *mpris.Watcher
if opts.titles {
media = mpris.NewWatcher(clientLog)
Expand All @@ -69,6 +68,12 @@ func Run(args []string) error {
meter = pulse.NewMeter(clientLog, opts.meterRate)
go meter.Run(ctx)
}

client := pw.NewClient(clientLog)
if meter != nil {
meter.JACKReady = client.JackMeterReady
}
go client.Run(ctx)
if debug.dump {
return dump(ctx, os.Stdout, &dumpLog, fs, opts, client, media, meter)
}
Expand Down
69 changes: 64 additions & 5 deletions internal/pipmix/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,18 @@ func (m *mixer) channelLine(it item, i int, focused bool) string {

var c pw.Channel
var nameStr string
if i < 0 {
if i < 0 && len(n.Channels) > 1 {
c = n.Channels[0]
nameStr = foldedName(n.Channels)
} else {
c = n.Channels[i]
nameStr = c.Name
c = n.Channels[max(i, 0)]
nameStr = ansi.Truncate(c.Name, labelWidth, "…")
}
name := strings.Repeat(" ", max(0, labelWidth-ansi.StringWidth(nameStr))) + nameStr
vol := " "
if !n.JACK {
vol = fmt.Sprintf("%3d", percent(c.Volume))
}
name := fmt.Sprintf("%*s", labelWidth, nameStr)
vol := fmt.Sprintf("%3d", percent(c.Volume))
if chanFocused {
name = boldStyle.Render(name)
vol = boldStyle.Render(vol)
Expand Down Expand Up @@ -575,6 +578,9 @@ func (m *mixer) channelLine(it item, i int, focused bool) string {
}

label := prefix + name + " " + vol + " "
if n.JACK {
return label + meterBar(m.level(it, i), m.peak(it, i), m.barWidth(), m.opts.limit, m.look())
}
return label + bar(c.Volume, m.level(it, i), m.peak(it, i), m.barWidth(), m.opts.limit, n.Mute, m.look())
}

Expand All @@ -588,6 +594,59 @@ func fixedBar(width int) string {
return strings.Repeat(" ", barX-contentX) + dimStyle.Render(strings.Repeat(faderEmpty, max(width, 0)))
}

// meterBar renders a level meter of width cells without a volume fader.
func meterBar(level, peak float64, width int, limit float64, look meterLook) string {
if width <= 0 {
return ""
}
lit, pk := 0, 0
if look != meterOff {
if level > 0 {
lit = knobCell(level, limit, width)
}
if peak > 0 {
pk = knobCell(peak, limit, width)
}
}
if lit == 0 && pk == 0 {
return dimStyle.Render(strings.Repeat(faderEmpty, width))
}
hundred := knobCell(1, limit, width)
styles := [...]lipgloss.Style{barFullStyle, barOverStyle, brightFull, brightOver, dimStyle}
type cell struct {
glyph string
style int
}
cells := make([]cell, width)
for i := range cells {
over := 0
if i > hundred {
over = 1
}
c := cell{faderEmpty, 4}
switch {
case i < lit && (look == meterSolid || look == meterShade):
c = cell{faderKnob, over}
case i == lit-1 && look == meterDot:
c = cell{faderKnob, 2 + over}
}
if i == pk-1 {
c = cell{faderKnob, 2 + over}
}
cells[i] = c
}
var b strings.Builder
for i := 0; i < len(cells); {
j := i + 1
for j < len(cells) && cells[j] == cells[i] {
j++
}
b.WriteString(styles[cells[i].style].Render(strings.Repeat(cells[i].glyph, j-i)))
i = j
}
return b.String()
}

var (
emptyOverStyle = lipgloss.NewStyle().Foreground(lipgloss.Red).Faint(true)
brightFull = lipgloss.NewStyle().Foreground(lipgloss.BrightGreen)
Expand Down
Loading
Loading