dtas.git  about / heads / tags
duct tape audio suite for *nix
blob 682afc74ef9527db3a712d598fcb90e005c6e976 5795 bytes (raw)
$ git show v0.1.I:bin/dtas-console	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
 
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net>
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
#
# Note: no idea what I'm doing, especially w.r.t. curses
require 'dtas/unix_client'
require 'dtas/rg_state'
require 'curses'
require 'yaml'

w = DTAS::UNIXClient.new
w.req_ok('watch')
c = DTAS::UNIXClient.new
cur = YAML.load(c.req('current'))
readable = [ w, $stdin ]

# current rg mode
rg_mode = DTAS::RGState::RG_MODE.keys.unshift("off")
if (rg = cur["rg"]) && (rg = rg["mode"])
  rg_mode_i = rg_mode.index(cur["rg"]["mode"])
else
  rg_mode_i = 0
end

def update_tfmt(prec)
  prec == 0 ? '%H:%M:%S' : "%H:%M:%S.%#{prec}N"
end
trap(:INT) { exit(0) }
trap(:TERM) { exit(0) }

# time precision
prec_nr = 1
prec_step = (0..9).to_a
prec_max = prec_step.size - 1
tfmt = update_tfmt(prec_step[prec_nr])
events = []
interval = 1.0 / 10 ** prec_nr

def show_events(lineno, screen, events)
  Curses.setpos(lineno += 1, 0)
  Curses.clrtoeol
  Curses.addstr('Events:')
  maxy = screen.maxy - 1
  maxx = screen.maxx
  events.reverse_each do |e|
    Curses.setpos(lineno += 1, 0)
    Curses.clrtoeol
    extra = e.size/maxx
    break if (lineno + extra) >= maxy

    # deal with long lines
    if extra
      rewind = lineno
      extra.times do
        Curses.setpos(lineno += 1, 0)
        Curses.clrtoeol
      end
      Curses.setpos(rewind, 0)
      Curses.addstr(e)
      Curses.setpos(lineno, 0)
    else
      Curses.addstr(e)
    end
  end

  # discard events we can't show
  nr_events = events.size
  if nr_events > maxy
    events = events[(nr_events - maxy)..-1]
    until lineno >= screen.maxy
      Curses.setpos(lineno += 1, 0)
      Curses.clrtoeol
    end
  else
    Curses.setpos(maxy + 1, 0)
    Curses.clrtoeol
  end
end

def fmt_to_s(f)
  r = [ f['rate'], f['channels'], f['type'], f['bits'] ]
  r.compact!
  r.join(',')
end

def rg_string(rg, current)
  rv = "rg mode=#{rg['mode']||'off'}"
  defaults = DTAS::RGState::RG_DEFAULT
  # don't show things that are too rare
  %w(preamp fallback_gain).each do |param|
    val = rg[param] || defaults[param]
    rv << " #{param}=#{val}"
  end
  env = current && current["env"] and rv << " / RGFX='#{env['RGFX']}'"
  rv
end

begin
  Curses.init_screen
  Curses.nonl
  Curses.cbreak
  Curses.noecho
  screen = Curses.stdscr
  screen.scrollok(true)
  screen.keypad(true)
  loop do
    lineno = -1
    pfmt = cur['format']
    if current = cur['current']
      Curses.setpos(lineno += 1, 0)
      Curses.clrtoeol
      Curses.addstr(current['infile'] || current['command'])

      elapsed = Time.now.to_f - current['spawn_at']
      if (nr = cur['current_initial']) && (current_format = current['format'])
        rate = current_format['rate'].to_f
        elapsed += nr / rate
        total = " [#{Time.at(current['samples'] / rate).strftime(tfmt)}]"
        fmt = "(#{fmt_to_s(current_format)} > #{fmt_to_s(pfmt)})"
      else
        total = ""
        fmt = fmt_to_s(pfmt)
        fmt = "(#{fmt} > #{fmt})"
      end

      Curses.setpos(lineno += 1, 0)
      Curses.clrtoeol
      Curses.addstr("#{Time.at(elapsed).strftime(tfmt)}#{total} #{fmt}")
    else
      Curses.setpos(lineno += 1, 0)
      Curses.clrtoeol
      Curses.addstr(cur['paused'] ? 'paused' : 'idle')
      Curses.setpos(lineno += 1, 0)
      Curses.clrtoeol
    end

    rgs = rg_string(cur["rg"] || {}, current)
    Curses.setpos(lineno += 1, 0)
    Curses.clrtoeol
    Curses.addstr(rgs)

    show_events(lineno, screen, events)

    Curses.refresh # draw and wait
    r = IO.select(readable, nil, nil, current ? interval : nil) or next
    r[0].each do |io|
      case io
      when w
        event = w.res_wait
        events << "#{Time.now.strftime(tfmt)} #{event}"
        # something happened, refresh current
        # we could be more intelligent here, maybe, but too much work.
        cur = YAML.load(c.req('current'))
      when $stdin
        # keybindings taken from mplayer / vi
        case key = Curses.getch
        when "j" then c.req_ok("seek -5")
        when "k" then c.req_ok("seek +5")
        when Curses::KEY_DOWN then c.req_ok("seek -60")
        when Curses::KEY_UP then c.req_ok("seek +60")
        when Curses::KEY_LEFT then c.req_ok("seek -10")
        when Curses::KEY_RIGHT then c.req_ok("seek +10")
        when Curses::KEY_BACKSPACE then c.req_ok("seek 0")
        # yes, some of us have long audio files
        when Curses::KEY_PPAGE then c.req_ok("seek +600")
        when Curses::KEY_NPAGE then c.req_ok("seek -600")
        when "9" then c.req_ok("rg preamp-=1")
        when "0" then c.req_ok("rg preamp+=1")
        when "F" then c.req_ok("rg fallback_gain+=1")
        when "f" then c.req_ok("rg fallback_gain-=1")
        when " "
          c.req("play_pause")
        when "r" # cycle through replaygain modes
          rg_mode_i >= 1 and c.req_ok("rg mode=#{rg_mode[rg_mode_i -= 1]}")
        when "R"
          rg_mode_i < (rg_mode.size - 1) and
            c.req_ok("rg mode=#{rg_mode[rg_mode_i += 1]}")
        when "p" # lower precision of time display
          if prec_nr >= 1
            prec_nr -= 1
            tfmt = update_tfmt(prec_step[prec_nr])
            interval = 1.0 / 10 ** prec_nr
          end
        when "P" # increase precision of time display
          if prec_nr < prec_max
            prec_nr += 1
            tfmt = update_tfmt(prec_step[prec_nr])
            interval = 1.0 / 10 ** prec_nr
          end
        when 27 # TODO readline/edit mode?
        else
          Curses.setpos(screen.maxy - 1, 0)
          Curses.clrtoeol
          Curses.addstr("unknown key=#{key.inspect}")
        end
      end
    end
  end
rescue EOFError
  Curses.close_screen
  abort "dtas-player exited"
ensure
  Curses.close_screen
end

git clone git://80x24.org/dtas.git
git clone https://80x24.org/dtas.git