1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Robert Keevil
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 Audioscrobbler spec at:
21 http://www.audioscrobbler.net/wiki/Portable_Player_Logging
40 #include "timefuncs.h"
43 #include "scrobbler.h"
45 #define SCROBBLER_VERSION "1.0"
48 #define SCROBBLER_FILE "/.scrobbler.log"
50 #define SCROBBLER_FILE "/.scrobbler-timeless.log"
53 /* increment this on any code change that effects output */
54 /* replace with CVS Revision keyword? */
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
;
63 static int scrobbler_fd
= -1;
65 static struct mp3entry scrobbler_entry
;
66 static bool pending
= false;
67 static bool scrobbler_initialised
= false;
69 static time_t timestamp
;
71 static unsigned long timestamp
;
74 /* Crude work-around for Archos Sims - return a set amount */
75 #if (CONFIG_CODEC != SWCODEC) && defined(SIMULATOR)
76 unsigned long audio_prev_elapsed(void)
82 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 scrobbler_fd
= open(SCROBBLER_FILE
, O_RDONLY
);
91 scrobbler_fd
= open(SCROBBLER_FILE
, O_RDWR
| O_CREAT
);
94 fdprintf(scrobbler_fd
, "#AUDIOSCROBBLER/%s\n", SCROBBLER_VERSION
);
95 fdprintf(scrobbler_fd
, "#TZ/UNKNOWN\n");
97 fdprintf(scrobbler_fd
,
98 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION
"\n");
100 fdprintf(scrobbler_fd
,
101 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION
" Timeless\n");
107 logf("SCROBBLER: cannot create log file");
113 /* write the cache entries */
114 scrobbler_fd
= open(SCROBBLER_FILE
, O_WRONLY
| O_APPEND
);
115 if(scrobbler_fd
>= 0)
117 logf("SCROBBLER: writing %d entries", cache_pos
);
119 for ( i
=0; i
< cache_pos
; i
++ )
121 logf("SCROBBLER: write %d", i
);
122 fdprintf(scrobbler_fd
, "%s", scrobbler_cache
+(SCROBBLER_CACHE_LEN
*i
));
128 logf("SCROBBLER: error writing file");
131 /* clear even if unsuccessful - don't want to overflow the buffer */
136 static void add_to_cache(void)
138 /* using HAVE_MMC to check for Ondios - anything better to use? */
140 #if defined(IPOD_NANO) || defined(HAVE_MMC)
141 if ( cache_pos
>= SCROBBLER_MAX_CACHE
)
143 if ( ( cache_pos
>= SCROBBLER_MAX_CACHE
) || ( ata_disk_is_active() ) )
145 #endif /* !SIMULATOR */
149 char rating
= 'S'; /* Skipped */
151 logf("SCROBBLER: add_to_cache[%d]", cache_pos
);
153 if ( audio_prev_elapsed() >
154 (scrobbler_entry
.length
/2) )
155 rating
= 'L'; /* Listened */
157 if (scrobbler_entry
.tracknum
> 0)
159 ret
= snprintf(scrobbler_cache
+(SCROBBLER_CACHE_LEN
*cache_pos
),
161 "%s\t%s\t%s\t%d\t%d\t%c\t%ld\n",
162 scrobbler_entry
.artist
,
163 scrobbler_entry
.album
?scrobbler_entry
.album
:"",
164 scrobbler_entry
.title
,
165 scrobbler_entry
.tracknum
,
166 (int)scrobbler_entry
.length
/1000,
170 ret
= snprintf(scrobbler_cache
+(SCROBBLER_CACHE_LEN
*cache_pos
),
172 "%s\t%s\t%s\t\t%d\t%c\t%ld\n",
173 scrobbler_entry
.artist
,
174 scrobbler_entry
.album
?scrobbler_entry
.album
:"",
175 scrobbler_entry
.title
,
176 (int)scrobbler_entry
.length
/1000,
181 if ( ret
>= SCROBBLER_CACHE_LEN
)
183 logf("SCROBBLER: entry too long:");
184 logf("SCROBBLER: %s", scrobbler_entry
.path
);
189 void scrobbler_change_event(struct mp3entry
*id
)
191 /* add entry using the previous scrobbler_entry and timestamp */
195 /* check if track was resumed > %50 played
196 check for blank artist or track name */
197 if ((id
->elapsed
> (id
->length
/2)) ||
198 (!id
->artist
) || (!id
->title
) )
201 logf("SCROBBLER: skipping file %s", id
->path
);
205 logf("SCROBBLER: add pending");
206 copy_mp3entry(&scrobbler_entry
, id
);
208 timestamp
= mktime(get_time());
216 int scrobbler_init(void)
218 logf("SCROBBLER: init %d", global_settings
.audioscrobbler
);
220 if(!global_settings
.audioscrobbler
)
223 scrobbler_cache
= buffer_alloc(SCROBBLER_MAX_CACHE
*SCROBBLER_CACHE_LEN
);
225 audio_set_track_changed_event(&scrobbler_change_event
);
228 scrobbler_initialised
= true;
233 void scrobbler_flush_cache(void)
235 if (scrobbler_initialised
)
237 /* Add any pending entries to the cache */
241 /* Write the cache to disk if needed */
249 void scrobbler_shutdown(void)
251 scrobbler_flush_cache();
253 if (scrobbler_initialised
)
255 audio_set_track_changed_event(NULL
);
256 scrobbler_initialised
= false;
260 bool scrobbler_is_enabled(void)
262 return scrobbler_initialised
;