summaryrefslogtreecommitdiff
path: root/apps/keymaps/keymap-sdl.c
blob: ce10c5cf174338cd99886c921d2c99f4ab2876b7 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Maurus Cuelenaere
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/* Button Code Definitions for Android targets */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "config.h"
#include "action.h"
#include "button.h"
#include "settings.h"

/*
 * The format of the list is as follows
 * { Action Code,   Button code,    Prereq button code }
 * if there's no need to check the previous button's value, use BUTTON_NONE
 * Insert LAST_ITEM_IN_LIST at the end of each mapping
 */

static const struct button_mapping button_context_standard[]  = {
    { ACTION_STD_PREV,        BUTTON_SCROLL_BACK,                BUTTON_NONE },
    { ACTION_STD_PREVREPEAT,  BUTTON_SCROLL_BACK|BUTTON_REPEAT,  BUTTON_NONE },
    { ACTION_STD_NEXT,        BUTTON_SCROLL_FWD,                 BUTTON_NONE },
    { ACTION_STD_NEXTREPEAT,  BUTTON_SCROLL_FWD|BUTTON_REPEAT,   BUTTON_NONE },
    
    { ACTION_STD_PREV,        BUTTON_UP,                 BUTTON_NONE },
    { ACTION_STD_PREVREPEAT,  BUTTON_UP|BUTTON_REPEAT,   BUTTON_NONE },
    { ACTION_STD_NEXT,        BUTTON_DOWN,               BUTTON_NONE },
    { ACTION_STD_NEXTREPEAT,  BUTTON_DOWN|BUTTON_REPEAT, BUTTON_NONE },

    { ACTION_STD_OK,          BUTTON_CENTER|BUTTON_REL,  BUTTON_CENTER },
    { ACTION_STD_OK,          BUTTON_RIGHT|BUTTON_REL,   BUTTON_RIGHT },
    { ACTION_STD_CANCEL,      BUTTON_LEFT,                    BUTTON_NONE },
    { ACTION_STD_CANCEL,      BUTTON_LEFT|BUTTON_REL,    BUTTON_LEFT },

    { ACTION_STD_CONTEXT,     BUTTON_MENU,                    BUTTON_NONE },

    LAST_ITEM_IN_LIST
}; /* button_context_standard */

static const struct button_mapping button_context_wps[]  = {
    { ACTION_WPS_BROWSE,        BUTTON_BACK,                 BUTTON_NONE },
    { ACTION_WPS_MENU,          BUTTON_MENU|BUTTON_REL,      BUTTON_MENU },
    { ACTION_WPS_CONTEXT,       BUTTON_MENU|BUTTON_REPEAT,   BUTTON_MENU },
    
    { ACTION_WPS_VOLUP,     BUTTON_SCROLL_FWD,                   BUTTON_NONE },
    { ACTION_WPS_VOLUP,     BUTTON_SCROLL_FWD|BUTTON_REPEAT,     BUTTON_NONE },
    { ACTION_WPS_VOLDOWN,   BUTTON_SCROLL_BACK,                  BUTTON_NONE },
    { ACTION_WPS_VOLDOWN,   BUTTON_SCROLL_BACK|BUTTON_REPEAT,    BUTTON_NONE },

    LAST_ITEM_IN_LIST
}; /* button_context_wps */

static const struct button_mapping button_context_list[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD)
}; /* button_context_list */

static const struct button_mapping button_context_tree[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_LIST)
}; /* button_context_tree */

static const struct button_mapping button_context_listtree_scroll_with_combo[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_CUSTOM|CONTEXT_TREE),
};

static const struct button_mapping button_context_listtree_scroll_without_combo[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_CUSTOM|CONTEXT_TREE),
};

