2
0

Player.pm 8.5 KB

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