Skip to content

Commit

Permalink
piano: Select quality instead of audio format
Browse files Browse the repository at this point in the history
Pandora limited the audio formats we can request explicitly. Don’t rely
on this and use the formats available with the given partner
credentials. Closes #271.
  • Loading branch information
PromyLOPh committed Jun 9, 2012
1 parent b32348d commit 524abb9
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 61 deletions.
5 changes: 2 additions & 3 deletions contrib/pianobar.1
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ Increase volume.
Replacement for %@ in station format string. It's " @ " by default. Replacement for %@ in station format string. It's " @ " by default.


.TP .TP
.B audio_format = {aacplus-lofi,aacplus,mp3,mp3-hifi} .B audio_quality = {high, medium, low}
Select audio format. aacplus is default if both libraries (faad, mad) are Select audio quality.
available. mp3-hifi is available for Pandora One customers only.


.TP .TP
.B autoselect = {1,0} .B autoselect = {1,0}
Expand Down
4 changes: 4 additions & 0 deletions src/libpiano/piano.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ const char *PianoErrorToStr (PianoReturn_t ret) {
return "Wrong email address or password."; return "Wrong email address or password.";
break; break;


case PIANO_RET_QUALITY_UNAVAILABLE:
return "Selected audio quality is not available.";
break;

/* pandora error messages */ /* pandora error messages */
case PIANO_RET_P_INTERNAL: case PIANO_RET_P_INTERNAL:
return "Internal error."; return "Internal error.";
Expand Down
12 changes: 9 additions & 3 deletions src/libpiano/piano.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ typedef enum {
PIANO_AF_UNKNOWN = 0, PIANO_AF_UNKNOWN = 0,
PIANO_AF_AACPLUS = 1, PIANO_AF_AACPLUS = 1,
PIANO_AF_MP3 = 2, PIANO_AF_MP3 = 2,
PIANO_AF_MP3_HI = 3,
PIANO_AF_AACPLUS_LO = 4,
} PianoAudioFormat_t; } PianoAudioFormat_t;


typedef enum {
PIANO_AQ_UNKNOWN = 0,
PIANO_AQ_LOW = 1,
PIANO_AQ_MEDIUM = 2,
PIANO_AQ_HIGH = 3,
} PianoAudioQuality_t;

