File: openaptxenc.c

package info (click to toggle)
libopenaptx 0.2.0-6%2Bexp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 240 kB
  • sloc: ansic: 1,562; makefile: 169
file content (119 lines) | stat: -rw-r--r-- 4,270 bytes parent folder | download | duplicates (3)
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
/*
 * aptX encoder utility
 * Copyright (C) 2018-2020  Pali Rohár <pali.rohar@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

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

#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif

#include <openaptx.h>

static unsigned char input_buffer[512*3*2*4];
static unsigned char output_buffer[512*6];

int main(int argc, char *argv[])
{
    int i;
    int hd;
    int ret;
    size_t length;
    size_t processed;
    size_t written;
    struct aptx_context *ctx;

#ifdef _WIN32
    _setmode(_fileno(stdin), _O_BINARY);
    _setmode(_fileno(stdout), _O_BINARY);
#endif

    hd = 0;

    for (i = 1; i < argc; ++i) {
        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
            fprintf(stderr, "aptX encoder utility %d.%d.%d (using libopenaptx %d.%d.%d)\n", OPENAPTX_MAJOR, OPENAPTX_MINOR, OPENAPTX_PATCH, aptx_major, aptx_minor, aptx_patch);
            fprintf(stderr, "\n");
            fprintf(stderr, "This utility encodes a raw 24 bit signed stereo\n");
            fprintf(stderr, "samples from stdin to aptX or aptX HD on stdout\n");
            fprintf(stderr, "\n");
            fprintf(stderr, "Usage:\n");
            fprintf(stderr, "        %s [options]\n", argv[0]);
            fprintf(stderr, "\n");
            fprintf(stderr, "Options:\n");
            fprintf(stderr, "        -h, --help   Display this help\n");
            fprintf(stderr, "        --hd         Encode to aptX HD\n");
            fprintf(stderr, "\n");
            fprintf(stderr, "Examples:\n");
            fprintf(stderr, "\n");
            fprintf(stderr, "        %s < sample.s24le > sample.aptx\n", argv[0]);
            fprintf(stderr, "\n");
            fprintf(stderr, "        %s --hd < sample.s24le > sample.aptxhd\n", argv[0]);
            fprintf(stderr, "\n");
            fprintf(stderr, "        sox sample.wav -t raw -r 44.1k -L -e s -b 24 -c 2 - | %s > sample.aptx\n", argv[0]);
            return 1;
        } else if (strcmp(argv[i], "--hd") == 0) {
            hd = 1;
        } else {
            fprintf(stderr, "%s: Invalid option %s\n", argv[0], argv[i]);
            return 1;
        }
    }

    ctx = aptx_init(hd);
    if (!ctx) {
        fprintf(stderr, "%s: Cannot initialize aptX encoder\n", argv[0]);
        return 1;
    }

    ret = 0;

    while (!feof(stdin)) {
        length = fread(input_buffer, 1, sizeof(input_buffer), stdin);
        if (ferror(stdin)) {
            fprintf(stderr, "%s: aptX encoding failed to read input data\n", argv[0]);
            ret = 1;
        }
        if (length == 0)
            break;
        processed = aptx_encode(ctx, input_buffer, length, output_buffer, sizeof(output_buffer), &written);
        if (processed != length) {
            fprintf(stderr, "%s: aptX encoding stopped in the middle of the sample, dropped %lu byte%s\n", argv[0], (unsigned long)(length-processed), (length-processed != 1) ? "s" : "");
            ret = 1;
        }
        if (fwrite(output_buffer, 1, written, stdout) != written) {
            fprintf(stderr, "%s: aptX encoding failed to write encoded data\n", argv[0]);
            ret = 1;
            break;
        }
        if (processed != length)
            break;
    }

    if (aptx_encode_finish(ctx, output_buffer, sizeof(output_buffer), &written)) {
        if (fwrite(output_buffer, 1, written, stdout) != written) {
            fprintf(stderr, "%s: aptX encoding failed to write encoded data\n", argv[0]);
            ret = 1;
        }
    }

    aptx_finish(ctx);
    return ret;
}