|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Valve Corporation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without modification, |
| 6 | + * are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation and/or |
| 13 | + * other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +/* Dumps a "blank" Proton Audio sample. For documentation of the Proton Audio |
| 32 | + * format, see audioconv.rs. */ |
| 33 | + |
| 34 | +#include <stdio.h> |
| 35 | +#include <sys/stat.h> |
| 36 | +#include <fcntl.h> |
| 37 | +#include <unistd.h> |
| 38 | +#include <math.h> |
| 39 | +#include <opus/opus.h> |
| 40 | +#include <stdlib.h> |
| 41 | +#include <string.h> |
| 42 | +#include <errno.h> |
| 43 | + |
| 44 | +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x)) |
| 45 | + |
| 46 | +/* indexes into frame_sizes[] */ |
| 47 | +#define FRAME_2p5MS 0 |
| 48 | +#define FRAME_SMALLEST FRAME_2p5MS |
| 49 | +#define FRAME_5MS 1 |
| 50 | +#define FRAME_10MS 2 |
| 51 | +#define FRAME_20MS 3 |
| 52 | +#define FRAME_40MS 4 |
| 53 | +#define FRAME_60MS 5 |
| 54 | +#define FRAME_LARGEST FRAME_60MS |
| 55 | + |
| 56 | +/* sample properties */ |
| 57 | +static const int SAMPLERATE = 48000; |
| 58 | +static const int CHANNELS = 1; |
| 59 | +static const int SAMPLE_LENGTH = FRAME_10MS; /* must match value in audioconv.rs */ |
| 60 | +static const float AUDIBLE_HZ = 400.f; /* freq for the --audible switch */ |
| 61 | + |
| 62 | +static const int frame_sizes[] = { |
| 63 | + SAMPLERATE * 2.5 / 1000, |
| 64 | + SAMPLERATE * 5 / 1000, |
| 65 | + SAMPLERATE * 10 / 1000, |
| 66 | + SAMPLERATE * 20 / 1000, |
| 67 | + SAMPLERATE * 40 / 1000, |
| 68 | + SAMPLERATE * 60 / 1000, |
| 69 | +}; |
| 70 | + |
| 71 | +static float theta = 0.f; |
| 72 | + |
| 73 | +static const uint32_t FLAG_HEADER = 0x10000000; |
| 74 | +static const uint32_t AUDIOCONV_PADDING_LENGTH_SHIFT = 12; |
| 75 | + |
| 76 | +static int complete_write(const int file, const void *buf, const size_t len) |
| 77 | +{ |
| 78 | + size_t written = 0; |
| 79 | + ssize_t ret; |
| 80 | + while(written < len){ |
| 81 | + ret = write(file, ((const char *)buf) + written, len - written); |
| 82 | + if(ret < 0){ |
| 83 | + if(errno != EINTR && errno != EAGAIN) |
| 84 | + return 0; |
| 85 | + }else |
| 86 | + written += ret; |
| 87 | + } |
| 88 | + return written == len; |
| 89 | +} |
| 90 | + |
| 91 | +static int dump_hz(float hz, int samples, OpusEncoder *encoder, int outfile) |
| 92 | +{ |
| 93 | + int i = 0, j, c, frame_size, tot = 0, padding; |
| 94 | + opus_int32 written; |
| 95 | + uint32_t pkt_header; |
| 96 | + float val, *vals; |
| 97 | + unsigned char packet[4000 /* suggested by opus_encoder(3) */ ]; |
| 98 | + |
| 99 | + fprintf(stdout, "dumping %u samples at %f hz\n", samples, hz); |
| 100 | + |
| 101 | + vals = malloc(sizeof(float) * CHANNELS * frame_sizes[FRAME_LARGEST]); |
| 102 | + |
| 103 | + while(i < samples){ |
| 104 | + |
| 105 | + /* find the largest packet we can write with the samples remaining */ |
| 106 | + frame_size = -1; |
| 107 | + for(j = ARRAY_SIZE(frame_sizes) - 1; j >= 0; --j){ |
| 108 | + if(samples - i >= frame_sizes[j]){ |
| 109 | + frame_size = frame_sizes[j]; |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if(frame_size < 0){ |
| 115 | + /* couldn't fill a whole packet, so write a partial */ |
| 116 | + frame_size = frame_sizes[FRAME_SMALLEST]; |
| 117 | + padding = frame_size - (samples - i); |
| 118 | + }else |
| 119 | + padding = 0; |
| 120 | + |
| 121 | + for(j = 0; j < frame_size - padding; ++j){ |
| 122 | + val = sinf(theta); |
| 123 | + for(c = 0; c < CHANNELS; ++c) |
| 124 | + vals[j * CHANNELS + c] = val; |
| 125 | + theta += hz * 2.f * M_PI / (float)SAMPLERATE; |
| 126 | + while(theta >= 2.f * M_PI) |
| 127 | + theta -= 2.f * M_PI; |
| 128 | + } |
| 129 | + |
| 130 | + if(padding) |
| 131 | + memset(vals + (frame_size - padding) * CHANNELS, 0, sizeof(float) * padding * CHANNELS); |
| 132 | + |
| 133 | + written = opus_encode_float(encoder, vals, frame_size, packet, sizeof(packet)); |
| 134 | + if(written < 0){ |
| 135 | + fprintf(stderr, "fatal: opus_encode failed!!!\n"); |
| 136 | + free(vals); |
| 137 | + return 0; |
| 138 | + } |
| 139 | + |
| 140 | + pkt_header = written | (padding << AUDIOCONV_PADDING_LENGTH_SHIFT); |
| 141 | + |
| 142 | + fprintf(stdout, "encoded %u samples (%u are padding) to %u bytes\n", frame_size, padding, written); |
| 143 | + if(!complete_write(outfile, &pkt_header, sizeof(pkt_header))){ |
| 144 | + fprintf(stderr, "fatal: error writing packet header!!!\n"); |
| 145 | + free(vals); |
| 146 | + return 0; |
| 147 | + } |
| 148 | + if(!complete_write(outfile, packet, written)){ |
| 149 | + fprintf(stderr, "fatal: error writing packet contents!!!\n"); |
| 150 | + free(vals); |
| 151 | + return 0; |
| 152 | + } |
| 153 | + tot += written + sizeof(written); |
| 154 | + |
| 155 | + i += frame_size; |
| 156 | + } |
| 157 | + |
| 158 | + fprintf(stdout, "done dumping, %u bytes\n", tot); |
| 159 | + |
| 160 | + free(vals); |
| 161 | + |
| 162 | + return 1; |
| 163 | +} |
| 164 | + |
| 165 | +/* OGG's opus header (from RFC 7845 Section 5.1) */ |
| 166 | +struct __attribute__((__packed__)) opus_header { |
| 167 | + uint64_t magic; |
| 168 | + uint8_t version; |
| 169 | + uint8_t channels; |
| 170 | + uint16_t preskip; |
| 171 | + uint32_t input_samplerate; |
| 172 | + uint16_t output_gain; |
| 173 | + uint8_t mapping_family; |
| 174 | +}; |
| 175 | + |
| 176 | +static int dump_header(int outfile) |
| 177 | +{ |
| 178 | + struct opus_header header; |
| 179 | + uint32_t sz = sizeof(header) | FLAG_HEADER; |
| 180 | + |
| 181 | + static const char magic[] = "OpusHead"; |
| 182 | + |
| 183 | + memcpy(&header.magic, magic, sizeof(magic)); |
| 184 | + header.version = 1; |
| 185 | + header.channels = CHANNELS; |
| 186 | + header.preskip = 0; |
| 187 | + header.input_samplerate = SAMPLERATE; |
| 188 | + header.output_gain = 0; |
| 189 | + header.mapping_family = 0; /* FIXME: we need to support mc */ |
| 190 | + |
| 191 | + if(!complete_write(outfile, &sz, sizeof(sz))){ |
| 192 | + fprintf(stderr, "fatal: error writing opus header header!!!\n"); |
| 193 | + return 0; |
| 194 | + } |
| 195 | + |
| 196 | + if(!complete_write(outfile, &header, sizeof(header))){ |
| 197 | + fprintf(stderr, "fatal: error writing opus header!!!\n"); |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | + return 1; |
| 202 | +} |
| 203 | + |
| 204 | +void usage(const char *name) |
| 205 | +{ |
| 206 | + fprintf(stderr, "usage:\n"); |
| 207 | + fprintf(stderr, "\t%s [--help|-h] [--audible] <outfile>\n", name); |
| 208 | + fprintf(stderr, "\n"); |
| 209 | + fprintf(stderr, "\t--audible\tGenerate an audible sound clip instead of silence.\n"); |
| 210 | + fprintf(stderr, "\t--help -h\tPrint this help message.\n\n"); |
| 211 | +} |
| 212 | + |
| 213 | +int main(int argc, char **argv) |
| 214 | +{ |
| 215 | + int err, outfile, i, audible = 0; |
| 216 | + OpusEncoder *encoder; |
| 217 | + const char *outfile_name = NULL; |
| 218 | + |
| 219 | + for(i = 1; i < argc; ++i){ |
| 220 | + if(!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")){ |
| 221 | + usage(argv[0]); |
| 222 | + return 0; |
| 223 | + }else if(!strcmp(argv[i], "--audible")){ |
| 224 | + audible = 1; |
| 225 | + }else if(outfile_name){ |
| 226 | + fprintf(stderr, "error: too many arguments.\n\n"); |
| 227 | + usage(argv[0]); |
| 228 | + return 1; |
| 229 | + }else{ |
| 230 | + outfile_name = argv[i]; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if(!outfile_name){ |
| 235 | + fprintf(stderr, "error: missing filename.\n\n"); |
| 236 | + usage(argv[0]); |
| 237 | + return 1; |
| 238 | + } |
| 239 | + |
| 240 | + encoder = opus_encoder_create(SAMPLERATE, CHANNELS, OPUS_APPLICATION_AUDIO, &err); |
| 241 | + if(!encoder){ |
| 242 | + fprintf(stderr, "couldn't create opus encoder, err: 0x%x\n", err); |
| 243 | + return 1; |
| 244 | + } |
| 245 | + |
| 246 | + outfile = open(outfile_name, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 247 | + if(outfile < 0){ |
| 248 | + fprintf(stderr, "couldn't open file \"%s\": %s\n", outfile_name, strerror(errno)); |
| 249 | + opus_encoder_destroy(encoder); |
| 250 | + return 1; |
| 251 | + } |
| 252 | + |
| 253 | + if(!dump_header(outfile)){ |
| 254 | + close(outfile); |
| 255 | + unlink(outfile_name); |
| 256 | + opus_encoder_destroy(encoder); |
| 257 | + return 1; |
| 258 | + } |
| 259 | + |
| 260 | + if(!dump_hz(audible ? AUDIBLE_HZ : 0.f, frame_sizes[SAMPLE_LENGTH], encoder, outfile)){ |
| 261 | + close(outfile); |
| 262 | + unlink(outfile_name); |
| 263 | + opus_encoder_destroy(encoder); |
| 264 | + return 1; |
| 265 | + } |
| 266 | + |
| 267 | + close(outfile); |
| 268 | + opus_encoder_destroy(encoder); |
| 269 | + |
| 270 | + return 0; |
| 271 | +} |
0 commit comments