static const struct button_mapping button_context_settings[]  = {
    { ACTION_SETTINGS_INC,          BUTTON_RIGHT,                BUTTON_NONE },
    { ACTION_SETTINGS_INCREPEAT,    BUTTON_RIGHT|BUTTON_REPEAT,  BUTTON_NONE },
    { ACTION_SETTINGS_DEC,          BUTTON_LEFT,                 BUTTON_NONE },
    { ACTION_SETTINGS_DECREPEAT,    BUTTON_LEFT|BUTTON_REPEAT,   BUTTON_NONE },
    { ACTION_STD_OK,                BUTTON_CENTER,               BUTTON_NONE },
    { ACTION_STD_CANCEL,            BUTTON_BACK,                      BUTTON_NONE },
    { ACTION_SETTINGS_INC,      BUTTON_SCROLL_FWD,                BUTTON_NONE },
    { ACTION_SETTINGS_INCREPEAT,BUTTON_SCROLL_FWD|BUTTON_REPEAT,  BUTTON_NONE },
    { ACTION_SETTINGS_DEC,      BUTTON_SCROLL_BACK,               BUTTON_NONE },
    { ACTION_SETTINGS_DECREPEAT,BUTTON_SCROLL_BACK|BUTTON_REPEAT, BUTTON_NONE },

    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD)
}; /* button_context_settings */

static const struct button_mapping button_context_settings_right_is_inc[]  = {
    { ACTION_SETTINGS_INC,       BUTTON_SCROLL_FWD,               BUTTON_NONE },
    { ACTION_SETTINGS_INCREPEAT, BUTTON_SCROLL_FWD|BUTTON_REPEAT, BUTTON_NONE },
    { ACTION_SETTINGS_DEC,       BUTTON_SCROLL_BACK,              BUTTON_NONE },
    { ACTION_SETTINGS_DECREPEAT, BUTTON_SCROLL_BACK|BUTTON_REPEAT,BUTTON_NONE },

    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD)
}; /* button_context_settingsgraphical */

static const struct button_mapping button_context_yesno[]  = {
    { ACTION_YESNO_ACCEPT,  BUTTON_CENTER,  BUTTON_NONE },

    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD)
}; /* button_context_settings_yesno */

static const struct button_mapping button_context_colorchooser[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_CUSTOM|CONTEXT_SETTINGS),
}; /* button_context_colorchooser */

static const struct button_mapping button_context_eq[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_CUSTOM|CONTEXT_SETTINGS),
}; /* button_context_eq */

/** Bookmark Screen **/
static const struct button_mapping button_context_bmark[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_LIST),
}; /* button_context_bmark */

static const struct button_mapping button_context_time[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_SETTINGS),
}; /* button_context_time */

static const struct button_mapping button_context_quickscreen[]  = {
    { ACTION_STD_CANCEL, BUTTON_BACK|BUTTON_REL,        BUTTON_NONE },

    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD)
}; /* button_context_quickscreen */

static const struct button_mapping button_context_pitchscreen[]  = {

    { ACTION_PS_INC_SMALL, BUTTON_RIGHT,                BUTTON_NONE },
    { ACTION_PS_INC_BIG,   BUTTON_RIGHT|BUTTON_REPEAT,  BUTTON_NONE },
    { ACTION_PS_DEC_SMALL, BUTTON_LEFT,                 BUTTON_NONE },
    { ACTION_PS_DEC_BIG,   BUTTON_LEFT|BUTTON_REPEAT,   BUTTON_NONE },
    { ACTION_PS_EXIT,      BUTTON_BACK,                      BUTTON_NONE },

    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD)
}; /* button_context_pitchcreen */

static const struct button_mapping button_context_keyboard[]  = {
    { ACTION_KBD_PAGE_FLIP,    BUTTON_MENU,                        BUTTON_NONE },
    { ACTION_KBD_CURSOR_LEFT,  BUTTON_LEFT,                   BUTTON_NONE },
    { ACTION_KBD_CURSOR_LEFT,  BUTTON_LEFT|BUTTON_REPEAT,     BUTTON_NONE },
    { ACTION_KBD_CURSOR_RIGHT, BUTTON_RIGHT,                  BUTTON_NONE },
    { ACTION_KBD_CURSOR_RIGHT, BUTTON_RIGHT|BUTTON_REPEAT,    BUTTON_NONE },

    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD)
}; /* button_context_keyboard */

static const struct button_mapping button_context_radio[]  = {
    LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_SETTINGS)
}; /* button_context_radio */

