Fix voicing of incorrect run time (top time instead of run time). Simplify runtime...
[kugel-rb.git] / apps / codecs / asap.c
blobc36a02369d25b3c8b76cbd32a2ee4f9c492ab178
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 Dominik Wenger
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 #include "codeclib.h"
23 #include "asap/asap.h"
25 CODEC_HEADER
27 #define CHUNK_SIZE (1024*2)
29 static byte samples[CHUNK_SIZE] IBSS_ATTR; /* The sample buffer */
30 static ASAP_State asap; /* asap codec state */
32 /* this is the codec entry point */
33 enum codec_status codec_main(void)
35 int n_bytes;
36 int song;
37 int duration;
38 char* module;
39 int bytesPerSample =2;
41 /* Generic codec initialisation */
42 ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512);
44 next_track:
45 if (codec_init()) {
46 DEBUGF("codec init failed\n");
47 return CODEC_ERROR;
50 while (!*ci->taginfo_ready && !ci->stop_codec)
51 ci->sleep(1);
53 codec_set_replaygain(ci->id3);
55 int bytes_done =0;
56 size_t filesize;
57 ci->seek_buffer(0);
58 module = ci->request_buffer(&filesize, ci->filesize);
59 if (!module || (size_t)filesize < (size_t)ci->filesize)
61 DEBUGF("loading error\n");
62 return CODEC_ERROR;
65 /*Init ASAP */
66 if (!ASAP_Load(&asap, ci->id3->path, module, filesize))
68 DEBUGF("%s: format not supported",ci->id3->path);
69 return CODEC_ERROR;
72 /* Make use of 44.1khz */
73 ci->configure(DSP_SET_FREQUENCY, 44100);
74 /* Sample depth is 16 bit little endian */
75 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
76 /* Stereo or Mono output ? */
77 if(asap.module_info.channels ==1)
79 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
80 bytesPerSample = 2;
82 else
84 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
85 bytesPerSample = 4;
87 /* reset eleapsed */
88 ci->set_elapsed(0);
90 song = asap.module_info.default_song;
91 duration = asap.module_info.durations[song];
92 if (duration < 0)
93 duration = 180 * 1000;
95 ASAP_PlaySong(&asap, song, duration);
96 ASAP_MutePokeyChannels(&asap, 0);
98 /* The main decoder loop */
99 while (1) {
100 ci->yield();
101 if (ci->stop_codec || ci->new_track)
102 break;
104 if (ci->seek_time) {
105 /* New time is ready in ci->seek_time */
107 /* seek to pos */
108 ASAP_Seek(&asap,ci->seek_time);
109 /* update elapsed */
110 ci->set_elapsed(ci->seek_time);
111 /* update bytes_done */
112 bytes_done = ci->seek_time*44.1*2;
113 /* seek ready */
114 ci->seek_complete();
117 /* Generate a buffer full of Audio */
118 #ifdef ROCKBOX_LITTLE_ENDIAN
119 n_bytes = ASAP_Generate(&asap, samples, sizeof(samples), ASAP_FORMAT_S16_LE);
120 #else
121 n_bytes = ASAP_Generate(&asap, samples, sizeof(samples), ASAP_FORMAT_S16_BE);
122 #endif
124 ci->pcmbuf_insert(samples, NULL, n_bytes /bytesPerSample);
126 bytes_done += n_bytes;
127 ci->set_elapsed((bytes_done / 2) / 44.1);
129 if(n_bytes != sizeof(samples))
130 break;
133 if (ci->request_next_track())
134 goto next_track;
136 return CODEC_OK;