Player.pm 8.8 KB

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