Audio.pm 7.0 KB

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