Move ARM assembler of musepack synthesis filter to own file. Additionally add ICONST_...
[kugel-rb.git] / apps / scrobbler.c
blobc1094392631fe7fc9ba1901156e0d6c33212a3e7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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
24 #include "file.h"
25 #include "sprintf.h"
26 #include "playback.h"
27 #include "logf.h"
28 #include "id3.h"
29 #include "kernel.h"
30 #include "audio.h"
31 #include "buffer.h"
32 #include "settings.h"
33 #include "ata_idle_notify.h"
34 #include "misc.h"
36 #if CONFIG_RTC
37 #include "time.h"
38 #include "timefuncs.h"
39 #endif
41 #include "scrobbler.h"
43 #define SCROBBLER_VERSION "1.0"
45 #if CONFIG_RTC
46 #define SCROBBLER_FILE "/.scrobbler.log"
47 #else
48 #define SCROBBLER_FILE "/.scrobbler-timeless.log"
49 #endif
51 /* increment this on any code change that effects output */
52 #define SCROBBLER_REVISION " $Revision$"
54 #define SCROBBLER_MAX_CACHE 32
55 /* longest entry I've had is 323, add a safety margin */
56 #define SCROBBLER_CACHE_LEN 512
58 static char* scrobbler_cache;
60 static int cache_pos;
61 static struct mp3entry scrobbler_entry;
62 static bool pending = false;
63 static bool scrobbler_initialised = false;
64 static bool scrobbler_ata_callback = false;
65 #if CONFIG_RTC
66 static time_t timestamp;
67 #else
68 static unsigned long timestamp;
69 #endif
71 /* Crude work-around for Archos Sims - return a set amount */
72 #if (CONFIG_CODEC != SWCODEC) && defined(SIMULATOR)
73 unsigned long audio_prev_elapsed(void)
75 return 120000;
77 #endif
79 static void write_cache(void)
81 int i;
82 int fd;
84 scrobbler_ata_callback = false;
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);
91 if(fd >= 0)
93 fdprintf(fd, "#AUDIOSCROBBLER/" SCROBBLER_VERSION "\n"
94 "#TZ/UNKNOWN\n"
95 #if CONFIG_RTC
96 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION "\n");
97 #else
98 "#CLIENT/Rockbox " TARGET_NAME SCROBBLER_REVISION " Timeless\n");
99 #endif
101 close(fd);
103 else
105 logf("SCROBBLER: cannot create log file");
109 /* write the cache entries */
110 fd = open(SCROBBLER_FILE, O_WRONLY | O_APPEND);
111 if(fd >= 0)
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));
120 close(fd);
122 else
124 logf("SCROBBLER: error writing file");
127 /* clear even if unsuccessful - don't want to overflow the buffer */
128 cache_pos = 0;
131 static bool scrobbler_flush_callback(void)
133 if (scrobbler_initialised && cache_pos)
134 write_cache();
135 return true;
138 static void add_to_cache(unsigned long play_length)
140 if ( cache_pos >= SCROBBLER_MAX_CACHE )
141 write_cache();
143 int ret;
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),
154 SCROBBLER_CACHE_LEN,
155 "%s\t%s\t%s\t%d\t%d\t%c\t%ld\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,
161 rating,
162 (long)timestamp);
163 } else {
164 ret = snprintf(scrobbler_cache+(SCROBBLER_CACHE_LEN*cache_pos),
165 SCROBBLER_CACHE_LEN,
166 "%s\t%s\t%s\t\t%d\t%c\t%ld\n",
167 scrobbler_entry.artist,
168 scrobbler_entry.album?scrobbler_entry.album:"",
169 scrobbler_entry.title,
170 (int)scrobbler_entry.length/1000,
171 rating,
172 (long)timestamp);
175 if ( ret >= SCROBBLER_CACHE_LEN )
177 logf("SCROBBLER: entry too long:");
178 logf("SCROBBLER: %s", scrobbler_entry.path);
179 } else {
180 cache_pos++;
181 if (!scrobbler_ata_callback)
183 register_ata_idle_func(scrobbler_flush_callback);
184 scrobbler_ata_callback = true;
190 void scrobbler_change_event(struct mp3entry *id)
192 /* add entry using the previous scrobbler_entry and timestamp */
193 if (pending)
194 add_to_cache(audio_prev_elapsed());
196 /* check if track was resumed > %50 played
197 check for blank artist or track name */
198 if ((id->elapsed > (id->length/2)) ||
199 (!id->artist ) || (!id->title ) )
201 pending = false;
202 logf("SCROBBLER: skipping file %s", id->path);
204 else
206 logf("SCROBBLER: add pending");
207 copy_mp3entry(&scrobbler_entry, id);
208 #if CONFIG_RTC
209 timestamp = mktime(get_time());
210 #else
211 timestamp = 0;
212 #endif
213 pending = true;
217 int scrobbler_init(void)
219 logf("SCROBBLER: init %d", global_settings.audioscrobbler);
221 if(!global_settings.audioscrobbler)
222 return -1;
224 scrobbler_cache = buffer_alloc(SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN);
226 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, scrobbler_change_event);
227 cache_pos = 0;
228 pending = false;
229 scrobbler_initialised = true;
231 return 1;
234 void scrobbler_flush_cache(void)
236 if (scrobbler_initialised)
238 /* Add any pending entries to the cache */
239 if(pending)
240 add_to_cache(audio_prev_elapsed());
242 /* Write the cache to disk if needed */
243 if (cache_pos)
244 write_cache();
246 pending = false;
250 void scrobbler_shutdown(void)
252 #ifndef SIMULATOR
253 if (scrobbler_ata_callback)
255 unregister_ata_idle_func(scrobbler_flush_callback, false);
256 scrobbler_ata_callback = false;
258 #endif
260 scrobbler_flush_cache();
262 if (scrobbler_initialised)
264 remove_event(PLAYBACK_EVENT_TRACK_CHANGE, scrobbler_change_event);
265 scrobbler_initialised = false;
269 void scrobbler_poweroff(void)
271 if (scrobbler_initialised && pending)
273 if ( audio_status() )
274 add_to_cache(audio_current_track()->elapsed);
275 else
276 add_to_cache(audio_prev_elapsed());
278 /* scrobbler_shutdown is called later, the cache will be written
279 * make sure the final track isn't added twice when that happens */
280 pending = false;
284 bool scrobbler_is_enabled(void)
286 return scrobbler_initialised;