Player.pm 9.5 KB

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