summaryrefslogtreecommitdiffstats
path: root/apps/plugins/wv2wav.c
blob: 909a0c3c6393ef786dc8d0fe8036a4497f534540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Christian Gmeiner
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"

#if (CONFIG_HWCODEC == MASNONE)
/* software codec platforms */

#include "lib/xxx2wav.h" /* Helper functions common to test decoders */
#include <codecs/libwavpack/wavpack.h>

#define BUFFER_SIZE 4096

static struct plugin_api* rb;
static file_info_struct file_info;
static long temp_buffer [BUFFER_SIZE] IDATA_ATTR;

/* Reformat samples from longs in processor's native endian mode to
 little-endian data with 2 bytes / sample. */
uchar* format_samples (int bps, uchar *dst, long *src, ulong samcnt)
{
  long temp;

  switch (bps) 
  {
    case 1:
      while (samcnt--)
      {
        *dst++ = (uchar)(temp = (*src++ << 8));
        *dst++ = (uchar)(temp >> 8);
      }

      break;

    case 2:
      while (samcnt--) 
      {
        *dst++ = (uchar)(temp = *src++);
        *dst++ = (uchar)(temp >> 8);
      }

      break;

    case 3:
      while (samcnt--) 
      {
        *dst++ = (uchar)(temp = (*src++ >> 8));
        *dst++ = (uchar)(temp >> 8);
      }

      break;

    case 4:
      while (samcnt--) 
      {
        *dst++ = (uchar)(temp = (*src++ >> 16));
        *dst++ = (uchar)(temp >> 8);
      }

      break;
  }

  return dst;
}

/* this is our function to decode a memory block from a file */
void wvpack_decode_data(file_info_struct* file_info, int samples_to_decode, WavpackContext **wpc)
{
  int bps = WavpackGetBytesPerSample(*wpc);
  /* nothing to decode */
  if (!samples_to_decode)
  {
    return;
  }
 
  /* decode now */
  ulong samples_unpacked = WavpackUnpackSamples(*wpc, temp_buffer, samples_to_decode);  

  if (samples_unpacked)
  {
    /* update some infos */
    file_info->current_sample += samples_unpacked;    
 
    /* for now, convert mono to stereo here, in place */
    if (WavpackGetReducedChannels (*wpc) == 1) {
      long *dst = temp_buffer + (samples_unpacked * 2);
      long *src = temp_buffer + samples_unpacked;
      long count = samples_unpacked;

      while (count--) {
        *--dst = *--src;
        *--dst = *src;
      }
    }
 
    format_samples (bps, (uchar *) temp_buffer, temp_buffer, samples_unpacked * file_info->channels);
    rb->write(file_info->outfile, temp_buffer, samples_unpacked * 4);
  }   
}

/* callback function for wavpack 
Maybe we do this at a lower level, but the
first thing is to get all working */
long Read(void* buffer, long size)
{
  long oldpos = file_info.curpos;
  
  if ((file_info.curpos + size) < file_info.filesize) 
  {   
    memcpy(buffer, &filebuf[file_info.curpos], size);
    file_info.curpos += size;
  } 
  else 
  {
    memcpy(buffer, &filebuf[file_info.curpos], file_info.filesize-file_info.curpos);
    file_info.curpos = file_info.filesize;
  }
    
  return (file_info.curpos - oldpos);
}

#ifdef USE_IRAM
extern char iramcopy[];
extern char iramstart[];
extern char iramend[];
#endif

/* this is the plugin entry point */
enum plugin_status plugin_start(struct plugin_api* api, void* file)
{
  WavpackContext *wpc;
  char error[80];
  
  /* generic plugin initialisation */
  TEST_PLUGIN_API(api);
  rb = api;

  #ifdef USE_IRAM
  rb->memcpy(iramstart, iramcopy, iramend-iramstart);
  #endif

  /* this function sets up the buffers and reads the file into RAM */
  if (local_init(file,"/wvtest.wav",&file_info,api)) 
  {
    return PLUGIN_ERROR;
  }
  
  /* setup wavpack */
  wpc = WavpackOpenFileInput(Read, error);

  /* was there an error? */
  if (!wpc) 
  {
    rb->splash(HZ*2, true, error);
    return PLUGIN_ERROR;
  }  

  /* grap/set some infos (forcing some to temp values) */
  file_info.channels = 2;
  file_info.total_samples = WavpackGetNumSamples(wpc);
  file_info.bitspersample = 16;
  file_info.samplerate = WavpackGetSampleRate(wpc);
  file_info.current_sample = 0;
  
  /* deciding loop */  
  file_info.start_tick=*(rb->current_tick);
  rb->button_clear_queue();

  while (file_info.current_sample < file_info.total_samples) 
  {
    wvpack_decode_data(&file_info, BUFFER_SIZE / file_info.channels, &wpc);

    display_status(&file_info);

    if (rb->button_get(false)!=BUTTON_NONE) 
    {
      close_wav(&file_info);
      return PLUGIN_OK;
    }
  }  
          
  close_wav(&file_info);    

  /* do some last checks */
  if ((WavpackGetNumSamples (wpc) != (ulong) -1) && (file_info.current_sample != WavpackGetNumSamples (wpc))) 
  {
    rb->splash(HZ*2, true, "incorrect number of samples!");
    return PLUGIN_ERROR;
  }  
  
  if (WavpackGetNumErrors (wpc)) {
    rb->splash(HZ*2, true, "crc errors detected!");
    return PLUGIN_ERROR;
  }
  
  rb->splash(HZ*2, true, "FINISHED!");

  return PLUGIN_OK;
}

#endif /* CONFIG_HWCODEC == MASNONE */