summaryrefslogtreecommitdiff
path: root/src/filewriter/flac.cc
blob: 13064d929a8190570707af2e9b1197e79611d710 (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
/*  FileWriter FLAC Plugin
 *  Copyright (c) 2007 William Pitcock <nenolod@sacredspiral.co.uk>
 *
 *  Partially derived from Og(g)re - Ogg-Output-Plugin:
 *  Copyright (c) 2002 Lars Siebold <khandha5@gmx.net>
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "filewriter.h"

#ifdef FILEWRITER_FLAC

#include <FLAC/all.h>

#include <libaudcore/audstrings.h>

static int channels;
static FLAC__StreamEncoder *flac_encoder;
static FLAC__StreamMetadata *flac_metadata;

static FLAC__StreamEncoderWriteStatus flac_write_cb(const FLAC__StreamEncoder *encoder,
    const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void * data)
{
    VFSFile *file = (VFSFile *) data;

    if (file->fwrite (buffer, 1, bytes) != (int64_t) bytes)
        return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;

    return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
}

static FLAC__StreamEncoderSeekStatus flac_seek_cb(const FLAC__StreamEncoder *encoder,
    FLAC__uint64 absolute_byte_offset, void * data)
{
    VFSFile *file = (VFSFile *) data;

    if (file->fseek (absolute_byte_offset, VFS_SEEK_SET) < 0)
        return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;

    return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
}

static FLAC__StreamEncoderTellStatus flac_tell_cb(const FLAC__StreamEncoder *encoder,
    FLAC__uint64 *absolute_byte_offset, void * data)
{
    VFSFile *file = (VFSFile *) data;

    *absolute_byte_offset = file->ftell ();

    return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
}

static void insert_vorbis_comment (FLAC__StreamMetadata * meta,
 const char * name, const Tuple & tuple, Tuple::Field field)
{
    Tuple::ValueType type = Tuple::field_get_type (field);
    if (tuple.get_value_type (field) != type)
        return;

    StringBuf temp;

    switch (type)
    {
    case Tuple::Int:
        temp = str_printf ("%s=%d", name, tuple.get_int (field));
        break;

    case Tuple::String:
        temp = str_printf ("%s=%s", name, (const char *) tuple.get_str (field));
        break;

    default:
        return;
    }

    FLAC__StreamMetadata_VorbisComment_Entry comment;
    comment.length = temp.len ();
    comment.entry = (unsigned char *) (char *) temp;

    FLAC__metadata_object_vorbiscomment_insert_comment (meta,
     meta->data.vorbis_comment.num_comments, comment, true);
}

static bool flac_open (VFSFile & file, const format_info & info, const Tuple & tuple)
{
    flac_encoder = FLAC__stream_encoder_new();

    FLAC__stream_encoder_set_channels(flac_encoder, info.channels);
    FLAC__stream_encoder_set_sample_rate(flac_encoder, info.frequency);

    flac_metadata = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);

    insert_vorbis_comment (flac_metadata, "TITLE", tuple, Tuple::Title);
    insert_vorbis_comment (flac_metadata, "ARTIST", tuple, Tuple::Artist);
    insert_vorbis_comment (flac_metadata, "ALBUM", tuple, Tuple::Album);
    insert_vorbis_comment (flac_metadata, "GENRE", tuple, Tuple::Genre);
    insert_vorbis_comment (flac_metadata, "COMMENT", tuple, Tuple::Comment);
    insert_vorbis_comment (flac_metadata, "DATE", tuple, Tuple::Date);
    insert_vorbis_comment (flac_metadata, "YEAR", tuple, Tuple::Year);
    insert_vorbis_comment (flac_metadata, "TRACKNUMBER", tuple, Tuple::Track);

    FLAC__stream_encoder_set_metadata(flac_encoder, &flac_metadata, 1);

    FLAC__stream_encoder_init_stream(flac_encoder, flac_write_cb, flac_seek_cb,
     flac_tell_cb, nullptr, &file);

    channels = info.channels;
    return true;
}

static void flac_write (VFSFile & file, const void * data, int length)
{
#if 1
    FLAC__int32 *encbuffer[2];
    int16_t *tmpdata = (int16_t *) data;
    int i;

    encbuffer[0] = new FLAC__int32[length / channels];
    encbuffer[1] = new FLAC__int32[length / channels];

    if (channels == 1)
    {
        for (i = 0; i < (length / 2); i++)
        {
            encbuffer[0][i] = tmpdata[i];
            encbuffer[1][i] = tmpdata[i];
        }
    }
    else
    {
        for (i = 0; i < (length / 4); i++)
        {
            encbuffer[0][i] = tmpdata[2 * i];
            encbuffer[1][i] = tmpdata[2 * i + 1];
        }
    }

    FLAC__stream_encoder_process(flac_encoder, (const FLAC__int32 **)encbuffer, length / (channels * 2));

    delete[] encbuffer[0];
    delete[] encbuffer[1];
#else
    FLAC__int32 *encbuffer;
    int16_t *tmpdata = (int16_t *) data;
    int i;

    encbuffer = new FLAC__int32[length];

    for (i=0; i < length; i++) {
        encbuffer[i] = tmpdata[i];
    }

    FLAC__stream_encoder_process_interleaved(flac_encoder, encbuffer, length);

    delete[] encbuffer;
#endif
}

static void flac_close (VFSFile & file)
{
    if (flac_encoder)
    {
        FLAC__stream_encoder_finish(flac_encoder);
        FLAC__stream_encoder_delete(flac_encoder);
        flac_encoder = nullptr;
    }

    if (flac_metadata)
    {
        FLAC__metadata_object_delete(flac_metadata);
        flac_metadata = nullptr;
    }
}

static int flac_format_required (int fmt)
{
    return FMT_S16_NE;
}

FileWriterImpl flac_plugin = {
    nullptr,  // init
    flac_open,
    flac_write,
    flac_close,
    flac_format_required,
};

#endif