Audio.pm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use File::Basename;
  6. use IPC::System::Simple;
  7. use Time::HiRes qw( usleep sleep time );
  8. # Package name
  9. package Audio;
  10. # Config
  11. my $MEDIA_PATH = `~/bin/video/mediaPath`;
  12. # App config
  13. my $DATA_DIR = DMX::dataDir();
  14. my $QT_FILE = $DATA_DIR . 'QT_PLAYER';
  15. my $QT_SOCK = $QT_FILE . '.socket';
  16. my $WAIT_DELAY = 0.1;
  17. my $MAX_WAIT = 5 / $WAIT_DELAY;
  18. # Debug
  19. my $DEBUG = 0;
  20. if ($ENV{'DEBUG'}) {
  21. $DEBUG = 1;
  22. }
  23. # State
  24. my %FILES = ();
  25. my $SOCK = undef();
  26. # Send the provided arguments to the QT socket
  27. sub sendCmd(@) {
  28. # Connect the socket as needed
  29. if (!$SOCK) {
  30. $SOCK = DMX::clientSock($QT_SOCK);
  31. }
  32. # Ensure the data is compatible with the encoding scheme
  33. foreach my $part (@_) {
  34. if ($part =~ /\|/) {
  35. die('Invalid QT command component: ' . $part . "\n");
  36. }
  37. }
  38. # Build the command stirng
  39. my $str = join('|', @_);
  40. if ($DEBUG) {
  41. print STDERR 'Sending QT_PLAYER command: ' . $str . "\n";
  42. }
  43. # Send the command
  44. $SOCK->send($str)
  45. or die("Unable to send to QT socket\n");
  46. }
  47. # Execute the provided string with osascript
  48. sub runApplescript($) {
  49. my ($script) = @_;
  50. if ($DEBUG) {
  51. print STDERR 'AppleScript: ' . $script . "\n";
  52. }
  53. my $retval = IPC::System::Simple::capture('osascript', '-e', $script);
  54. $retval =~ s/[\r\n]$//;
  55. if ($DEBUG) {
  56. print STDERR "\tAppleScript result: " . $retval . "\n";
  57. }
  58. return $retval;
  59. }
  60. # Load a document
  61. sub load($$) {
  62. my ($name, $path) = @_;
  63. sendCmd('LOAD', $name, $path);
  64. # Wait for the file to load
  65. my $count = 0;
  66. while (!loaded($name)) {
  67. Time::HiRes::sleep($WAIT_DELAY);
  68. $count++;
  69. if ($count > $MAX_WAIT) {
  70. die('Unable to open file: ' . $name . "\n");
  71. }
  72. }
  73. }
  74. # Is the named document loaded?
  75. sub loaded($) {
  76. my ($name) = @_;
  77. parseList();
  78. return exists($FILES{$name});
  79. }
  80. # Unload the named document
  81. sub unload($) {
  82. my ($name) = @_;
  83. if ($DEBUG) {
  84. print STDERR 'Audio::loadAudio(): ' . $name . "\n";
  85. }
  86. sendCmd('UNLOAD', $name);
  87. }
  88. # Play the named document, waiting for completion
  89. sub play($) {
  90. my ($name) = @_;
  91. if ($DEBUG) {
  92. print STDERR 'Audio::play(): ' . $name . "\n";
  93. }
  94. # Ensure the file is loaded
  95. dieUnloaded($name);
  96. my @cmd = ('tell application "QuickTime Player"');
  97. push(@cmd, 'play document ' . $FILES{$name});
  98. push(@cmd, 'repeat while playing of document ' . $FILES{$name} . ' = true');
  99. push(@cmd, 'delay 0.1');
  100. push(@cmd, 'end repeat');
  101. push(@cmd, 'end tell');
  102. runApplescript(join("\n", @cmd));
  103. }
  104. # Play the named document and immediately return
  105. sub background($) {
  106. my ($name) = @_;
  107. if ($DEBUG) {
  108. print STDERR 'Audio::background(): ' . $name . "\n";
  109. }
  110. # Ensure the file is loaded
  111. dieUnloaded($name);
  112. runApplescript('tell application "QuickTime Player" to play document ' . $FILES{$name});
  113. }
  114. # Pause the named document
  115. sub pause($) {
  116. my ($name) = @_;
  117. if ($DEBUG) {
  118. print STDERR 'Audio::pause(): ' . $name . "\n";
  119. }
  120. # Ensure the file is loaded
  121. dieUnloaded($name);
  122. runApplescript('tell application "QuickTime Player" to pause document ' . $FILES{$name});
  123. }
  124. # Stop playback of the named document
  125. sub stop($) {
  126. my ($name) = @_;
  127. if ($DEBUG) {
  128. print STDERR "Audio::stopAudio()\n";
  129. }
  130. # Ensure the file is loaded
  131. dieUnloaded($name);
  132. # Stop and rewind
  133. runApplescript('tell application "QuickTime Player" to stop document ' . $FILES{$name});
  134. runApplescript('tell application "QuickTime Player" to set current time of document ' . $FILES{$name} . ' to 0');
  135. }
  136. # Is the named document playing?
  137. sub playing($) {
  138. my ($name) = @_;
  139. if ($DEBUG) {
  140. print STDERR 'Audio::playing(): ' . $name . "\n";
  141. }
  142. # Ensure the file is loaded
  143. dieUnloaded($name);
  144. # QT has a "playing" property but it is only true when rate == 1.0
  145. my $rate = rate($name);
  146. if ($rate > 0) {
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. # Set or get the audio position
  152. sub position($$) {
  153. my ($name, $new) = @_;
  154. if ($DEBUG) {
  155. print STDERR 'Audio::position(): ' . $name . "\n";
  156. }
  157. # Ensure the file is loaded
  158. dieUnloaded($name);
  159. if ($new) {
  160. runApplescript('tell application "QuickTime Player" to set current time of document ' . $FILES{$name} . ' to ' . $new);
  161. } else {
  162. return runApplescript('tell application "QuickTime Player" to get current time of document ' . $FILES{$name});
  163. }
  164. }
  165. # Set or get the playback rate
  166. sub rate($$) {
  167. my ($name, $new) = @_;
  168. if ($DEBUG) {
  169. print STDERR 'Audio::rate(): ' . $name . "\n";
  170. }
  171. # Ensure the file is loaded
  172. dieUnloaded($name);
  173. if ($new) {
  174. runApplescript('tell application "QuickTime Player" to set rate of document ' . $FILES{$name} . ' to ' . $new);
  175. } else {
  176. return runApplescript('tell application "QuickTime Player" to get rate of document ' . $FILES{$name});
  177. }
  178. }
  179. # Set or get a document's volume
  180. sub volume($$) {
  181. my ($name, $new) = @_;
  182. if ($DEBUG) {
  183. print STDERR 'Audio::volume(): ' . $name . "\n";
  184. }
  185. # Ensure the file is loaded
  186. dieUnloaded($name);
  187. if ($new) {
  188. runApplescript('tell application "QuickTime Player" to set audio volume of document ' . $FILES{$name} . ' to ' . $new);
  189. } else {
  190. return runApplescript('tell application "QuickTime Player" to get audio volume of document ' . $FILES{$name});
  191. }
  192. }
  193. # Set or get the system output volume
  194. sub systemVolume($) {
  195. my ($new) = @_;
  196. if ($DEBUG) {
  197. print STDERR "Audio::systemVolume()\n";
  198. }
  199. if ($new) {
  200. runApplescript('set volume output volume ' . $new);
  201. } else {
  202. return runApplescript('get output volume of (get volume settings)');
  203. }
  204. }
  205. # Stop playback on all documents
  206. sub stopAll() {
  207. sendCmd('STOP');
  208. }
  209. # Reset the entire QT Player process
  210. sub reset() {
  211. sendCmd('RESET');
  212. }
  213. # Read and parse the qt_player file list
  214. sub parseList() {
  215. %FILES = ();
  216. open(my $fh, $QT_FILE)
  217. or die('Unable to open QT_PLAYER file: ' . $! . "\n");
  218. while (<$fh>) {
  219. my ($name, $handle) = $_ =~ /^([^|]+)\|\s*(\".+\")\s*$/;
  220. if (!$name || !$handle) {
  221. die('Invalid QT_PLAYER line: ' . $_);
  222. }
  223. $FILES{$name} = $handle;
  224. }
  225. close($fh);
  226. }
  227. sub dieUnloaded($) {
  228. my ($name) = @_;
  229. if (!loaded($name)) {
  230. die('File not loaded: ' . $name . "\n");
  231. }
  232. }
  233. # Always return true
  234. 1;