Player.pm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package Plugins::SqueezeESP32::Player;
  2. use strict;
  3. use base qw(Slim::Player::SqueezePlay);
  4. use Digest::MD5 qw(md5);
  5. use List::Util qw(min);
  6. use Slim::Utils::Log;
  7. use Slim::Utils::Prefs;
  8. my $sprefs = preferences('server');
  9. my $prefs = preferences('plugin.squeezeesp32');
  10. my $log = logger('plugin.squeezeesp32');
  11. {
  12. __PACKAGE__->mk_accessor('rw', 'tone_update');
  13. }
  14. sub new {
  15. my $class = shift;
  16. my $client = $class->SUPER::new(@_);
  17. $client->init_accessor(
  18. tone_update => 0,
  19. );
  20. return $client;
  21. }
  22. our $defaultPrefs = {
  23. 'analogOutMode' => 0,
  24. 'bass' => 0,
  25. 'treble' => 0,
  26. 'lineInAlwaysOn' => 0,
  27. 'lineInLevel' => 50,
  28. 'menuItem' => [qw(
  29. NOW_PLAYING
  30. BROWSE_MUSIC
  31. RADIO
  32. PLUGIN_MY_APPS_MODULE_NAME
  33. PLUGIN_APP_GALLERY_MODULE_NAME
  34. FAVORITES
  35. GLOBAL_SEARCH
  36. PLUGIN_LINE_IN
  37. PLUGINS
  38. SETTINGS
  39. SQUEEZENETWORK_CONNECT
  40. )],
  41. };
  42. my $handlersAdded;
  43. sub model { 'squeezeesp32' }
  44. sub modelName { 'SqueezeESP32' }
  45. sub hasScrolling { 1 }
  46. sub hasIR { 1 }
  47. # TODO: add in settings when ready
  48. sub hasLineIn { 0 }
  49. sub hasHeadSubOut { 1 }
  50. sub maxTreble { 20 }
  51. sub minTreble { -13 }
  52. sub maxBass { 20 }
  53. sub minBass { -13 }
  54. sub init {
  55. my $client = shift;
  56. if (!$handlersAdded) {
  57. # Add a handler for line-in/out status changes
  58. Slim::Networking::Slimproto::addHandler( LIOS => \&lineInOutStatus );
  59. # Create a new event for sending LIOS updates
  60. Slim::Control::Request::addDispatch(
  61. ['lios', '_state'],
  62. [1, 0, 0, undef],
  63. );
  64. Slim::Control::Request::addDispatch(
  65. ['lios', 'linein', '_state'],
  66. [1, 0, 0, undef],
  67. );
  68. Slim::Control::Request::addDispatch(
  69. ['lios', 'lineout', '_state'],
  70. [1, 0, 0, undef],
  71. );
  72. $handlersAdded = 1;
  73. }
  74. $client->SUPER::init(@_);
  75. main::INFOLOG && $log->is_info && $log->info("SqueezeESP player connected: " . $client->id);
  76. }
  77. sub initPrefs {
  78. my $client = shift;
  79. $sprefs->client($client)->init($defaultPrefs);
  80. $prefs->client($client)->init( {
  81. equalizer => [(0) x 10],
  82. artwork => undef,
  83. } );
  84. $client->SUPER::initPrefs;
  85. }
  86. # Allow the player to define it's display width (and probably more)
  87. sub playerSettingsFrame {
  88. my $client = shift;
  89. my $data_ref = shift;
  90. my $value;
  91. my $id = unpack('C', $$data_ref);
  92. # New SETD command 0xfe for display width & height
  93. if ($id == 0xfe) {
  94. $value = (unpack('Cn', $$data_ref))[1];
  95. if ($value > 100 && $value < 400) {
  96. $prefs->client($client)->set('width', $value);
  97. my $height = (unpack('Cnn', $$data_ref))[2];
  98. $prefs->client($client)->set('height', $height || 0);
  99. $client->display->modes($client->display->build_modes);
  100. $client->display->widthOverride(1, $value);
  101. $client->update;
  102. main::INFOLOG && $log->is_info && $log->info("Setting player $value" . "x" . "$height for ", $client->name);
  103. }
  104. }
  105. $client->SUPER::playerSettingsFrame($data_ref);
  106. }
  107. sub bass {
  108. my ($client, $new) = @_;
  109. my $value = $client->SUPER::bass($new);
  110. $client->update_equalizer($value, [2, 1, 3]) if defined $new;
  111. return $value;
  112. }
  113. sub treble {
  114. my ($client, $new) = @_;
  115. my $value = $client->SUPER::treble($new);
  116. $client->update_equalizer($value, [8, 9, 7]) if defined $new;
  117. return $value;
  118. }
  119. sub send_equalizer {
  120. my ($client, $equalizer) = @_;
  121. $equalizer ||= $prefs->client($client)->get('equalizer') || [(0) x 10];
  122. my $size = @$equalizer;
  123. my $data = pack("c[$size]", @{$equalizer});
  124. $client->sendFrame( eqlz => \$data );
  125. }
  126. sub update_equalizer {
  127. my ($client, $value, $index) = @_;
  128. return if $client->tone_update;
  129. my $equalizer = $prefs->client($client)->get('equalizer');
  130. $equalizer->[$index->[0]] = $value;
  131. $equalizer->[$index->[1]] = int($value / 2 + 0.5);
  132. $equalizer->[$index->[2]] = int($value / 4 + 0.5);
  133. $prefs->client($client)->set('equalizer', $equalizer);
  134. }
  135. sub update_tones {
  136. my ($client, $equalizer) = @_;
  137. $client->tone_update(1);
  138. $sprefs->client($client)->set('bass', int(($equalizer->[1] * 2 + $equalizer->[2] + $equalizer->[3] * 4) / 7 + 0.5));
  139. $sprefs->client($client)->set('treble', int(($equalizer->[7] * 4 + $equalizer->[8] + $equalizer->[9] * 2) / 7 + 0.5));
  140. $client->tone_update(0);
  141. }
  142. sub update_artwork {
  143. my $client = shift;
  144. my $cprefs = $prefs->client($client);
  145. my $artwork = $cprefs->get('artwork') || return;
  146. return unless $artwork->{'enable'};
  147. my $header = pack('Nnn', $artwork->{'enable'}, $artwork->{'x'}, $artwork->{'y'});
  148. $client->sendFrame( grfa => \$header );
  149. $client->display->update;
  150. my $s = min($cprefs->get('height') - $artwork->{'y'}, $cprefs->get('width') - $artwork->{'x'});
  151. my $params = { force => shift || 0 };
  152. my $path = 'music/current/cover_' . $s . 'x' . $s . '_o.jpg';
  153. my $body = Slim::Web::Graphics::artworkRequest($client, $path, $params, \&send_artwork, undef, HTTP::Response->new);
  154. send_artwork($client, undef, \$body) if $body;
  155. }
  156. sub send_artwork {
  157. my ($client, $params, $dataref) = @_;
  158. # I'm not sure why we are called so often, so only send when needed
  159. my $md5 = md5($$dataref);
  160. return if $client->pluginData('artwork_md5') eq $md5 && !$params->{'force'};
  161. $client->pluginData('artwork', $dataref);
  162. $client->pluginData('artwork_md5', $md5);
  163. my $artwork = $prefs->client($client)->get('artwork') || {};
  164. my $length = length $$dataref;
  165. my $offset = 0;
  166. $log->info("got resized artwork (length: ", length $$dataref, ")");
  167. my $header = pack('Nnn', $length, $artwork->{'x'}, $artwork->{'y'});
  168. while ($length > 0) {
  169. $length = 1280 if $length > 1280;
  170. $log->info("sending grfa $length");
  171. my $data = $header . pack('N', $offset) . substr( $$dataref, 0, $length, '' );
  172. $client->sendFrame( grfa => \$data );
  173. $offset += $length;
  174. $length = length $$dataref;
  175. }
  176. }
  177. sub clear_artwork {
  178. my ($client, $request) = @_;
  179. my $artwork = $prefs->client($client)->get('artwork');
  180. if ($artwork && $artwork->{'enable'}) {
  181. main::INFOLOG && $log->is_info && $log->info("artwork stop/clear " . $request->getRequestString());
  182. $client->pluginData('artwork_md5', '');
  183. # refresh screen and disable artwork when artwork was full screen (hack)
  184. if (!$artwork->{'x'} && !$artwork->{'y'}) {
  185. $client->sendFrame(grfa => \("\x00"x4)) unless $artwork->{'x'} || $artwork->{'y'};
  186. $client->display->update;
  187. }
  188. }
  189. }
  190. sub config_artwork {
  191. my ($client) = @_;
  192. if ( my $artwork = $prefs->client($client)->get('artwork') ) {
  193. my $header = pack('Nnn', $artwork->{'enable'}, $artwork->{'x'}, $artwork->{'y'});
  194. $client->sendFrame( grfa => \$header );
  195. $client->display->update;
  196. }
  197. }
  198. sub reconnect {
  199. my $client = shift;
  200. $client->SUPER::reconnect(@_);
  201. $client->pluginData('artwork_md5', '');
  202. $client->config_artwork;
  203. $client->send_equalizer;
  204. }
  205. # Change the analog output mode between headphone and sub-woofer
  206. # If no mode is specified, the value of the client's analogOutMode preference is used.
  207. # Otherwise the mode is temporarily changed to the given value without altering the preference.
  208. sub setAnalogOutMode {
  209. my $client = shift;
  210. # 0 = headphone (internal speakers off), 1 = sub out,
  211. # 2 = always on (internal speakers on), 3 = always off
  212. my $mode = shift;
  213. if (! defined $mode) {
  214. $mode = $sprefs->client($client)->get('analogOutMode');
  215. }
  216. my $data = pack('C', $mode);
  217. $client->sendFrame('audo', \$data);
  218. }
  219. # LINE_IN 0x01
  220. # LINE_OUT 0x02
  221. # HEADPHONE 0x04
  222. sub lineInConnected {
  223. my $state = Slim::Networking::Slimproto::voltage(shift) || return 0;
  224. return $state & 0x01 || 0;
  225. }
  226. sub lineOutConnected {
  227. my $state = Slim::Networking::Slimproto::voltage(shift) || return 0;
  228. return $state & 0x02 || 0;
  229. }
  230. sub lineInOutStatus {
  231. my ( $client, $data_ref ) = @_;
  232. my $state = unpack 'n', $$data_ref;
  233. my $oldState = {
  234. in => $client->lineInConnected(),
  235. out => $client->lineOutConnected(),
  236. };
  237. Slim::Networking::Slimproto::voltage( $client, $state );
  238. Slim::Control::Request::notifyFromArray( $client, [ 'lios', $state ] );
  239. if ($oldState->{in} != $client->lineInConnected()) {
  240. Slim::Control::Request::notifyFromArray( $client, [ 'lios', 'linein', $client->lineInConnected() ] );
  241. if ( Slim::Utils::PluginManager->isEnabled('Slim::Plugin::LineIn::Plugin')) {
  242. Slim::Plugin::LineIn::Plugin::lineInItem($client, 1);
  243. }
  244. }
  245. if ($oldState->{out} != $client->lineOutConnected()) {
  246. Slim::Control::Request::notifyFromArray( $client, [ 'lios', 'lineout', $client->lineOutConnected() ] );
  247. }
  248. }
  249. 1;