2
0

Player.pm 6.7 KB

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