stephen / nodetunes

AirTunes v2 Server implementation
215 stars 64 forks source link

Can't set Volume with my iOS or macOS Device #43

Open antonbrams opened 8 years ago

antonbrams commented 8 years ago

is it an issue? p.s. and when do you will update the version with fixed "server.stop()"?

jbonnett92 commented 7 years ago

Second this.

jbonnett92 commented 7 years ago

Ok after looking at the lib/rtspmethods.js file, I noticed that the volume had a onVolumeChange listener. With knowledge of that I created this:

'use strict';

var AirTunesServer = require('nodetunes');
var Speaker = require('speaker');
var Volume = require('pcm-volume');

var speaker = new Speaker({
  channels: 2,
  bitDepth: 16,
  sampleRate: 44100,
});
var volume = new Volume();
var server = new AirTunesServer({ serverName: 'Test Speaker' });

server.on('clientConnected', function(stream) {
  volume.pipe(speaker);
  stream.pipe(volume);
});

server.on('volumeChange', function(e) {
  if(e == -144) { // muted
    volume.setVolume(0);
  } else { // set volume
    var newVolume = (e - -30) * (1 - 0) / (0 - -30) + 0;
    volume.setVolume(newVolume);
  }
});

server.start();

All you need to do is copy and paste that code, also install the dependency npm install pcm-volume, you may want to add --save to the end when building program.

rafaelncarvalho commented 7 years ago

@jbonnett92 It's worked to change the volume, but when I disconnect and reconnect it, the sound is distorted, do you know what is causing this? I have tested with my computer and raspberry, both presented this issue.

jbonnett92 commented 7 years ago

@rafaelncarvalho Try this:

'use strict';

var AirTunesServer = require('nodetunes');
var Speaker = require('speaker');
var Volume = require('pcm-volume');

var speaker = new Speaker({
  channels: 2,
  bitDepth: 16,
  sampleRate: 44100,
});
var volume = new Volume();
volume.pipe(speaker);
var server = new AirTunesServer({ serverName: 'Test Speaker' });

server.on('clientConnected', function(stream) {
  stream.pipe(volume);
});

server.on('volumeChange', function(e) {
  if(e == -144) { // muted
    volume.setVolume(0);
  } else { // set volume
    var newVolume = (e - -30) * (1 - 0) / (0 - -30) + 0;
    volume.setVolume(newVolume);
  }
});

server.start();

Let me know if it does or doesn't work.

rafaelncarvalho commented 7 years ago

@jbonnett92 It works perfectly! Thank you very much!