const struct button_mapping* target_get_context_mapping(int context)
{
    switch (context)
    {
        case CONTEXT_STD:
            return button_context_standard;
        case CONTEXT_WPS:
            return button_context_wps;

        case CONTEXT_LIST:
            return button_context_list;
        case CONTEXT_MAINMENU:
        case CONTEXT_TREE:
            if (global_settings.hold_lr_for_scroll_in_list)
                return button_context_listtree_scroll_without_combo;
            else
                return button_context_listtree_scroll_with_combo;
        case CONTEXT_CUSTOM|CONTEXT_TREE:
            return button_context_tree;

        case CONTEXT_SETTINGS:
            return button_context_settings;
        case CONTEXT_CUSTOM|CONTEXT_SETTINGS:
        case CONTEXT_SETTINGS_RECTRIGGER:
            return button_context_settings_right_is_inc;

        case CONTEXT_SETTINGS_COLOURCHOOSER:
            return button_context_colorchooser;
        case CONTEXT_SETTINGS_EQ:
            return button_context_eq;

        case CONTEXT_SETTINGS_TIME:
            return button_context_time;

        case CONTEXT_YESNOSCREEN:
            return button_context_yesno;
        case CONTEXT_FM:
            return button_context_radio;
        case CONTEXT_BOOKMARKSCREEN:
            return button_context_bmark;
        case CONTEXT_QUICKSCREEN:
            return button_context_quickscreen;
        case CONTEXT_PITCHSCREEN:
            return button_context_pitchscreen;
        case CONTEXT_KEYBOARD:
            return button_context_keyboard;
    }
    return button_context_standard;
}
opt">; wpc.config.bits_per_sample = (wpc.config.bytes_per_sample * 8) - ((wps->wphdr.flags & SHIFT_MASK) >> SHIFT_LSB); if (!wpc.config.sample_rate) { if (!wps || !wps->wphdr.block_samples || (wps->wphdr.flags & SRATE_MASK) == SRATE_MASK) wpc.config.sample_rate = 44100; else wpc.config.sample_rate = sample_rates [(wps->wphdr.flags & SRATE_MASK) >> SRATE_LSB]; } if (!wpc.config.num_channels) { wpc.config.num_channels = (wps->wphdr.flags & MONO_FLAG) ? 1 : 2; wpc.config.channel_mask = 0x5 - wpc.config.num_channels; } if (!(wps->wphdr.flags & FINAL_BLOCK)) wpc.reduced_channels = (wps->wphdr.flags & MONO_FLAG) ? 1 : 2; return &wpc; } // This function obtains general information about an open file and returns // a mask with the following bit values: // MODE_LOSSLESS: file is lossless (pure lossless only) // MODE_HYBRID: file is hybrid mode (lossy part only) // MODE_FLOAT: audio data is 32-bit ieee floating point // MODE_HIGH: file was created in "high" mode (information only) // MODE_FAST: file was created in "fast" mode (information only) int WavpackGetMode (WavpackContext *wpc) { int mode = 0; if (wpc) { if (wpc->config.flags & CONFIG_HYBRID_FLAG) mode |= MODE_HYBRID; else if (!(wpc->config.flags & CONFIG_LOSSY_MODE)) mode |= MODE_LOSSLESS; if (wpc->lossy_blocks) mode &= ~MODE_LOSSLESS; if (wpc->config.flags & CONFIG_FLOAT_DATA) mode |= MODE_FLOAT; if (wpc->config.flags & CONFIG_HIGH_FLAG) mode |= MODE_HIGH; if (wpc->config.flags & CONFIG_FAST_FLAG) mode |= MODE_FAST; } return mode; } // Unpack the specified number of samples from the current file position. // Note that "samples" here refers to "complete" samples, which would be // 2 int32_t's for stereo files. The audio data is returned right-justified in // 32-bit int32_t's in the endian mode native to the executing processor. So, // if the original data was 16-bit, then the values returned would be // +/-32k. Floating point data can also be returned if the source was // floating point data (and this is normalized to +/-1.0). The actual number // of samples unpacked is returned, which should be equal to the number // requested unless the end of fle is encountered or an error occurs. uint32_t WavpackUnpackSamples (WavpackContext *wpc, int32_t *buffer, uint32_t samples) { WavpackStream *wps = &wpc->stream; uint32_t bcount, samples_unpacked = 0, samples_to_unpack; int num_channels = wpc->config.num_channels; while (samples) { if (!wps->wphdr.block_samples || !(wps->wphdr.flags & INITIAL_BLOCK) || wps->sample_index >= wps->wphdr.block_index + wps->wphdr.block_samples) { bcount = read_next_header (wpc->infile, &wps->wphdr); if (bcount == (uint32_t) -1) break; if (wps->wphdr.version < MIN_STREAM_VERS || wps->wphdr.version > MAX_STREAM_VERS) { strcpy_loc (wpc->error_message, "invalid WavPack file!"); break; } if (!wps->wphdr.block_samples || wps->sample_index == wps->wphdr.block_index) if (!unpack_init (wpc)) break; } if (!wps->wphdr.block_samples || !(wps->wphdr.flags & INITIAL_BLOCK) || wps->sample_index >= wps->wphdr.block_index + wps->wphdr.block_samples) continue; if (wps->sample_index < wps->wphdr.block_index) { samples_to_unpack = wps->wphdr.block_index - wps->sample_index; if (samples_to_unpack > samples) samples_to_unpack = samples; wps->sample_index += samples_to_unpack; samples_unpacked += samples_to_unpack; samples -= samples_to_unpack; if (wpc->reduced_channels) samples_to_unpack *= wpc->reduced_channels; else samples_to_unpack *= num_channels; while (samples_to_unpack--) *buffer++ = 0; continue; } samples_to_unpack = wps->wphdr.block_index + wps->wphdr.block_samples - wps->sample_index; if (samples_to_unpack > samples) samples_to_unpack = samples; unpack_samples (wpc, buffer, samples_to_unpack); if (wpc->reduced_channels) buffer += samples_to_unpack * wpc->reduced_channels; else buffer += samples_to_unpack * num_channels; samples_unpacked += samples_to_unpack; samples -= samples_to_unpack; if (wps->sample_index == wps->wphdr.block_index + wps->wphdr.block_samples) { if (check_crc_error (wpc)) wpc->crc_errors++; } if (wps->sample_index == wpc->total_samples) break; } return samples_unpacked; } // Get total number of samples contained in the WavPack file, or -1 if unknown uint32_t WavpackGetNumSamples (WavpackContext *wpc) { return wpc ? wpc->total_samples : (uint32_t) -1; } // Get the current sample index position, or -1 if unknown uint32_t WavpackGetSampleIndex (WavpackContext *wpc) { if (wpc) return wpc->stream.sample_index; return (uint32_t) -1; } // Get the number of errors encountered so far int WavpackGetNumErrors (WavpackContext *wpc) { return wpc ? wpc->crc_errors : 0; } // return TRUE if any uncorrected lossy blocks were actually written or read int WavpackLossyBlocks (WavpackContext *wpc) { return wpc ? wpc->lossy_blocks : 0; } // Returns the sample rate of the specified WavPack file uint32_t WavpackGetSampleRate (WavpackContext *wpc) { return wpc ? wpc->config.sample_rate : 44100; } // Returns the number of channels of the specified WavPack file. Note that // this is the actual number of channels contained in the file, but this // version can only decode the first two. int WavpackGetNumChannels (WavpackContext *wpc) { return wpc ? wpc->config.num_channels : 2; } // Returns the actual number of valid bits per sample contained in the // original file, which may or may not be a multiple of 8. Floating data // always has 32 bits, integers may be from 1 to 32 bits each. When this // value is not a multiple of 8, then the "extra" bits are located in the // LSBs of the results. That is, values are right justified when unpacked // into int32_t's, but are left justified in the number of bytes used by the // original data. int WavpackGetBitsPerSample (WavpackContext *wpc) { return wpc ? wpc->config.bits_per_sample : 16; } // Returns the number of bytes used for each sample (1 to 4) in the original // file. This is required information for the user of this module because the // audio data is returned in the LOWER bytes of the int32_t buffer and must be // left-shifted 8, 16, or 24 bits if normalized int32_t's are required. int WavpackGetBytesPerSample (WavpackContext *wpc) { return wpc ? wpc->config.bytes_per_sample : 2; } // This function will return the actual number of channels decoded from the // file (which may or may not be less than the actual number of channels, but // will always be 1 or 2). Normally, this will be the front left and right // channels of a multi-channel file. int WavpackGetReducedChannels (WavpackContext *wpc) { if (wpc) return wpc->reduced_channels ? wpc->reduced_channels : wpc->config.num_channels; else return 2; } // Read from current file position until a valid 32-byte WavPack 4.0 header is // found and read into the specified pointer. The number of bytes skipped is // returned. If no WavPack header is found within 1 meg, then a -1 is returned // to indicate the error. No additional bytes are read past the header and it // is returned in the processor's native endian mode. Seeking is not required. static uint32_t read_next_header (read_stream infile, WavpackHeader *wphdr) { char buffer [sizeof (*wphdr)], *sp = buffer + sizeof (*wphdr), *ep = sp; uint32_t bytes_skipped = 0; int bleft; while (1) { if (sp < ep) { bleft = ep - sp; memcpy (buffer, sp, bleft); } else bleft = 0; if (infile (buffer + bleft, sizeof (*wphdr) - bleft) != (int32_t) sizeof (*wphdr) - bleft) return -1; sp = buffer; if (*sp++ == 'w' && *sp == 'v' && *++sp == 'p' && *++sp == 'k' && !(*++sp & 1) && sp [2] < 16 && !sp [3] && sp [5] == 4 && sp [4] >= (MIN_STREAM_VERS & 0xff) && sp [4] <= (MAX_STREAM_VERS & 0xff)) { memcpy (wphdr, buffer, sizeof (*wphdr)); little_endian_to_native (wphdr, WavpackHeaderFormat); return bytes_skipped; } while (sp < ep && *sp != 'w') sp++; if ((bytes_skipped += sp - buffer) > 1024 * 1024) return -1; } } // Open context for writing WavPack files. The returned context pointer is used // in all following calls to the library. A return value of NULL indicates // that memory could not be allocated for the context. WavpackContext *WavpackOpenFileOutput (void) { CLEAR (wpc); return &wpc; } // Set configuration for writing WavPack files. This must be done before // sending any actual samples, however it is okay to send wrapper or other // metadata before calling this. The "config" structure contains the following // required information: // config->bytes_per_sample see WavpackGetBytesPerSample() for info // config->bits_per_sample see WavpackGetBitsPerSample() for info // config->num_channels self evident // config->sample_rate self evident // In addition, the following fields and flags may be set: // config->flags: // -------------- // o CONFIG_HYBRID_FLAG select hybrid mode (must set bitrate) // o CONFIG_JOINT_STEREO select joint stereo (must set override also) // o CONFIG_JOINT_OVERRIDE override default joint stereo selection // o CONFIG_HYBRID_SHAPE select hybrid noise shaping (set override & // shaping_weight != 0.0) // o CONFIG_SHAPE_OVERRIDE override default hybrid noise shaping // (set CONFIG_HYBRID_SHAPE and shaping_weight) // o CONFIG_FAST_FLAG "fast" compression mode // o CONFIG_HIGH_FLAG "high" compression mode // o CONFIG_BITRATE_KBPS hybrid bitrate is kbps, not bits / sample // config->bitrate hybrid bitrate in either bits/sample or kbps // config->shaping_weight hybrid noise shaping coefficient override // config->float_norm_exp select floating-point data (127 for +/-1.0) // If the number of samples to be written is known then it should be passed // here. If the duration is not known then pass -1. In the case that the size // is not known (or the writing is terminated early) then it is suggested that // the application retrieve the first block written and let the library update // the total samples indication. A function is provided to do this update and // it should be done to the "correction" file also. If this cannot be done // (because a pipe is being used, for instance) then a valid WavPack will still // be created, but when applications want to access that file they will have // to seek all the way to the end to determine the actual duration. Also, if // a RIFF header has been included then it should be updated as well or the // WavPack file will not be directly unpackable to a valid wav file (although // it will still be usable by itself). A return of FALSE indicates an error. int WavpackSetConfiguration (WavpackContext *wpc, WavpackConfig *config, uint32_t total_samples) { WavpackStream *wps = &wpc->stream; uint32_t flags = (config->bytes_per_sample - 1), shift = 0; int num_chans = config->num_channels; int i; if ((wpc->config.flags & CONFIG_HYBRID_FLAG) || wpc->config.float_norm_exp || num_chans < 1 || num_chans > 2) return FALSE; wpc->total_samples = total_samples; wpc->config.sample_rate = config->sample_rate; wpc->config.num_channels = config->num_channels; wpc->config.bits_per_sample = config->bits_per_sample; wpc->config.bytes_per_sample = config->bytes_per_sample; wpc->config.flags = config->flags; shift = (config->bytes_per_sample * 8) - config->bits_per_sample; for (i = 0; i < 15; ++i) if (wpc->config.sample_rate == sample_rates [i]) break; flags |= i << SRATE_LSB; flags |= shift << SHIFT_LSB; flags |= CROSS_DECORR; if (!(config->flags & CONFIG_JOINT_OVERRIDE) || (config->flags & CONFIG_JOINT_STEREO)) flags |= JOINT_STEREO; flags |= INITIAL_BLOCK | FINAL_BLOCK; if (num_chans == 1) { flags &= ~(JOINT_STEREO | CROSS_DECORR | HYBRID_BALANCE); flags |= MONO_FLAG; } flags &= ~MAG_MASK; flags += (1 << MAG_LSB) * ((flags & BYTES_STORED) * 8 + 7); memcpy (wps->wphdr.ckID, "wvpk", 4); wps->wphdr.ckSize = sizeof (WavpackHeader) - 8; wps->wphdr.total_samples = wpc->total_samples; wps->wphdr.version = CUR_STREAM_VERS; wps->wphdr.flags = flags; pack_init (wpc); return TRUE; } // Add wrapper (currently RIFF only) to WavPack blocks. This should be called // before sending any audio samples. If the exact contents of the RIFF header // are not known because, for example, the file duration is uncertain or // trailing chunks are possible, simply write a "dummy" header of the correct // length. When all data has been written it will be possible to read the // first block written and update the header directly. An example of this can // be found in the Audition filter. void WavpackAddWrapper (WavpackContext *wpc, void *data, uint32_t bcount) { wpc->wrapper_data = data; wpc->wrapper_bytes = bcount; } // Start a WavPack block to be stored in the specified buffer. This must be // called before calling WavpackPackSamples(). Note that writing CANNOT wrap // in the buffer; the entire output block must fit in the buffer. int WavpackStartBlock (WavpackContext *wpc, uchar *begin, uchar *end) { wpc->stream.blockbuff = begin; wpc->stream.blockend = end; return pack_start_block (wpc); } // Pack the specified samples. Samples must be stored in int32_ts in the native // endian format of the executing processor. The number of samples specified // indicates composite samples (sometimes called "frames"). So, the actual // number of data points would be this "sample_count" times the number of // channels. The caller must decide how many samples to place in each // WavPack block (1/2 second is common), but this function may be called as // many times as desired to build the final block (and performs the actual // compression during the call). A return of FALSE indicates an error. int WavpackPackSamples (WavpackContext *wpc, int32_t *sample_buffer, uint32_t sample_count) { if (!sample_count || pack_samples (wpc, sample_buffer, sample_count)) return TRUE; strcpy_loc (wpc->error_message, "output buffer overflowed!"); return FALSE; } // Finish the WavPack block being built, returning the total size of the // block in bytes. Note that the possible conversion of the WavPack header to // little-endian takes place here. uint32_t WavpackFinishBlock (WavpackContext *wpc) { WavpackStream *wps = &wpc->stream; uint32_t bcount; pack_finish_block (wpc); bcount = ((WavpackHeader *) wps->blockbuff)->ckSize + 8; native_to_little_endian ((WavpackHeader *) wps->blockbuff, WavpackHeaderFormat); return bcount; } // Given the pointer to the first block written (to either a .wv or .wvc file), // update the block with the actual number of samples written. This should // be done if WavpackSetConfiguration() was called with an incorrect number // of samples (or -1). It is the responsibility of the application to read and // rewrite the block. An example of this can be found in the Audition filter. void WavpackUpdateNumSamples (WavpackContext *wpc, void *first_block) { little_endian_to_native (wpc, WavpackHeaderFormat); ((WavpackHeader *) first_block)->total_samples = WavpackGetSampleIndex (wpc); native_to_little_endian (wpc, WavpackHeaderFormat); } // Given the pointer to the first block written to a WavPack file, this // function returns the location of the stored RIFF header that was originally // written with WavpackAddWrapper(). This would normally be used to update // the wav header to indicate that a different number of samples was actually // written or if additional RIFF chunks are written at the end of the file. // It is the responsibility of the application to read and rewrite the block. // An example of this can be found in the Audition filter. void *WavpackGetWrapperLocation (void *first_block) { if (((uchar *) first_block) [32] == ID_RIFF_HEADER) return ((uchar *) first_block) + 34; else return NULL; }