Audio.pm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use File::Touch;
  6. use File::Basename;
  7. use Time::HiRes qw( usleep sleep time );
  8. use IPC::System::Simple;
  9. # Package name
  10. package Audio;
  11. # Config
  12. my $MEDIA_PATH = `~/bin/video/mediaPath`;
  13. my %FILES = ();
  14. # Debug
  15. my $DEBUG = 0;
  16. if ($ENV{'DEBUG'}) {
  17. $DEBUG = 1;
  18. }
  19. sub runApplescript($) {
  20. my ($script) = @_;
  21. if ($DEBUG) {
  22. print STDERR 'AppleScript: ' . $script . "\n";
  23. }
  24. my $retval = IPC::System::Simple::capture('osascript', '-e', $script);
  25. if ($DEBUG) {
  26. print STDERR "\tAppleScript result: " . $retval . "\n";
  27. }
  28. return $retval;
  29. }
  30. sub addLoad($$) {
  31. my ($name, $path) = @_;
  32. if ($DEBUG) {
  33. print STDERR 'Audio::addLoad(): ' . $name . "\n";
  34. }
  35. add($name, $path);
  36. load($name);
  37. }
  38. sub add($$) {
  39. my ($name, $path) = @_;
  40. if ($DEBUG) {
  41. print STDERR 'Audio::add(): ' . $name . "\n";
  42. }
  43. # Construct an absolute file path
  44. if (!($path =~ /^\//)) {
  45. $path = $MEDIA_PATH . '/' . $path;
  46. }
  47. # Validate
  48. if (!-r $path) {
  49. die('Invalid audio path: ' . $path . "\n");
  50. }
  51. # We only support one document per name
  52. drop($name);
  53. # Append the array
  54. my %tmp = ('path' => $path);
  55. $FILES{$name} = \%tmp;
  56. }
  57. sub drop($) {
  58. my ($name) = @_;
  59. if ($DEBUG) {
  60. print STDERR 'Audio::drop(): ' . $name . "\n";
  61. }
  62. # Stop and unload as necessary
  63. if (loaded($name)) {
  64. stop($name);
  65. unload($name);
  66. }
  67. # Drop as necessary
  68. if (available($name)) {
  69. delete($FILES{$name});
  70. }
  71. }
  72. sub loaded($) {
  73. my ($name) = @_;
  74. if ($DEBUG) {
  75. print STDERR 'Audio::loaded(): ' . $name . "\n";
  76. }
  77. if (available($name) && defined($FILES{$name}->{'name'})) {
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. sub available($) {
  83. my ($name) = @_;
  84. if ($DEBUG) {
  85. print STDERR 'Audio::available(): ' . $name . "\n";
  86. }
  87. if (defined($name) && defined($FILES{$name})) {
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. sub play($) {
  93. my ($name) = @_;
  94. if ($DEBUG) {
  95. print STDERR 'Audio::play(): ' . $name . "\n";
  96. }
  97. # Start playback
  98. background($name);
  99. # Wait for it to complete
  100. my @cmd = ('tell application "QuickTime Player"');
  101. push(@cmd, 'repeat while playing of document ' . $FILES{$name}->{'name'} . ' = true');
  102. push(@cmd, 'delay 0.1');
  103. push(@cmd, 'end repeat');
  104. push(@cmd, 'end tell');
  105. runApplescript(join("\n", @cmd));
  106. }
  107. sub background($) {
  108. my ($name) = @_;
  109. if ($DEBUG) {
  110. print STDERR 'Audio::background(): ' . $name . "\n";
  111. }
  112. if (!loaded($name)) {
  113. die('Invalid QT document: ' . $name . "\n");
  114. }
  115. # Start playback
  116. my @cmd = ('tell application "QuickTime Player"');
  117. push(@cmd, 'set current time of document ' . $FILES{$name}->{'name'} . ' to 0');
  118. push(@cmd, 'play document ' . $FILES{$name}->{'name'});
  119. push(@cmd, 'end tell');
  120. runApplescript(join("\n", @cmd));
  121. }
  122. sub playing($) {
  123. my ($name) = @_;
  124. if ($DEBUG) {
  125. print STDERR 'Audio::playing(): ' . $name . "\n";
  126. }
  127. if (!loaded($name)) {
  128. die('Invalid QT document: ' . $name . "\n");
  129. }
  130. # Ask QT if the document is playing
  131. my $result = runApplescript('tell application "QuickTime Player" to get playing of document ' . $FILES{$name}->{'name'});
  132. if ($result =~ /true/i) {
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. sub position($$) {
  138. my ($name, $new) = @_;
  139. if ($DEBUG) {
  140. print STDERR 'Audio::position(): ' . $name . "\n";
  141. }
  142. if (!loaded($name)) {
  143. die('Invalid QT document: ' . $name . "\n");
  144. }
  145. # Optionally set the audio position
  146. if ($new) {
  147. runApplescript('tell application "QuickTime Player" to set current time of document ' . $FILES{$name}->{'name'} . ' to ' . $new);
  148. }
  149. # Get the current playback position from QT
  150. return runApplescript('tell application "QuickTime Player" to get current time of document ' . $FILES{$name}->{'name'});
  151. }
  152. sub rate($$) {
  153. my ($name, $new) = @_;
  154. if ($DEBUG) {
  155. print STDERR 'Audio::rate(): ' . $name . "\n";
  156. }
  157. if (!loaded($name)) {
  158. die('Invalid QT document: ' . $name . "\n");
  159. }
  160. # Optionally set the playback rate
  161. if ($new) {
  162. runApplescript('tell application "QuickTime Player" to set rate of document ' . $FILES{$name}->{'name'} . ' to ' . $new);
  163. }
  164. # Get the current playback position from QT
  165. return runApplescript('tell application "QuickTime Player" to get rate of document ' . $FILES{$name}->{'name'});
  166. }
  167. sub stop($) {
  168. my ($name) = @_;
  169. if ($DEBUG) {
  170. print STDERR "Audio::stopAudio()\n";
  171. }
  172. # Allow control of all documents or a specific document
  173. my $doc = 'every document';
  174. if (!loaded($name)) {
  175. $doc = 'document ' . $FILES{$name}->{'name'};
  176. }
  177. # Stop and rewind all QT documents
  178. runApplescript('tell application "QuickTime Player" to stop ' . $doc);
  179. runApplescript('tell application "QuickTime Player" to set current time of ' . $doc . ' to 0');
  180. # Bring Plex back to the front
  181. runApplescript('tell application "Plex" to activate');
  182. }
  183. sub unload($) {
  184. my ($name) = @_;
  185. if ($DEBUG) {
  186. print STDERR 'Audio::loadAudio(): ' . $name . "\n";
  187. }
  188. if (!available($name)) {
  189. die('Invalid QT document: ' . $name . "\n");
  190. }
  191. # Nothing to do if the file isn't loaded
  192. if (!loaded($name)) {
  193. return;
  194. }
  195. # Close to document
  196. runApplescript('tell application "QuickTime Player" to close document ' . $FILES{$name}->{'name'});
  197. # Delete our local handle
  198. delete($FILES{$name}->{'name'});
  199. }
  200. sub load($) {
  201. my ($name) = @_;
  202. if ($DEBUG) {
  203. print STDERR 'Audio::loadAudio(): ' . $name . "\n";
  204. }
  205. if (!available($name)) {
  206. die('Invalid QT document: ' . $name . "\n");
  207. }
  208. # Unload first, if we're already loaded
  209. if (loaded($name)) {
  210. unload($name);
  211. }
  212. # Find out how many files are already open
  213. my $count = runApplescript('tell application "QuickTime Player" to count items of every document');
  214. # Open the file
  215. IPC::System::Simple::system('open', '-a', 'QuickTime Player', $FILES{$name}->{'path'});
  216. # Wait for QT to load the new file
  217. my @cmd = ('tell application "QuickTime Player"');
  218. push(@cmd, 'repeat while (count items of every document) <= ' . $count);
  219. push(@cmd, 'delay 0.1');
  220. push(@cmd, 'end repeat');
  221. push(@cmd, 'get document 1');
  222. push(@cmd, 'end tell');
  223. my $doc = runApplescript(join("\n", @cmd));
  224. # Clean up the document name
  225. $doc =~ s/^\s*document //;
  226. $doc =~ s/\s+$//;
  227. $doc =~ s/\"/\\\"/g;
  228. $doc = '"' . $doc . '"';
  229. # Save the document handle
  230. $FILES{$name}->{'name'} = $doc;
  231. # Bring Plex back to the front
  232. runApplescript('tell application "Plex" to activate');
  233. }
  234. sub init() {
  235. if ($DEBUG) {
  236. print STDERR "Audio::init()\n";
  237. }
  238. # Close all QT documents and relaunch QT Player
  239. runApplescript('tell application "QuickTime Player" to close every document');
  240. runApplescript('tell application "QuickTime Player" to quit');
  241. sleep(1);
  242. IPC::System::Simple::system('open', '-a', 'QuickTime Player');
  243. # Always load and play the SILENCE file (for sanity and general init)
  244. add('SILENCE', 'DMX/Silence.wav');
  245. load('SILENCE');
  246. play('SILENCE');
  247. }
  248. # Always return true
  249. 1;