1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006-2008 Robert Keevil
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 Audioscrobbler spec at:
23 http://www.audioscrobbler.net/wiki/Portable_Player_Logging
35 #include "ata_idle_notify.h"
37 #include "appevents.h"
41 #include "timefuncs.h"
44 #include "scrobbler.h"
46 #define SCROBBLER_VERSION "1.1"
49 #define SCROBBLER_FILE "/.scrobbler.log"
51 #define SCROBBLER_FILE "/.scrobbler-timeless.log"
54 /* increment this on any code change that effects output */
55 #define SCROBBLER_REVISION " $Revision$"
57 #define SCROBBLER_MAX_CACHE 32
58 /* longest entry I've had is 323, add a safety margin */
59 #define SCROBBLER_CACHE_LEN 512
61 static char* scrobbler_cache
;
64 static struct mp3entry scrobbler_entry
;
65 static bool pending
= false;
66 static bool scrobbler_initialised
= false;
68 static time_t timestamp
;
70 static unsigned long timestamp
;
73 /* Crude work-around for Archos Sims - return a set amount */
74 #if (CONFIG_CODEC != SWCODEC) && defined(SIMULATOR)
75 unsigned long audio_prev_elapsed(void)
81 static void write_cache(void)
86 /* If the file doesn't exist, create it.
87 Check at each write since file may be deleted at any time */
88 if(!file_exists(SCROBBLER_FILE
))
90 fd
= open(SCROBBLER_FILE
, O_RDWR
| O_CREAT
);
93 fdprintf(fd
, "#AUDIOSCROBBLER/" SCROBBLER_VERSION
"\n"
96 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION
"\n");
98 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION
" Timeless\n");
105 logf("SCROBBLER: cannot create log file");
109 /* write the cache entries */
110 fd
= open(SCROBBLER_FILE
, O_WRONLY
| O_APPEND
);
113 logf("SCROBBLER: writing %d entries", cache_pos
);
115 for ( i
=0; i
< cache_pos
; i
++ )
117 logf("SCROBBLER: write %d", i
);
118 fdprintf(fd
, "%s", scrobbler_cache
+(SCROBBLER_CACHE_LEN
*i
));
124 logf("SCROBBLER: error writing file");
127 /* clear even if unsuccessful - don't want to overflow the buffer */
131 static bool scrobbler_flush_callback(void)
133 if (scrobbler_initialised
&& cache_pos
)
138 static void add_to_cache(unsigned long play_length
)
140 if ( cache_pos
>= SCROBBLER_MAX_CACHE
)
144 char rating
= 'S'; /* Skipped */
146 logf("SCROBBLER: add_to_cache[%d]", cache_pos
);
148 if ( play_length
> (scrobbler_entry
.length
/2) )
149 rating
= 'L'; /* Listened */
151 if (scrobbler_entry
.tracknum
> 0)
153 ret
= snprintf(scrobbler_cache
+(SCROBBLER_CACHE_LEN
*cache_pos
),
155 "%s\t%s\t%s\t%d\t%d\t%c\t%ld\t%s\n",
156 scrobbler_entry
.artist
,
157 scrobbler_entry
.album
?scrobbler_entry
.album
:"",
158 scrobbler_entry
.title
,
159 scrobbler_entry
.tracknum
,
160 (int)scrobbler_entry
.length
/1000,
163 scrobbler_entry
.mb_track_id
?scrobbler_entry
.mb_track_id
:"");
165 ret
= snprintf(scrobbler_cache
+(SCROBBLER_CACHE_LEN
*cache_pos
),
167 "%s\t%s\t%s\t\t%d\t%c\t%ld\t%s\n",
168 scrobbler_entry
.artist
,
169 scrobbler_entry
.album
?scrobbler_entry
.album
:"",
170 scrobbler_entry
.title
,
171 (int)scrobbler_entry
.length
/1000,
174 scrobbler_entry
.mb_track_id
?scrobbler_entry
.mb_track_id
:"");
177 if ( ret
>= SCROBBLER_CACHE_LEN
)
179 logf("SCROBBLER: entry too long:");
180 logf("SCROBBLER: %s", scrobbler_entry
.path
);
183 register_storage_idle_func(scrobbler_flush_callback
);
188 void scrobbler_change_event(struct mp3entry
*id
)
190 /* add entry using the previous scrobbler_entry and timestamp */
192 add_to_cache(audio_prev_elapsed());
194 /* check if track was resumed > %50 played
195 check for blank artist or track name */
196 if ((id
->elapsed
> (id
->length
/2)) ||
197 (!id
->artist
) || (!id
->title
) )
200 logf("SCROBBLER: skipping file %s", id
->path
);
204 logf("SCROBBLER: add pending");
205 copy_mp3entry(&scrobbler_entry
, id
);
207 timestamp
= mktime(get_time());
215 int scrobbler_init(void)
217 logf("SCROBBLER: init %d", global_settings
.audioscrobbler
);
219 if(!global_settings
.audioscrobbler
)
222 scrobbler_cache
= buffer_alloc(SCROBBLER_MAX_CACHE
*SCROBBLER_CACHE_LEN
);
224 add_event(PLAYBACK_EVENT_TRACK_CHANGE
, false, scrobbler_change_event
);
227 scrobbler_initialised
= true;
232 void scrobbler_flush_cache(void)
234 if (scrobbler_initialised
)
236 /* Add any pending entries to the cache */
238 add_to_cache(audio_prev_elapsed());
240 /* Write the cache to disk if needed */
248 void scrobbler_shutdown(void)
250 scrobbler_flush_cache();
252 if (scrobbler_initialised
)
254 remove_event(PLAYBACK_EVENT_TRACK_CHANGE
, scrobbler_change_event
);
255 scrobbler_initialised
= false;
259 void scrobbler_poweroff(void)
261 if (scrobbler_initialised
&& pending
)
263 if ( audio_status() )
264 add_to_cache(audio_current_track()->elapsed
);
266 add_to_cache(audio_prev_elapsed());
268 /* scrobbler_shutdown is called later, the cache will be written
269 * make sure the final track isn't added twice when that happens */
274 bool scrobbler_is_enabled(void)
276 return scrobbler_initialised
;