everything related to duct tape audio suite (dtas)
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: <dtas-all@nongnu.org>
Cc: e@80x24.org
Subject: [PATCH 5/5] dtas-sourceedit: update player as user saves in the editor
Date: Sat, 17 Jan 2015 11:49:22 +0000
Message-ID: <1421495362-31430-6-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1421495362-31430-1-git-send-email-e@80x24.org>

This allows changes in the source YAML file to be reflected
immediately in player after the user saves the file in their
favorite $EDITOR.  Previously, a user would need to:

	1) start dtas-sourceedit, spawning $EDITOR
	2) edit the file
	3) save changes
	4) exit $EDITOR
	5) repeat starting from 1) until happy with the results

Now, the workflow allows avoiding the context switch between their
$EDITOR and terminal to restart dtas-sourcedit:

	1) start dtas-sourceedit, spawning $EDITOR
	2) edit the file
	3) save changes
	4) repeat starting from 1) until happy with the results
	5) exit $EDITOR

In my experience, this greatly speeds up tuning of the playback
change, giving all the repeatability and flexibility of editing text
files while having the immediacy of an interactive UI.

Keep in mind this can cause problems for those with auto-save
enabled in their $EDITOR buffer at inopportune times, so a
-N/--no-watch option is added.
---
 Documentation/dtas-sourceedit.txt  | 16 ++++++++
 bin/dtas-sourceedit                | 84 ++++++++++++++++++++++++++++----------
 lib/dtas/source/splitfx.rb         |  4 +-
 lib/dtas/{source => }/watchable.rb |  2 +-
 4 files changed, 81 insertions(+), 25 deletions(-)
 rename lib/dtas/{source => }/watchable.rb (97%)

diff --git a/Documentation/dtas-sourceedit.txt b/Documentation/dtas-sourceedit.txt
index 4335f14..cff3bbc 100644
--- a/Documentation/dtas-sourceedit.txt
+++ b/Documentation/dtas-sourceedit.txt
@@ -17,6 +17,22 @@ a pipe or file, it is parsed as YAML and fed to the dtas-player(1) instance
 non-interactively.  This is useful for loading various profiles from the
 filesystem.
 
+On Linux machines with the sleepy_penguin RubyGem installed, inotify(7)
+is used to monitor the file for changes while the text exitor is running.
+Each time a user finishes saving a file, changes are committed immediately.
+This behavior may be disabled by using the -N or --no-watch command-line
+switch.
+
+# OPTIONS
+-N, \--no-watch
+:   Disable inotify(7) support on Linux systems
+
+-n, \--dry-run
+:   Only print commands which would be sent to dtas-player
+
+-V, \--verbose
+:   Print out commands as they are sent to dtas-player
+
 # EXAMPLES
 
 Invoking dtas-sourceedit will spawn your favorite text editor on "sox":
diff --git a/bin/dtas-sourceedit b/bin/dtas-sourceedit
index b411b11..1980141 100755
--- a/bin/dtas-sourceedit
+++ b/bin/dtas-sourceedit
@@ -1,16 +1,32 @@
 #!/usr/bin/env ruby
-# Copyright (C) 2013-2014, Eric Wong <e@80x24.org> and all contributors
+# Copyright (C) 2013-2015, all contributors <dtas-all@nongnu.org>
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require 'optparse'
 require 'dtas/edit_client'
+require 'dtas/sigevent'
+require 'dtas/watchable'
 include DTAS::EditClient
 c = client_socket
 sources = c.req('source ls') || "(unknown)"
-usage = "Usage: #{DTAS_PROGNAME} [-n|--dry-run][-V|--verbose] SOURCENAME\n" \
-        "available SOURCENAME values: #{sources}"
+usage = "Usage: #{DTAS_PROGNAME} [OPTIONS] SOURCENAME\n" \
+        "available SOURCENAME values: #{sources}\n" \
 
-# use a real option parser if we have anything more complex
-dry_run = !!(ARGV.delete('-n') || ARGV.delete('--dry-run'))
-verbose = !!(ARGV.delete('-V') || ARGV.delete('--verbose'))
+dry_run = verbose = false
+watch = defined?(DTAS::Watchable)
+OptionParser.new('', 24, '  ') do |op|
+  op.banner = usage
+  watch and
+    op.on('-N', '--no-watch', 'disable inotify support') { watch = false }
+
+  op.on('-n', '--dry-run', 'only print commands, do not run them') {
+    dry_run = true
+  }
+  op.on('-V', '--verbose', 'print out commands sent to change the source') {
+    verbose = true
+  }
+  op.on('-h', '--help') { puts(op.to_s); exit }
+  op.parse!(ARGV)
+end
 
 ARGV.size <= 1 or abort usage
 name = ARGV[0] || "sox"
