summaryrefslogtreecommitdiff
path: root/cwav.c
blob: 59f3788d9bd6378b5c6c4f1d24dccdc2e3259848 (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
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009  Alexey Yakovenko

    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 program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
#include "codec.h"
#include "cwav.h"

typedef struct {
    uint32_t riffMagic;
    uint32_t size;
    uint32_t waveMagic;
    uint32_t fmtMagic;
    uint32_t chunkSize;
    uint16_t formatTag;
    uint16_t channels;
    uint32_t samplesPerSecond;
    uint32_t bytesPerSecond;
    uint16_t blockAlignment;
    uint16_t bitsPerSample;
    uint32_t dataMagic;
    uint32_t dataSize;
} wavHeader_t;

static FILE *file;

int cwav_init (const char *fname, int track, float start, float end) {

    file = fopen (fname, "rb");
    if (!file) {
        return -1;
    }

    wavHeader_t header;

    // read WAV header
    if (fread (&header, sizeof (header), 1, file) != 1)
    {
        printf ("WARNING: WAV header not found in %s\n", fname);
        fclose (file);
        file = NULL;
        return -1;
    }
    cwav.info.bitsPerSample = header.bitsPerSample;
    cwav.info.channels = header.channels;
    //cwav.info.dataSize = header.dataSize;
    cwav.info.samplesPerSecond = header.samplesPerSecond;

    return 0;
}

void
cwav_free (void)
{
    if (file)
    {
        fclose (file);
        file = NULL;
    }
}

int
cwav_read (char *bytes, int size) {
    int bytesread = 0;
    int first = 1;
    for (;;) {
        int bytesread = fread (bytes, 1, size, file);
        if (bytesread == size) {
            break;
        }
        size -= bytesread;
        bytes += bytesread;
        fseek (file, sizeof (wavHeader_t), SEEK_SET);
    }
    return 0;
}

codec_t cwav = {
    .init = cwav_init,
    .free = cwav_free,
    .read = cwav_read
};