Player.pm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. $client->config_artwork;
  76. $client->send_equalizer;
  77. main::INFOLOG && $log->is_info && $log->info("SqueezeESP player connected: " . $client->id);
  78. }
  79. sub initPrefs {
  80. my $client = shift;
  81. $sprefs->client($client)->init($defaultPrefs);
  82. $prefs->client($client)->init( { equalizer => [(0) x 10] } );
  83. $client->SUPER::initPrefs;
  84. }
  85. # Allow the player to define it's display width (and probably more)
  86. sub playerSettingsFrame {
  87. my $client = shift;
  88. my $data_ref = shift;
  89. my $value;
  90. my $id = unpack('C', $$data_ref);
  91. # New SETD command 0xfe for display width & height
  92. if ($id == 0xfe) {
  93. $value = (unpack('Cn', $$data_ref))[1];
  94. if ($value > 100 && $value < 400) {
  95. $prefs->client($client)->set('width', $value);
  96. my $height = (unpack('Cnn', $$data_ref))[2];
  97. $prefs->client($client)->set('height', $height || 0);
  98. $client->display->modes($client->display->build_modes);
  99. $client->display->widthOverride(1, $value);
  100. $client->update;
  101. main::INFOLOG && $log->is_info && $log->info("Setting player $value" . "x" . "$height for ", $client->name);
  102. }
  103. }
  104. $client->SUPER::playerSettingsFrame($data_ref);
  105. }
  106. sub bass {
  107. my ($client, $new) = @_;
  108. my $value = $client->SUPER::bass($new);
  109. $client->update_equalizer($value, [2, 1, 3]) if defined $new;
  110. return $value;
  111. }
  112. sub treble {
  113. my ($client, $new) = @_;
  114. my $value = $client->SUPER::treble($new);
  115. $client->update_equalizer($value, [8, 9, 7]) if defined $new;
  116. return $value;
  117. }
  118. sub send_equalizer {
  119. my ($client, $equalizer) = @_;
  120. $equalizer ||= $prefs->client($client)->get('equalizer') || [(0) x 10];
  121. my $size = @$equalizer;
  122. my $data = pack("c[$size]", @{$equalizer});
  123. $client->sendFrame( eqlz => \$data );
  124. }
  125. sub update_equalizer {
  126. my ($client, $value, $index) = @_;
  127. return if $client->tone_update;
  128. my $equalizer = $prefs->client($client)->get('equalizer');
  129. $equalizer->[$index->[0]] = $value;
  130. $equalizer->[$index->[1]] = int($value / 2 + 0.5);
  131. $equalizer->[$index->[2]] = int($value / 4 + 0.5);
  132. $prefs->client($client)->set('equalizer', $equalizer);
  133. }
  134. sub update_tones {
  135. my ($client, $equalizer) = @_;
  136. $client->tone_update(1);
  137. $sprefs->client($client)->set('bass', int(($equalizer->[1] * 2 + $equalizer->[2] + $equalizer->[3] * 4) / 7 + 0.5));
  138. $sprefs->client($client)->set('treble', int(($equalizer->[7] * 4 + $equalizer->[8] + $equalizer->[9] * 2) / 7 + 0.5));
  139. $client->tone_update(0);
  140. }
  141. sub update_artwork {
  142. my $client = shift;
  143. my $cprefs = $prefs->client($client);
  144. my $artwork = $cprefs->get('artwork') || return;
  145. return unless $artwork->{'enable'};
  146. my $s = min($cprefs->get('height') - $artwork->{'y'}, $cprefs->get('width') - $artwork->{'x'});
  147. my $params = { force => shift || 0 };
  148. my $path = 'music/current/cover_' . $s . 'x' . $s . '_o.jpg';
  149. my $body = Slim::Web::Graphics::artworkRequest($client, $path, $params, \&send_artwork, undef, HTTP::Response->new);
  150. send_artwork($client, undef, \$body) if $body;
  151. }
  152. sub send_artwork {
  153. my ($client, $params, $dataref) = @_;
  154. # I'm not sure why we are called so often, so only send when needed
  155. my $md5 = md5($$dataref);
  156. return if $client->pluginData('artwork_md5') eq $md5 && !$params->{'force'};
  157. $client->pluginData('artwork', $dataref);
  158. $client->pluginData('artwork_md5', $md5);
  159. my $artwork = $prefs->client($client)->get('artwork') || {};
  160. my $length = length $$dataref;
  161. my $offset = 0;
  162. $log->info("got resized artwork (length: ", length $$dataref, ")");
  163. my $header = pack('Nnn', $length, $artwork->{'x'}, $artwork->{'y'});
  164. while ($length > 0) {
  165. $length = 1280 if $length > 1280;
  166. $log->info("sending grfa $length");
  167. my $data = $header . pack('N', $offset) . substr( $$dataref, 0, $length, '' );
  168. $client->sendFrame( grfa => \$data );
  169. $offset += $length;
  170. $length = length $$dataref;
  171. }
  172. }
  173. sub clear_artwork {
  174. my ($client, $request) = @_;
  175. my $artwork = $prefs->client($client)->get('artwork');
  176. if ($artwork && $artwork->{'enable'}) {
  177. main::INFOLOG && $log->is_info && $log->info("artwork stop/clear " . $request->getRequestString());
  178. $client->pluginData('artwork_md5', '');
  179. # refresh screen and disable artwork when artwork was full screen (hack)
  180. if (!$artwork->{'x'} && !$artwork->{'y'}) {
  181. $client->sendFrame(grfa => \("\x00"x4)) unless $artwork->{'x'} || $artwork->{'y'};
  182. $client->display->update;
  183. }
  184. }
  185. }
  186. sub config_artwork {
  187. my ($client) = @_;
  188. if ( my $artwork = $prefs->client($client)->get('artwork') ) {
  189. my $header = pack('Nnn', $artwork->{'enable'}, $artwork->{'x'}, $artwork->{'y'});
  190. $client->sendFrame( grfa => \$header );
  191. }
  192. }
  193. sub reconnect {
  194. my $client = shift;
  195. $client->pluginData('artwork_md5', '');
  196. $client->SUPER::reconnect(@_);
  197. }
  198. # Change the analog output mode between headphone and sub-woofer
  199. # If no mode is specified, the value of the client's analogOutMode preference is used.
  200. # Otherwise the mode is temporarily changed to the given value without altering the preference.
  201. sub setAnalogOutMode {
  202. my $client = shift;
  203. # 0 = headphone (internal speakers off), 1 = sub out,
  204. # 2 = always on (internal speakers on), 3 = always off
  205. my $mode = shift;
  206. if (! defined $mode) {
  207. $mode = $sprefs->client($client)->get('analogOutMode');
  208. }
  209. my $data = pack('C', $mode);
  210. $client->sendFrame('audo', \$data);
  211. }
  212. # LINE_IN 0x01
  213. # LINE_OUT 0x02
  214. # HEADPHONE 0x04
  215. sub lineInConnected {
  216. my $state = Slim::Networking::Slimproto::voltage(shift) || return 0;
  217. return $state & 0x01 || 0;
  218. }
  219. sub lineOutConnected {
  220. my $state = Slim::Networking::Slimproto::voltage(shift) || return 0;
  221. return $state & 0x02 || 0;
  222. }
  223. sub lineInOutStatus {
  224. my ( $client, $data_ref ) = @_;
  225. my $state = unpack 'n', $$data_ref;
  226. my $oldState = {
  227. in => $client->lineInConnected(),
  228. out => $client->lineOutConnected(),
  229. };
  230. Slim::Networking::Slimproto::voltage( $client, $state );
  231. Slim::Control::Request::notifyFromArray( $client, [ 'lios', $state ] );
  232. if ($oldState->{in} != $client->lineInConnected()) {
  233. Slim::Control::Request::notifyFromArray( $client, [ 'lios', 'linein', $client->lineInConnected() ] );
  234. if ( Slim::Utils::PluginManager->isEnabled('Slim::Plugin::LineIn::Plugin')) {
  235. Slim::Plugin::LineIn::Plugin::lineInItem($client, 1);
  236. }
  237. }
  238. if ($oldState->{out} != $client->lineOutConnected()) {
  239. Slim::Control::Request::notifyFromArray( $client, [ 'lios', 'lineout', $client->lineOutConnected() ] );
  240. }
  241. }
  242. 1;