Changeset 4bec78f in mainline


Ignore:
Timestamp:
2012-07-05T20:48:10Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
124f9bd
Parents:
725d038
Message:

dplay: Update to use new interface.

Ugly and broken, but compiles fine.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/dplay/dplay.c

    r725d038 r4bec78f  
    5050
    5151#define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/dsp"
    52 
    53 static void play(async_exch_t *device, unsigned buffer_id,
    54     void *buffer, size_t size, FILE *source,
     52#define SUBBUFFERS 4
     53
     54typedef struct {
     55        struct {
     56                void *base;
     57                size_t size;
     58                unsigned id;
     59                void* position;
     60        } buffer;
     61        FILE* source;
     62} playback_t;
     63
     64
     65static void device_event_callback(ipc_callid_t id, ipc_call_t *call, void* arg)
     66{
     67        if (IPC_GET_IMETHOD(*call) != IPC_FIRST_USER_METHOD) {
     68                printf("Unknown event.\n");
     69                return;
     70        }
     71        playback_t *pb = arg;
     72        printf("Got device event!!!\n");
     73        const size_t buffer_part = pb->buffer.size / SUBBUFFERS;
     74        const size_t bytes = fread(pb->buffer.position, sizeof(uint8_t),
     75           buffer_part, pb->source);
     76        if (bytes == 0)
     77                exit(0); // ugly but temporary
     78        pb->buffer.position += bytes;
     79        if (bytes != buffer_part)
     80                bzero(pb->buffer.position, buffer_part - bytes);
     81        pb->buffer.position += buffer_part - bytes;
     82        if (pb->buffer.position >= (pb->buffer.base + pb->buffer.size))
     83                pb->buffer.position = pb->buffer.base;
     84}
     85
     86
     87static void play(async_exch_t *device, playback_t *pb,
    5588    unsigned sampling_rate, unsigned sample_size, unsigned channels, bool sign)
    5689{
    5790        assert(device);
    58         const size_t update_size = size / 8;
     91        assert(pb);
     92        pb->buffer.position = pb->buffer.base;
     93        printf("Playing: %dHz, %d-bit samples, %d channel(s), %sSIGNED.\n",
     94            sampling_rate, sample_size, channels, sign ? "": "UN");
     95        const size_t bytes = fread(pb->buffer.base, sizeof(uint8_t),
     96            pb->buffer.size, pb->source);
     97        if (bytes != pb->buffer.size)
     98                return;
     99        printf("Buffer data ready.\n");
     100        sleep(10);
     101#if 0
     102        const size_t update_size = size / SUBBUFFERS;
    59103
    60104        /* Time to play half the buffer. */
     
    70114        gettimeofday(&time, NULL);
    71115        printf("Starting playback.\n");
    72         int ret = audio_pcm_buffer_start_playback(device, buffer_id,
     116        int ret = audio_pcm_buffer_start_playback(device, buffer_id, SUBBUFFERS,
    73117            sampling_rate, sample_size, channels, sign);
    74118        if (ret != EOK) {
     
    85129                const suseconds_t delay = min(tv_sub(&time, &current), interval);
    86130                if (delay > 0)
    87                         usleep(delay);
     131                        udelay(delay);
    88132
    89133                const size_t bytes =
     
    106150                printf("Failed to stop playback: %s.\n", str_error(ret));
    107151        }
     152#endif
    108153}
    109154
     
    134179
    135180        async_sess_t *session = devman_device_connect(
    136             EXCHANGE_ATOMIC, pcm_handle, IPC_FLAG_BLOCKING);
     181            EXCHANGE_SERIALIZE, pcm_handle, IPC_FLAG_BLOCKING);
    137182        if (!session) {
    138183                printf("Failed to connect to device.\n");
     
    146191                return 1;
    147192        }
    148         const char* info;
     193        const char* info = NULL;
    149194        ret = audio_pcm_buffer_get_info_str(exch, &info);
    150195        if (ret != EOK) {
     
    157202        free(info);
    158203
    159         void *buffer = NULL;
    160         size_t size = 0;
    161         unsigned id = 0;
    162         ret = audio_pcm_buffer_get_buffer(exch, &buffer, &size, &id);
     204        playback_t pb = {{0}, NULL};
     205        pb.buffer.size = 4096;
     206        printf("Requesting buffer: %p, %zu, %u.\n", pb.buffer.base, pb.buffer.size, pb.buffer.id);
     207        ret = audio_pcm_buffer_get_buffer(exch, &pb.buffer.base,
     208            &pb.buffer.size, &pb.buffer.id, device_event_callback, &pb);
    163209        if (ret != EOK) {
    164210                printf("Failed to get PCM buffer: %s.\n", str_error(ret));
     
    167213                return 1;
    168214        }
    169         printf("Buffer (%u): %p %zu.\n", id, buffer, size);
    170 
    171         FILE *source = fopen(file, "rb");
    172         if (source == NULL) {
     215        printf("Buffer (%u): %p %zu.\n", pb.buffer.id, pb.buffer.base,
     216            pb.buffer.size);
     217
     218        pb.source = fopen(file, "rb");
     219        if (pb.source == NULL) {
    173220                printf("Failed to open %s.\n", file);
    174                 munmap(buffer, size);
    175                 audio_pcm_buffer_release_buffer(exch, id);
     221                munmap(pb.buffer.base, pb.buffer.size);
     222                audio_pcm_buffer_release_buffer(exch, pb.buffer.id);
    176223                async_exchange_end(exch);
    177224                async_hangup(session);
     
    179226        }
    180227        wave_header_t header;
    181         fread(&header, sizeof(header), 1, source);
     228        fread(&header, sizeof(header), 1, pb.source);
    182229        unsigned rate, sample_size, channels;
    183230        bool sign;
     
    187234        if (ret != EOK) {
    188235                printf("Error parsing wav header: %s.\n", error);
    189                 fclose(source);
    190                 munmap(buffer, size);
    191                 audio_pcm_buffer_release_buffer(exch, id);
    192                 async_exchange_end(exch);
    193                 async_hangup(session);
    194                 return 1;
    195         }
    196 
    197         play(exch, id, buffer, size, source, rate, sample_size, channels, sign);
    198 
    199         munmap(buffer, size);
    200         audio_pcm_buffer_release_buffer(exch, id);
     236                fclose(pb.source);
     237                munmap(pb.buffer.base, pb.buffer.size);
     238                audio_pcm_buffer_release_buffer(exch, pb.buffer.id);
     239                async_exchange_end(exch);
     240                async_hangup(session);
     241                return 1;
     242        }
     243
     244        play(exch, &pb, rate, sample_size, channels, sign);
     245
     246        munmap(pb.buffer.base, pb.buffer.size);
     247        audio_pcm_buffer_release_buffer(exch, pb.buffer.id);
    201248        async_exchange_end(exch);
    202249        async_hangup(session);
Note: See TracChangeset for help on using the changeset viewer.