Player.pm 9.0 KB

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