summaryrefslogtreecommitdiff
path: root/source/io/SampleSourcePcm.c
blob: f0be1e88effe793a9e0f53853a05932c3175a4fb (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// SampleSourcePcm.c - MrsWatson
// Created by Nik Reiman on 1/2/12.
// Copyright (c) 2012 Teragon Audio. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

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

#include "audio/AudioSettings.h"
#include "base/PlatformInfo.h"
#include "io/SampleSourcePcm.h"
#include "logging/EventLogger.h"

static boolByte openSampleSourcePcm(void *sampleSourcePtr, const SampleSourceOpenAs openAs)
{
    SampleSource sampleSource = (SampleSource)sampleSourcePtr;
    SampleSourcePcmData extraData = (SampleSourcePcmData)(sampleSource->extraData);

    extraData->dataBufferNumItems = 0;

    if (openAs == SAMPLE_SOURCE_OPEN_READ) {
        if (charStringIsEqualToCString(sampleSource->sourceName, "-", false)) {
            extraData->fileHandle = stdin;
            charStringCopyCString(sampleSource->sourceName, "stdin");
            extraData->isStream = true;
        } else {
            extraData->fileHandle = fopen(sampleSource->sourceName->data, "rb");
        }
    } else if (openAs == SAMPLE_SOURCE_OPEN_WRITE) {
        if (charStringIsEqualToCString(sampleSource->sourceName, "-", false)) {
            extraData->fileHandle = stdout;
            charStringCopyCString(sampleSource->sourceName, "stdout");
            extraData->isStream = true;
        } else {
            extraData->fileHandle = fopen(sampleSource->sourceName->data, "wb");
        }
    } else {
        logInternalError("Invalid type for openAs in PCM file");
        return false;
    }

    if (extraData->fileHandle == NULL) {
        logError("PCM File '%s' could not be opened for %s",
                 sampleSource->sourceName->data, openAs == SAMPLE_SOURCE_OPEN_READ ? "reading" : "writing");
        return false;
    }

    sampleSource->openedAs = openAs;
    return true;
}

size_t sampleSourcePcmRead(SampleSourcePcmData self, SampleBuffer sampleBuffer)
{
    size_t pcmSamplesRead = 0;

    if (self == NULL || self->fileHandle == NULL) {
        logCritical("Corrupt PCM data structure");
        return 0;
    }

    if (self->dataBufferNumItems < (size_t)(sampleBuffer->numChannels * sampleBuffer->blocksize)) {
        self->dataBufferNumItems = (size_t)(sampleBuffer->numChannels * sampleBuffer->blocksize);
        self->interlacedPcmDataBuffer = (short *)realloc(self->interlacedPcmDataBuffer, sizeof(short) * self->dataBufferNumItems);
    }

    // Clear the PCM data buffer, or else the last block will have dirty samples in the end
    memset(self->interlacedPcmDataBuffer, 0, sizeof(short) * self->dataBufferNumItems);

    pcmSamplesRead = fread(self->interlacedPcmDataBuffer, sizeof(short), self->dataBufferNumItems, self->fileHandle);

    if (pcmSamplesRead < self->dataBufferNumItems) {
        logDebug("End of PCM file reached");
        // Set the blocksize of the sample buffer to be the number of frames read
        sampleBuffer->blocksize = pcmSamplesRead / sampleBuffer->numChannels;
    }

    logDebug("Read %d samples from PCM file", pcmSamplesRead);

    sampleBufferCopyPcmSamples(sampleBuffer, self->interlacedPcmDataBuffer);
    return pcmSamplesRead;
}

static boolByte readBlockFromPcmFile(void *sampleSourcePtr, SampleBuffer sampleBuffer)
{
    SampleSource sampleSource = (SampleSource)sampleSourcePtr;
    SampleSourcePcmData extraData = (SampleSourcePcmData)(sampleSource->extraData);
    SampleCount originalBlocksize = sampleBuffer->blocksize;
    size_t samplesRead = sampleSourcePcmRead(extraData, sampleBuffer);
    sampleSource->numSamplesProcessed += samplesRead;
    return (boolByte)(originalBlocksize == sampleBuffer->blocksize);
}

size_t sampleSourcePcmWrite(SampleSourcePcmData self, const SampleBuffer sampleBuffer)
{
    size_t pcmSamplesWritten = 0;
    size_t numSamplesToWrite = (size_t)(sampleBuffer->numChannels * sampleBuffer->blocksize);

    if (self == NULL || self->fileHandle == NULL) {
        logCritical("Corrupt PCM data structure");
        return false;
    }

    if (self->dataBufferNumItems < (size_t)(sampleBuffer->numChannels * sampleBuffer->blocksize)) {
        self->dataBufferNumItems = (size_t)(sampleBuffer->numChannels * sampleBuffer->blocksize);
        self->interlacedPcmDataBuffer = (short *)realloc(self->interlacedPcmDataBuffer, sizeof(short) * self->dataBufferNumItems);
    }

    // Clear the PCM data buffer just to be safe
    memset(self->interlacedPcmDataBuffer, 0, sizeof(short) * self->dataBufferNumItems);

    boolByte isLittleEndian = (boolByte)(self->isLittleEndian != platformInfoIsLittleEndian());
    sampleBufferGetPcmSamples(sampleBuffer, self->interlacedPcmDataBuffer, isLittleEndian);
    pcmSamplesWritten = fwrite(self->interlacedPcmDataBuffer, sizeof(short), numSamplesToWrite, self->fileHandle);

    if (pcmSamplesWritten < numSamplesToWrite) {
        logWarn("Short write to PCM file");
        return pcmSamplesWritten;
    }

    logDebug("Wrote %d samples to PCM file", pcmSamplesWritten);
    return pcmSamplesWritten;
}

static boolByte writeBlockToPcmFile(void *sampleSourcePtr, const SampleBuffer sampleBuffer)
{
    SampleSource sampleSource = (SampleSource)sampleSourcePtr;
    SampleSourcePcmData extraData = (SampleSourcePcmData)(sampleSource->extraData);
    unsigned int samplesWritten = (int)sampleSourcePcmWrite(extraData, sampleBuffer);
    sampleSource->numSamplesProcessed += samplesWritten;
    return (boolByte)(samplesWritten == sampleBuffer->blocksize);
}

static void _closeSampleSourcePcm(void *sampleSourcePtr)
{
    SampleSource sampleSource = (SampleSource)sampleSourcePtr;
    SampleSourcePcmData extraData = (SampleSourcePcmData)sampleSource->extraData;

    if (extraData->fileHandle != NULL) {
        fclose(extraData->fileHandle);
    }
}

void sampleSourcePcmSetSampleRate(void *sampleSourcePtr, SampleRate sampleRate)
{
    SampleSource sampleSource = (SampleSource)sampleSourcePtr;
    SampleSourcePcmData extraData = (SampleSourcePcmData)sampleSource->extraData;
    extraData->sampleRate = (unsigned int)sampleRate;
}

void sampleSourcePcmSetNumChannels(void *sampleSourcePtr, int numChannels)
{
    SampleSource sampleSource = (SampleSource)sampleSourcePtr;
    SampleSourcePcmData extraData = (SampleSourcePcmData)sampleSource->extraData;
    extraData->numChannels = (unsigned short)numChannels;
}

void freeSampleSourceDataPcm(void *sampleSourceDataPtr)
{
    SampleSourcePcmData extraData = (SampleSourcePcmData)sampleSourceDataPtr;

    if (extraData->interlacedPcmDataBuffer != NULL) {
        free(extraData->interlacedPcmDataBuffer);
        extraData->interlacedPcmDataBuffer = NULL;
    }

    free(extraData);
}

SampleSource _newSampleSourcePcm(const CharString sampleSourceName)
{
    SampleSource sampleSource = (SampleSource)malloc(sizeof(SampleSourceMembers));
    SampleSourcePcmData extraData = (SampleSourcePcmData)malloc(sizeof(SampleSourcePcmDataMembers));

    sampleSource->sampleSourceType = SAMPLE_SOURCE_TYPE_PCM;
    sampleSource->openedAs = SAMPLE_SOURCE_OPEN_NOT_OPENED;
    sampleSource->sourceName = newCharString();
    charStringCopy(sampleSource->sourceName, sampleSourceName);
    sampleSource->numSamplesProcessed = 0;

    sampleSource->openSampleSource = openSampleSourcePcm;
    sampleSource->readSampleBlock = readBlockFromPcmFile;
    sampleSource->writeSampleBlock = writeBlockToPcmFile;
    sampleSource->closeSampleSource = _closeSampleSourcePcm;
    sampleSource->freeSampleSourceData = freeSampleSourceDataPcm;

    extraData->isStream = false;
    extraData->isLittleEndian = true;
    extraData->fileHandle = NULL;
    extraData->dataBufferNumItems = 0;
    extraData->interlacedPcmDataBuffer = NULL;

    extraData->numChannels = (unsigned short)getNumChannels();
    extraData->sampleRate = (unsigned int)getSampleRate();
    extraData->bitsPerSample = 16;
    sampleSource->extraData = extraData;

    return sampleSource;
}