typedef struct PianoSong { typedef struct PianoSong {
char *artist; char *artist;
char *stationId; char *stationId;
Expand Down Expand Up @@ -178,7 +183,7 @@ typedef struct {


typedef struct { typedef struct {
PianoStation_t *station; PianoStation_t *station;
PianoAudioFormat_t format; PianoAudioQuality_t quality;
PianoSong_t *retPlaylist; PianoSong_t *retPlaylist;
} PianoRequestDataGetPlaylist_t; } PianoRequestDataGetPlaylist_t;


Expand Down Expand Up @@ -245,6 +250,7 @@ typedef enum {
PIANO_RET_CONTINUE_REQUEST = 3, PIANO_RET_CONTINUE_REQUEST = 3,
PIANO_RET_OUT_OF_MEMORY = 4, PIANO_RET_OUT_OF_MEMORY = 4,
PIANO_RET_INVALID_LOGIN = 5, PIANO_RET_INVALID_LOGIN = 5,
PIANO_RET_QUALITY_UNAVAILABLE = 6,


/* pandora error codes */ /* pandora error codes */
PIANO_RET_P_INTERNAL = PIANO_RET_OFFSET+0, PIANO_RET_P_INTERNAL = PIANO_RET_OFFSET+0,
Expand Down
31 changes: 0 additions & 31 deletions src/libpiano/request.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,34 +36,6 @@ THE SOFTWARE.
#include "piano.h" #include "piano.h"
#include "crypt.h" #include "crypt.h"


/* convert audio format id to string
* @param format id
* @return constant string
*/
static const char *PianoAudioFormatToString (PianoAudioFormat_t format) {
switch (format) {
case PIANO_AF_AACPLUS_LO:
return "HTTP_32_AACPLUS";
break;

case PIANO_AF_AACPLUS:
return "HTTP_64_AACPLUS";
break;

case PIANO_AF_MP3:
return "HTTP_128_MP3";
break;

case PIANO_AF_MP3_HI:
return "HTTP_192_MP3";
break;

default:
return NULL;
break;
}
}

/* prepare piano request (initializes request type, urlpath and postData) /* prepare piano request (initializes request type, urlpath and postData)
* @param piano handle * @param piano handle
* @param request structure * @param request structure
Expand Down Expand Up @@ -156,14 +128,11 @@ PianoReturn_t PianoRequest (PianoHandle_t *ph, PianoRequest_t *req,
assert (reqData != NULL); assert (reqData != NULL);
assert (reqData->station != NULL); assert (reqData->station != NULL);
assert (reqData->station->id != NULL); assert (reqData->station->id != NULL);
assert (reqData->format != PIANO_AF_UNKNOWN);


req->secure = true; req->secure = true;


json_object_object_add (j, "stationToken", json_object_object_add (j, "stationToken",
json_object_new_string (reqData->station->id)); json_object_new_string (reqData->station->id));
json_object_object_add (j, "additionalAudioUrl",
json_object_new_string (PianoAudioFormatToString (reqData->format)));


method = "station.getPlaylist"; method = "station.getPlaylist";
break; break;
Expand Down
35 changes: 33 additions & 2 deletions src/libpiano/response.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ PianoReturn_t PianoResponse (PianoHandle_t *ph, PianoRequest_t *req) {


assert (req->responseData != NULL); assert (req->responseData != NULL);
assert (reqData != NULL); assert (reqData != NULL);
assert (reqData->quality != PIANO_AQ_UNKNOWN);


json_object *items = json_object_object_get (result, "items"); json_object *items = json_object_object_get (result, "items");
assert (items != NULL); assert (items != NULL);
Expand All @@ -245,7 +246,37 @@ PianoReturn_t PianoResponse (PianoHandle_t *ph, PianoRequest_t *req) {
continue; continue;
} }


song->audioUrl = PianoJsonStrdup (s, "additionalAudioUrl"); /* get audio url based on selected quality */
static const char *qualityMap[] = {"", "lowQuality", "mediumQuality",
"highQuality"};
assert (reqData->quality < sizeof (qualityMap)/sizeof (*qualityMap));
static const char *formatMap[] = {"", "aacplus", "mp3"};
json_object *map = json_object_object_get (s, "audioUrlMap");
assert (map != NULL);

if (map != NULL) {
map = json_object_object_get (map, qualityMap[reqData->quality]);

if (map != NULL) {
const char *encoding = json_object_get_string (
json_object_object_get (map, "encoding"));
assert (encoding != NULL);
for (size_t k = 0; k < sizeof (formatMap)/sizeof (*formatMap); k++) {
if (strcmp (formatMap[k], encoding) == 0) {
song->audioFormat = k;
break;
}
}
song->audioUrl = PianoJsonStrdup (map, "audioUrl");
} else {
/* requested quality is not available */
ret = PIANO_RET_QUALITY_UNAVAILABLE;
free (song);
PianoDestroyPlaylist (playlist);
goto cleanup;
}
}

song->artist = PianoJsonStrdup (s, "artistName"); song->artist = PianoJsonStrdup (s, "artistName");
song->album = PianoJsonStrdup (s, "albumName"); song->album = PianoJsonStrdup (s, "albumName");
song->title = PianoJsonStrdup (s, "songName"); song->title = PianoJsonStrdup (s, "songName");
Expand All @@ -255,7 +286,6 @@ PianoReturn_t PianoResponse (PianoHandle_t *ph, PianoRequest_t *req) {
song->detailUrl = PianoJsonStrdup (s, "songDetailUrl"); song->detailUrl = PianoJsonStrdup (s, "songDetailUrl");
song->fileGain = json_object_get_double ( song->fileGain = json_object_get_double (
json_object_object_get (s, "trackGain")); json_object_object_get (s, "trackGain"));
song->audioFormat = reqData->format;
switch (json_object_get_int (json_object_object_get (s, switch (json_object_get_int (json_object_object_get (s,
"songRating"))) { "songRating"))) {
case 1: case 1:
Expand Down Expand Up @@ -665,6 +695,7 @@ PianoReturn_t PianoResponse (PianoHandle_t *ph, PianoRequest_t *req) {
} }
} }


cleanup:
json_object_put (j); json_object_put (j);


return ret; return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static void BarMainGetPlaylist (BarApp_t *app) {
WaitressReturn_t wRet; WaitressReturn_t wRet;
PianoRequestDataGetPlaylist_t reqData; PianoRequestDataGetPlaylist_t reqData;
reqData.station = app->curStation; reqData.station = app->curStation;
reqData.format = app->settings.audioFormat; reqData.quality = app->settings.audioQuality;


BarUiMsg (&app->settings, MSG_INFO, "Receiving new playlist... "); BarUiMsg (&app->settings, MSG_INFO, "Receiving new playlist... ");
if (!BarUiPianoCall (app, PIANO_REQUEST_GET_PLAYLIST, if (!BarUiPianoCall (app, PIANO_REQUEST_GET_PLAYLIST,
Expand Down
4 changes: 0 additions & 4 deletions src/player.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ void *BarPlayerThread (void *data) {


switch (player->audioFormat) { switch (player->audioFormat) {
#ifdef ENABLE_FAAD #ifdef ENABLE_FAAD
case PIANO_AF_AACPLUS_LO:
case PIANO_AF_AACPLUS: case PIANO_AF_AACPLUS:
player->aacHandle = NeAACDecOpen(); player->aacHandle = NeAACDecOpen();
/* set aac conf */ /* set aac conf */
Expand All @@ -443,7 +442,6 @@ void *BarPlayerThread (void *data) {


#ifdef ENABLE_MAD #ifdef ENABLE_MAD
case PIANO_AF_MP3: case PIANO_AF_MP3:
case PIANO_AF_MP3_HI:
mad_stream_init (&player->mp3Stream); mad_stream_init (&player->mp3Stream);
mad_frame_init (&player->mp3Frame); mad_frame_init (&player->mp3Frame);
mad_synth_init (&player->mp3Synth); mad_synth_init (&player->mp3Synth);
Expand Down Expand Up @@ -472,7 +470,6 @@ void *BarPlayerThread (void *data) {


switch (player->audioFormat) { switch (player->audioFormat) {
#ifdef ENABLE_FAAD #ifdef ENABLE_FAAD
case PIANO_AF_AACPLUS_LO:
case PIANO_AF_AACPLUS: case PIANO_AF_AACPLUS:
NeAACDecClose(player->aacHandle); NeAACDecClose(player->aacHandle);
free (player->sampleSize); free (player->sampleSize);
Expand All @@ -481,7 +478,6 @@ void *BarPlayerThread (void *data) {


#ifdef ENABLE_MAD #ifdef ENABLE_MAD
case PIANO_AF_MP3: case PIANO_AF_MP3:
case PIANO_AF_MP3_HI:
mad_synth_finish (&player->mp3Synth); mad_synth_finish (&player->mp3Synth);
mad_frame_finish (&player->mp3Frame); mad_frame_finish (&player->mp3Frame);
mad_stream_finish (&player->mp3Stream); mad_stream_finish (&player->mp3Stream);
Expand Down
24 changes: 8 additions & 16 deletions src/settings.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -121,13 +121,7 @@ void BarSettingsRead (BarSettings_t *settings) {
sizeof (dispatchActions) / sizeof (*dispatchActions)); sizeof (dispatchActions) / sizeof (*dispatchActions));


/* apply defaults */ /* apply defaults */
#ifdef ENABLE_FAAD settings->audioQuality = PIANO_AQ_HIGH;
settings->audioFormat = PIANO_AF_AACPLUS;
#else
#ifdef ENABLE_MAD
settings->audioFormat = PIANO_AF_MP3;
#endif
#endif
settings->autoselect = true; settings->autoselect = true;
settings->history = 5; settings->history = 5;
settings->volume = 0; settings->volume = 0;
Expand Down Expand Up @@ -226,15 +220,13 @@ void BarSettingsRead (BarSettings_t *settings) {
break; break;
} }
} }
} else if (streq ("audio_format", key)) { } else if (streq ("audio_quality", key)) {
if (streq (val, "aacplus")) { if (streq (val, "low")) {
settings->audioFormat = PIANO_AF_AACPLUS; settings->audioQuality = PIANO_AQ_LOW;
} else if (streq (val, "aacplus-lofi")) { } else if (streq (val, "medium")) {
settings->audioFormat = PIANO_AF_AACPLUS_LO; settings->audioQuality = PIANO_AQ_MEDIUM;
} else if (streq (val, "mp3")) { } else if (streq (val, "high")) {
settings->audioFormat = PIANO_AF_MP3; settings->audioQuality = PIANO_AQ_HIGH;
} else if (streq (val, "mp3-hifi")) {
settings->audioFormat = PIANO_AF_MP3_HI;
} }
} else if (streq ("autostart_station", key)) { } else if (streq ("autostart_station", key)) {
settings->autostartStation = strdup (val); settings->autostartStation = strdup (val);
Expand Down
2 changes: 1 addition & 1 deletion src/settings.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ typedef struct {
unsigned int history; unsigned int history;
int volume; int volume;
BarStationSorting_t sortOrder; BarStationSorting_t sortOrder;
PianoAudioFormat_t audioFormat; PianoAudioQuality_t audioQuality;
char *username; char *username;
char *password; char *password;
char *controlProxy; /* non-american listeners need this */ char *controlProxy; /* non-american listeners need this */
Expand Down

0 comments on commit 524abb9

Please sign in to comment.