summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/vtx.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/vtx.c')
-rw-r--r--lib/rbcodec/codecs/vtx.c138
1 files changed, 138 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/vtx.c b/lib/rbcodec/codecs/vtx.c
new file mode 100644
index 0000000000..031af946b9
--- /dev/null
+++ b/lib/rbcodec/codecs/vtx.c
@@ -0,0 +1,138 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * VTX Codec for rockbox based on the Ayumi engine
11 *
12 * Ayumi engine Written by Peter Sovietov in 2015
13 * Ported to rockbox '2019 by Roman Stolyarov
14 *
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
23 *
24 ****************************************************************************/
25
26#include <codecs/lib/codeclib.h>
27#include "libayumi/ayumi_render.h"
28
29CODEC_HEADER
30
31#define VTX_SAMPLE_RATE 44100
32
33/* Maximum number of bytes to process in one iteration */
34#define CHUNK_SIZE (1024*2)
35
36static int16_t samples[CHUNK_SIZE] IBSS_ATTR;
37extern ayumi_render_t ay;
38
39/****************** rockbox interface ******************/
40
41/* this is the codec entry point */
42enum codec_status codec_main(enum codec_entry_call_reason reason)
43{
44 if (reason == CODEC_LOAD) {
45 /* we only render 16 bits */
46 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
47
48 /* 44 Khz, Interleaved stereo */
49 ci->configure(DSP_SET_FREQUENCY, VTX_SAMPLE_RATE);
50 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
51 }
52
53 return CODEC_OK;
54}
55
56/* this is called for each file to process */
57enum codec_status codec_run(void)
58{
59 uint8_t *buf;
60 size_t n;
61 intptr_t param;
62 uint32_t elapsed_time;
63 long smp;
64 int res;
65
66 /* reset values */
67 elapsed_time = 0;
68 param = ci->id3->elapsed;
69
70 DEBUGF("VTX: next_track\n");
71 if (codec_init()) {
72 return CODEC_ERROR;
73 }
74
75 codec_set_replaygain(ci->id3);
76
77 /* Read the entire file */
78 DEBUGF("VTX: request file\n");
79 ci->seek_buffer(0);
80 buf = ci->request_buffer(&n, ci->filesize);
81 if (!buf || n < (size_t)ci->filesize) {
82 DEBUGF("VTX: file load failed\n");
83 return CODEC_ERROR;
84 }
85
86 res = AyumiRender_LoadFile((void *)buf, ci->filesize);
87 if (!res) {
88 DEBUGF("VTX: AyumiRender_LoadFile failed\n");
89 return CODEC_ERROR;
90 }
91
92 res = AyumiRender_AyInit(ay.info.chiptype, VTX_SAMPLE_RATE, ay.info.chipfreq, ay.info.playerfreq, 1);
93 if (!res) {
94 DEBUGF("VTX: AyumiRender_AyInit failed\n");
95 return CODEC_ERROR;
96 }
97
98 res = AyumiRender_SetLayout(ay.info.layout, 0);
99 if (!res) {
100 DEBUGF("VTX: AyumiRender_SetLayout failed\n");
101 return CODEC_ERROR;
102 }
103
104 if (param) {
105 goto resume_start;
106 }
107
108 /* The main decoder loop */
109 while (1) {
110 long action = ci->get_command(&param);
111
112 if (action == CODEC_ACTION_HALT)
113 break;
114
115 if (action == CODEC_ACTION_SEEK_TIME) {
116 resume_start:
117 ci->set_elapsed(param);
118 elapsed_time = param;
119 ulong sample = ((ulong)elapsed_time * 441) / 10;
120 AyumiRender_Seek(sample);
121 ci->seek_complete();
122 }
123
124 /* Generate audio buffer */
125 smp = AyumiRender_AySynth((void *)&samples[0], CHUNK_SIZE >> 1);
126
127 ci->pcmbuf_insert(samples, NULL, smp);
128
129 /* Set elapsed time for one track files */
130 elapsed_time += smp * 10 / 441;
131 ci->set_elapsed(elapsed_time);
132
133 if (AyumiRender_GetPos() >= AyumiRender_GetMaxPos())
134 break;
135 }
136
137 return CODEC_OK;
138}