File: audioin_raw.c

package info (click to toggle)
audacity 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 129,312 kB
  • sloc: ansic: 373,350; cpp: 276,880; sh: 56,060; python: 18,922; makefile: 10,309; lisp: 8,365; xml: 1,888; perl: 1,798; java: 1,551; asm: 545; pascal: 395; sed: 58; awk: 35
file content (116 lines) | stat: -rw-r--r-- 3,088 bytes parent folder | download | duplicates (7)
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
/*
 *	TwoLAME: an optimized MPEG Audio Layer Two encoder
 *
 *	Copyright (C) 2004-2007 The TwoLAME Project
 *
 *	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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id: frontend.h 156 2007-03-20 23:57:35Z nhumfrey $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "frontend.h"


/* We still use libsndfiles' SF_INFO structure for convenience */


static void print_info_raw(struct audioin_s *audioin)
{

    fprintf(stderr, "Raw input format: %d channels, %d-bit, %d Hz\n",
            audioin->sfinfo->channels, audioin->samplesize, audioin->sfinfo->samplerate);

}


/* Read in some audio samples into buffer */
static int read_raw(audioin_t * audioin, short *buffer, int samples)
{
    FILE *file = audioin->file;

    if (audioin->samplesize == 16) {
        return fread(buffer, 2, samples, file);
    } else {
        fprintf(stderr, "Error: sorry %d-bit samples are not supported at the moment.\n",
                audioin->samplesize);
    }

    // Error
    return -1;
}


/* Return error string (or NULL) */
static const char *error_str_raw(audioin_t * audioin)
{
    FILE *file = audioin->file;
    int error = ferror(file);
    if (error == 0)
        return NULL;
    return strerror(error);
}


static int close_raw(audioin_t * audioin)
{
    FILE *file = audioin->file;

    free(audioin);

    return fclose(file);
}


audioin_t *open_audioin_raw(char *filename, SF_INFO * sfinfo, int samplesize)
{
    audioin_t *audioin = NULL;


    // Allocate memory for structure
    audioin = malloc(sizeof(audioin_t));
    if (audioin == NULL) {
        fprintf(stderr, "Failed to allocate memory for audioin_t structure.\n");
        exit(ERR_MEM_ALLOC);
    }
    // Open the input file by filename
    if (strcmp(filename, "-") == 0) {
        // Use STDIN
        audioin->file = stdin;
    } else {
        audioin->file = fopen(filename, "rb");
    }

    // Check for errors
    if (audioin->file == NULL) {
        fprintf(stderr, "Failed to open input file (%s):\n", filename);
        fprintf(stderr, "  %s\n", strerror(errno));
        exit(ERR_OPENING_INPUT);
    }
    // Fill-in data structure
    audioin->samplesize = samplesize;
    audioin->sfinfo = sfinfo;
    audioin->print_info = print_info_raw;
    audioin->read = read_raw;
    audioin->error_str = error_str_raw;
    audioin->close = close_raw;


    return audioin;
}