Player.pm 9.6 KB

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