Exemplo n.º 1
0
static void encodeKey(Vector<char>& data, const IDBKeyData& key)
{
    SIDBKeyType type = serializedTypeForKeyType(key.type());
    data.append(static_cast<char>(type));

    switch (type) {
    case SIDBKeyType::Number:
        writeDouble(data, key.number());
        break;
    case SIDBKeyType::Date:
        writeDouble(data, key.date());
        break;
    case SIDBKeyType::String: {
        auto string = key.string();
        uint32_t length = string.length();
        writeLittleEndian(data, length);

        for (size_t i = 0; i < length; ++i)
            writeLittleEndian(data, string[i]);

        break;
    }
    case SIDBKeyType::Binary: {
        auto& buffer = key.binary();
        uint64_t size = buffer.size();
        writeLittleEndian(data, size);

        auto* bufferData = buffer.data();
        ASSERT(bufferData || !size);
        if (bufferData)
            data.append(bufferData->data(), bufferData->size());

        break;
    }
    case SIDBKeyType::Array: {
        auto& array = key.array();
        uint64_t size = array.size();
        writeLittleEndian(data, size);
        for (auto& key : array)
            encodeKey(data, key);

        break;
    }
    case SIDBKeyType::Min:
    case SIDBKeyType::Max:
        break;
    }
}
Exemplo n.º 2
0
    void EventHeader::toStream( std::ostream &stream )
    {
        stream.write( reinterpret_cast<const char*>(&m_mode), 1 );

        // convert number of events to 24bit little endian representation
        uint32 numev = getNumEvents( );
        uint8 tmp[3];
        tmp[0] = numev % 256;
        tmp[1] = (numev / 256) % 256;
        tmp[2] = (numev / 65536) % 256;
        stream.write( reinterpret_cast<const char*>(tmp), 3 );

        writeLittleEndian( stream, getSamplingRate( ) );

        if( getMode() == 1 )
        {
            Mode1Event e;
            // write event positions
            for( size_t i=0; i<getNumEvents(); i++)
            {
                getEvent( i, e );
                writeLittleEndian( stream, e.position );
            }
            // write event types
            for( size_t i=0; i<getNumEvents(); i++)
            {
                getEvent( i, e );
                writeLittleEndian( stream, e.type );
            }
        }
        else if( getMode() == 3 )
        {
            Mode3Event e;
            // write event positions
            for( size_t i=0; i<getNumEvents(); i++)
            {
                getEvent( i, e );
                writeLittleEndian( stream, e.position );
            }
            // write event types
            for( size_t i=0; i<getNumEvents(); i++)
            {
                getEvent( i, e );
                writeLittleEndian( stream, e.type );
            }
            // write event channels
            for( size_t i=0; i<getNumEvents(); i++)
            {
                getEvent( i, e );
                writeLittleEndian( stream, e.channel );
            }
            // write event durations
            for( size_t i=0; i<getNumEvents(); i++)
            {
                getEvent( i, e );
                writeLittleEndian( stream, e.duration );
            }
        }
    }
Exemplo n.º 3
0
void writeWav(char * filename, unsigned long numSamples, short int * data, int sRate) {
    FILE* wavFile;
    unsigned int sampleRate;
    unsigned int numChannels;
    unsigned int bytesPerSample;
    unsigned int byteRate;
    unsigned long i;    /* counter for samples */

    numChannels = 1;   /* monoaural */
    bytesPerSample = 2;

    if (sRate<=0) sampleRate = 44100;
    else sampleRate = (unsigned int) sRate;

    byteRate = sampleRate * numChannels * bytesPerSample;

    wavFile = fopen(filename, "w");
//    assert(wavFile);   /* make sure it opened */

    /* write RIFF header */
    fwrite("RIFF", 1, 4, wavFile);
    writeLittleEndian(36 + bytesPerSample * numSamples * numChannels, 4, wavFile);
    fwrite("WAVE", 1, 4, wavFile);

    /* write fmt  subchunk */
    fwrite("fmt ", 1, 4, wavFile);
    writeLittleEndian(16, 4, wavFile);   /* SubChunk1Size is 16 */
    writeLittleEndian(1, 2, wavFile);    /* PCM is format 1 */
    writeLittleEndian(numChannels, 2, wavFile);
    writeLittleEndian(sampleRate, 4, wavFile);
    writeLittleEndian(byteRate, 4, wavFile);
    writeLittleEndian(numChannels * bytesPerSample, 2, wavFile);  /* block align */
    writeLittleEndian(8*bytesPerSample, 2, wavFile);  /* bits/sample */

    /* write data subchunk */
    fwrite("data", 1, 4, wavFile);
    writeLittleEndian(bytesPerSample * numSamples*numChannels, 4, wavFile);
    for (i=0; i< numSamples; i++) {\
    	writeLittleEndian((unsigned int)(data[i]),bytesPerSample, wavFile);
    }

    fclose(wavFile);
}
Exemplo n.º 4
0
static void writeDouble(Vector<char>& data, double d)
{
    writeLittleEndian(data, *reinterpret_cast<uint64_t*>(&d));
}