
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>

#include "../mecha/mecha.h"

#include "music_packet.h"

struct s_MusicPacket
{
    dynTstring *data;
    struct timeval timeLength;
    int timestamp;
};

MusicPacket *
music_packet_new (void)
{
    MusicPacket *instance = (MusicPacket *) malloc (sizeof (MusicPacket));

    instance->data = dynFinit();

    instance->timeLength.tv_sec = 0;
    instance->timeLength.tv_usec = 0;

    instance->timestamp = time (0);

    return (instance);
}

void
music_packet_destroy (void *ptr)
{
    MusicPacket *instance = (MusicPacket *) ptr;

    dynFfree (instance->data);

    free (instance);
}

MusicPacket *
music_packet_duplicate (MusicPacket *instance)
{
    MusicPacket *newInstance = (MusicPacket *) malloc (sizeof (MusicPacket));

    newInstance->data = dynFinit();
    dynFappend_string (newInstance->data, instance->data);

    newInstance->timeLength = instance->timeLength;

    newInstance->timestamp = instance->timestamp;

    return (newInstance);
}

dynTstring *
music_packet_data_get (MusicPacket *instance)
{
    return (instance->data);
}

int
music_packet_timestamp_get (MusicPacket *instance)
{
    return (instance->timestamp);
}

struct timeval
music_packet_time_length_get (MusicPacket *instance)
{
    return (instance->timeLength);
}

int
music_packet_time_length_usecs_get (MusicPacket *instance)
{
    return ((instance->timeLength.tv_sec * 1000000L) + instance->timeLength.tv_usec);
}

void
music_packet_time_length_add_usec (MusicPacket *instance, long usec)
{
    instance->timeLength.tv_sec += usec / 1000000L;
    instance->timeLength.tv_usec += usec % 1000000L;
}
