Player.pm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. loudness => 0,
  94. artwork => undef,
  95. led_config => 0,
  96. led_visualizer => 0,
  97. led_brightness => 20,
  98. } );
  99. $prefs->setValidate({
  100. validator => sub {
  101. my ($pref, $new, $params, $old, $client) = @_;
  102. $new ||= [(0) x 10];
  103. foreach (0..9) {
  104. return 0 if $new->[$_] < $client->minBass;
  105. return 0 if $new->[$_] > $client->maxBass;
  106. }
  107. return 1;
  108. }
  109. }, 'equalizer');
  110. $client->SUPER::initPrefs;
  111. }
  112. sub power {
  113. my $client = shift;
  114. my $on = shift;
  115. my $res = $client->SUPER::power($on, @_);
  116. return $res unless defined $on;
  117. if ($on) {
  118. $client->update_artwork(1);
  119. } else {
  120. $client->clear_artwork(1);
  121. if ($client->hasLED) {
  122. Plugins::SqueezeESP32::RgbLed::updateLED($client, 0);
  123. }
  124. }
  125. return $res;
  126. }
  127. # Allow the player to define it's display width (and probably more)
  128. sub playerSettingsFrame {
  129. my $client = shift;
  130. my $data_ref = shift;
  131. my $value;
  132. my $id = unpack('C', $$data_ref);
  133. # New SETD command 0xfe for display width & height
  134. if ($id == 0xfe) {
  135. $value = (unpack('Cn', $$data_ref))[1];
  136. if ($value > 100 && $value < 400) {
  137. $prefs->client($client)->set('width', $value);
  138. my $height = (unpack('Cnn', $$data_ref))[2];
  139. $prefs->client($client)->set('height', $height || 0);
  140. $client->display->modes($client->display->build_modes);
  141. $client->display->widthOverride(1, $value);
  142. $client->update;
  143. main::INFOLOG && $log->is_info && $log->info("Setting player $value" . "x" . "$height for ", $client->name);
  144. }
  145. my $led_config = (unpack('Cnnn',$$data_ref))[3];
  146. $prefs->client($client)->set('led_config', $led_config);
  147. main::INFOLOG && $log->is_info && $led_config && $log->info("Setting led length $led_config for ", $client->name);
  148. }
  149. $client->SUPER::playerSettingsFrame($data_ref);
  150. }
  151. sub bass {
  152. my ($client, $new) = @_;
  153. my $value = $client->SUPER::bass($new);
  154. $client->update_equalizer($value, [2, 1, 3]) if defined $new;
  155. return $value;
  156. }
  157. sub treble {
  158. my ($client, $new) = @_;
  159. my $value = $client->SUPER::treble($new);
  160. $client->update_equalizer($value, [8, 9, 7]) if defined $new;
  161. return $value;
  162. }
  163. sub send_equalizer {
  164. my ($client, $equalizer) = @_;
  165. $equalizer ||= $prefs->client($client)->get('equalizer') || [(0) x 10];
  166. my $size = @$equalizer;
  167. my $data = pack("c[$size]", @{$equalizer});
  168. $client->sendFrame( eqlz => \$data );
  169. }
  170. sub send_loudness {
  171. my ($client, $loudness) = @_;
  172. $loudness //= $prefs->client($client)->get('loudness');
  173. my $data = pack("c1", $loudness);
  174. $client->sendFrame( loud => \$data );
  175. }
  176. sub update_equalizer {
  177. my ($client, $value, $index) = @_;
  178. return if $client->tone_update;
  179. my $equalizer = $prefs->client($client)->get('equalizer');
  180. $equalizer->[$index->[0]] = $value;
  181. $equalizer->[$index->[1]] = int($value / 2 + 0.5);
  182. $equalizer->[$index->[2]] = int($value / 4 + 0.5);
  183. $prefs->client($client)->set('equalizer', $equalizer);
  184. }
  185. sub update_tones {
  186. my ($client, $equalizer) = @_;
  187. $client->tone_update(1);
  188. $sprefs->client($client)->set('bass', int(($equalizer->[1] * 2 + $equalizer->[2] + $equalizer->[3] * 4) / 7 + 0.5));
  189. $sprefs->client($client)->set('treble', int(($equalizer->[7] * 4 + $equalizer->[8] + $equalizer->[9] * 2) / 7 + 0.5));
  190. $client->tone_update(0);
  191. }
  192. sub update_artwork {
  193. my $client = shift;
  194. my $cprefs = $prefs->client($client);
  195. my $artwork = $cprefs->get('artwork') || return;
  196. return unless $artwork->{'enable'} && $client->display->isa("Plugins::SqueezeESP32::Graphics");
  197. my $header = pack('Nnn', $artwork->{'enable'}, $artwork->{'x'}, $artwork->{'y'});
  198. $client->sendFrame( grfa => \$header );
  199. $client->display->update;
  200. my $s = min($cprefs->get('height') - $artwork->{'y'}, $cprefs->get('width') - $artwork->{'x'});
  201. my $params = { force => shift || 0 };
  202. my $path = 'music/current/cover_' . $s . 'x' . $s . '_o.jpg';
  203. my $body = Slim::Web::Graphics::artworkRequest($client, $path, $params, \&send_artwork, undef, HTTP::Response->new);
  204. send_artwork($client, undef, \$body) if $body;
  205. }
  206. sub send_artwork {
  207. my ($client, $params, $dataref) = @_;
  208. # I'm not sure why we are called so often, so only send when needed
  209. my $md5 = md5($$dataref);
  210. return if $client->pluginData('artwork_md5') eq $md5 && !$params->{'force'};
  211. $client->pluginData('artwork', $dataref);
  212. $client->pluginData('artwork_md5', $md5);
  213. my $artwork = $prefs->client($client)->get('artwork') || {};
  214. my $length = length $$dataref;
  215. my $offset = 0;
  216. $log->info("got resized artwork (length: ", length $$dataref, ")");
  217. my $header = pack('Nnn', $length, $artwork->{'x'}, $artwork->{'y'});
  218. while ($length > 0) {
  219. $length = 1280 if $length > 1280;
  220. $log->info("sending grfa $length");
  221. my $data = $header . pack('N', $offset) . substr( $$dataref, 0, $length, '' );
  222. $client->sendFrame( grfa => \$data );
  223. $offset += $length;
  224. $length = length $$dataref;
  225. }
  226. }
  227. sub clear_artwork {
  228. my ($client, $force, $from) = @_;
  229. my $artwork = $prefs->client($client)->get('artwork');
  230. if ($artwork && $artwork->{'enable'}) {
  231. main::INFOLOG && $log->is_info && $log->info("artwork stop/clear " . ($from || ""));
  232. $client->pluginData('artwork_md5', '');
  233. # refresh screen and disable artwork when artwork was full screen (hack)
  234. if ((!$artwork->{'x'} && !$artwork->{'y'}) || $force) {
  235. $client->sendFrame(grfa => \("\x00"x4));
  236. $client->display->update;
  237. }
  238. }
  239. }
  240. sub config_artwork {
  241. my ($client) = @_;
  242. if ( my $artwork = $prefs->client($client)->get('artwork') ) {
  243. my $header = pack('Nnn', $artwork->{'enable'}, $artwork->{'x'}, $artwork->{'y'});
  244. $client->sendFrame( grfa => \$header );
  245. $client->display->update;
  246. }
  247. }
  248. sub reconnect {
  249. my $client = shift;
  250. $client->SUPER::reconnect(@_);
  251. $client->pluginData('artwork_md5', '');
  252. $client->config_artwork if $client->display->isa("Plugins::SqueezeESP32::Graphics");
  253. $client->send_equalizer;
  254. $client->send_loudness;
  255. }
  256. # Change the analog output mode between headphone and sub-woofer
  257. # If no mode is specified, the value of the client's analogOutMode preference is used.
  258. # Otherwise the mode is temporarily changed to the given value without altering the preference.
  259. sub setAnalogOutMode {
  260. my $client = shift;
  261. # 0 = headphone (internal speakers off), 1 = sub out,
  262. # 2 = always on (internal speakers on), 3 = always off
  263. my $mode = shift;
  264. if (! defined $mode) {
  265. $mode = $sprefs->client($client)->get('analogOutMode');
  266. }
  267. my $data = pack('C', $mode);
  268. $client->sendFrame('audo', \$data);
  269. }
  270. # LINE_IN 0x01
  271. # LINE_OUT 0x02
  272. # HEADPHONE 0x04
  273. sub lineInConnected {
  274. my $state = Slim::Networking::Slimproto::voltage(shift) || return 0;
  275. return $state & 0x01 || 0;
  276. }
  277. sub lineOutConnected {
  278. my $state = Slim::Networking::Slimproto::voltage(shift) || return 0;
  279. return $state & 0x02 || 0;
  280. }
  281. sub lineInOutStatus {
  282. my ( $client, $data_ref ) = @_;
  283. my $state = unpack 'n', $$data_ref;
  284. my $oldState = {
  285. in => $client->lineInConnected(),
  286. out => $client->lineOutConnected(),
  287. };
  288. Slim::Networking::Slimproto::voltage( $client, $state );
  289. Slim::Control::Request::notifyFromArray( $client, [ 'lios', $state ] );
  290. if ($oldState->{in} != $client->lineInConnected()) {
  291. Slim::Control::Request::notifyFromArray( $client, [ 'lios', 'linein', $client->lineInConnected() ] );
  292. if ( Slim::Utils::PluginManager->isEnabled('Slim::Plugin::LineIn::Plugin')) {
  293. Slim::Plugin::LineIn::Plugin::lineInItem($client, 1);
  294. }
  295. }
  296. if ($oldState->{out} != $client->lineOutConnected()) {
  297. Slim::Control::Request::notifyFromArray( $client, [ 'lios', 'lineout', $client->lineOutConnected() ] );
  298. }
  299. }
  300. sub voltage {
  301. my $voltage = Slim::Networking::Slimproto::voltage(shift) || return 0;
  302. return sprintf("%.2f", ($voltage >> 4) / 128);
  303. }
  304. 1;