Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset b499127 in mainline


Ignore:
Timestamp:
2011-12-05T14:23:28Z (10 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master
Children:
c17c872c
Parents:
9b05c3e
Message:

dplay: Update buffer while playing.

File:
1 edited

Legend:

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

    r9b05c3e rb499127  
    4242#include <stdio.h>
    4343#include <sys/mman.h>
     44#include <sys/time.h>
    4445
    4546#include "beep.h"
    4647
    4748#define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/dsp"
     49
     50static void play(async_exch_t *device, unsigned buffer_id,
     51    void *buffer, size_t size, const void *data, size_t data_size,
     52    unsigned sampling_rate, unsigned sample_size, unsigned channels, bool sign)
     53{
     54        assert(device);
     55        const void* data_end = data + data_size;
     56        const size_t half_buf = size / 2;
     57
     58        /* Time to play half the buffer. */
     59        const unsigned interval = 1000000 /
     60            (sampling_rate /  (half_buf / (channels * (sample_size / 8))));
     61        printf("Time to play half buffer: %zu us.\n", interval);
     62        /* Initialize buffer. */
     63        memcpy(buffer, data, size);
     64        data += size;
     65
     66        struct timeval time;
     67        gettimeofday(&time, NULL);
     68        printf("Starting playback.\n");
     69        int ret = audio_pcm_buffer_start_playback(device, buffer_id,
     70            sampling_rate, sample_size, channels, sign);
     71        if (ret != EOK) {
     72                printf("Failed to start playback: %s.\n", str_error(ret));
     73                return;
     74        }
     75        void *buffer_place = buffer;
     76        while (data < data_end) {
     77                tv_add(&time, interval); /* Next update point */
     78                struct timeval current;
     79                gettimeofday(&current, NULL);
     80
     81                const suseconds_t delay = tv_sub(&time, &current);
     82                if (delay > 0)
     83                        usleep(delay);
     84                const size_t remain = data_end - data;
     85                if (remain < half_buf) {
     86                        memcpy(buffer_place, data, remain);
     87                        bzero(buffer_place + remain, half_buf - remain);
     88                        data += remain;
     89                } else {
     90                        memcpy(buffer_place, data, half_buf);
     91                        data += half_buf;
     92                }
     93                if (buffer_place == buffer) {
     94                        buffer_place = buffer + half_buf;
     95                } else {
     96                        buffer_place = buffer;
     97                }
     98        }
     99
     100        printf("Stopping playback.\n");
     101        ret = audio_pcm_buffer_stop_playback(device, buffer_id);
     102        if (ret != EOK) {
     103                printf("Failed to stop playback: %s.\n", str_error(ret));
     104        }
     105}
    48106
    49107int main(int argc, char *argv[])
     
    97155        }
    98156        printf("Buffer (%u): %p %zu.\n", id, buffer, size);
    99         memcpy(buffer, beep, size);
    100157
    101         printf("Starting playback.\n");
    102         ret = audio_pcm_buffer_start_playback(exch, id, 44100, 16, 1, true);
    103         if (ret != EOK) {
    104                 printf("Failed to start playback: %s.\n", str_error(ret));
    105                 munmap(buffer, size);
    106                 audio_pcm_buffer_release_buffer(exch, id);
    107                 async_exchange_end(exch);
    108                 async_hangup(session);
    109                 return 1;
    110         } else {
    111                 sleep(2);
    112         }
    113 
    114         printf("Stopping playback.\n");
    115         ret = audio_pcm_buffer_stop_playback(exch, id);
    116         if (ret != EOK) {
    117                 printf("Failed to start playback: %s.\n", str_error(ret));
    118         }
    119 
     158        play(exch, id, buffer, size, beep, beep_size,
     159            rate, sample_size, channels, sign);
    120160
    121161        munmap(buffer, size);
     
    125165        return 0;
    126166}
    127 
    128 /** @}
     167/**
     168 * @}
    129169 */
Note: See TracChangeset for help on using the changeset viewer.