@@ -21,27 +37,51 @@ buf = c.req(%W(source cat #{name}))
 abort(buf) if buf =~ /\AERR/
 orig = YAML.load(buf)
 
+commit_update = lambda do |buf|
+  source = YAML.load(buf)
+  cmd = %W(source ed #{name})
+  update_cmd_env(cmd, orig, source)
+
+  # nil OK
+  %w(tryorder command).each { |field| cmd << "#{field}=#{source[field]}" }
+
+  warn(Shellwords.join(cmd)) if verbose || dry_run
+  c.req_ok(cmd) unless dry_run
+  orig = source
+end
+
 if st_in.file? || st_in.pipe?
   buf = $stdin.read
+  commit_update.call(buf)
 else
   tmp = tmpyaml
+  tmp_path = tmp.path
+  do_update = lambda { commit_update.call(File.read(tmp_path)) }
   tmp.write(buf << DTAS_DISCLAIMER)
-  cmd = "#{editor} #{tmp.path}"
-  system(cmd) or abort "#{cmd} failed: #$?"
-  buf = File.read(tmp.path)
-end
+  cmd = "#{editor} #{tmp_path}"
 
-source = YAML.load(buf)
-cmd = %W(source ed #{name})
-update_cmd_env(cmd, orig, source)
+  sev = DTAS::Sigevent.new
+  rset = [ sev ]
+  if watch
+    ino = DTAS::Watchable::InotifyReadableIter.new
+    ino.watch_file(tmp_path, do_update)
+    rset << ino
+  end
 
-# nil OK
-%w(tryorder command).each do |field|
-  cmd << "#{field}=#{source[field]}"
+  trap(:CHLD) { sev.signal }
+  pid = Process.spawn(cmd)
+  begin
+    r = IO.select(rset) or next
+    r[0].each do |io|
+      case io
+      when sev
+        _, status = Process.waitpid2(pid)
+        status.success? or abort "#{cmd} failed: #{status.inspect}"
+        do_update.call
+        exit
+      when ino
+        ino.readable_iter # calls do_update
+      end
+    end
+  end while true
 end
-
-if verbose || dry_run
-  warn Shellwords.join(cmd)
-end
-
-c.req_ok(cmd) unless dry_run
diff --git a/lib/dtas/source/splitfx.rb b/lib/dtas/source/splitfx.rb
index 19b2c16..80d8473 100644
--- a/lib/dtas/source/splitfx.rb
+++ b/lib/dtas/source/splitfx.rb
@@ -3,12 +3,12 @@
 require 'yaml'
 require_relative 'sox'
 require_relative '../splitfx'
-require_relative 'watchable'
+require_relative '../watchable'
 
 class DTAS::Source::SplitFX < DTAS::Source::Sox # :nodoc:
   MAX_YAML_SIZE = 512 * 1024
   attr_writer :sox, :sfx
-  include DTAS::Source::Watchable if defined?(DTAS::Source::Watchable)
+  include DTAS::Watchable if defined?(DTAS::Watchable)
 
   SPLITFX_DEFAULTS = SOX_DEFAULTS.merge(
     "command" => "#{SOX_DEFAULTS["command"]} $FX",
diff --git a/lib/dtas/source/watchable.rb b/lib/dtas/watchable.rb
similarity index 97%
rename from lib/dtas/source/watchable.rb
rename to lib/dtas/watchable.rb
index 3ff9ef8..d4c384c 100644
--- a/lib/dtas/source/watchable.rb
+++ b/lib/dtas/watchable.rb
@@ -7,7 +7,7 @@ end
 
 # used to restart DTAS::Source::SplitFX processing in dtas-player
 # if the YAML file is edited
-module DTAS::Source::Watchable
+module DTAS::Watchable
   class InotifyReadableIter < SleepyPenguin::Inotify
     def self.new
       super(:CLOEXEC)
-- 
EW



      parent reply	other threads:[~2015-01-17 11:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-17 11:49 [PATCH 0/5] misc updates, major sourceedit feature! Eric Wong
2015-01-17 11:49 ` [PATCH 1/5] partstats: use Etc.nprocessors on Ruby 2.2+ Eric Wong
2015-01-17 11:49 ` [PATCH 2/5] splitfx: remove unnecessary assignment Eric Wong
2015-01-17 11:49 ` [PATCH 3/5] player: enqueued commands cannot use bypass Eric Wong
2015-01-17 11:49 ` [PATCH 4/5] dtas-console: avoid crashing on failed cue seeks Eric Wong
2015-01-17 11:49 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://80x24.org/dtas/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1421495362-31430-6-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=dtas-all@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

everything related to duct tape audio suite (dtas)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://80x24.org/dtas-all
	git clone --mirror http://ou63pmih66umazou.onion/dtas-all

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 dtas-all dtas-all/ https://80x24.org/dtas-all \
		dtas-all@nongnu.org
	public-inbox-index dtas-all

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.audio.dtas
	nntp://ou63pmih66umazou.onion/inbox.comp.audio.dtas
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for the project(s) associated with this inbox:

	dtas.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git