Changeset a8e87da in mainline for uspace/app/wavplay/main.c


Ignore:
Timestamp:
2013-04-10T18:49:43Z (11 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b1dfe13
Parents:
76e863c
Message:

wavplay: comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/wavplay/main.c

    r76e863c ra8e87da  
    5151#define STREAM_BUFFER_SIZE   (64 * 1024)
    5252
     53/**
     54 * Play audio file using a new stream on provided context.
     55 * @param ctx Provided context.
     56 * @param filename File to play.
     57 * @return Error code.
     58 */
    5359static int hplay_ctx(hound_context_t *ctx, const char *filename)
    5460{
     
    5965                return EINVAL;
    6066        }
     67
     68        /* Read and parse WAV header */
    6169        wave_header_t header;
    6270        size_t read = fread(&header, sizeof(header), 1, source);
     
    7684        }
    7785
     86        /* Allocate buffer and create new context */
     87        char * buffer = malloc(READ_SIZE);
     88        if (!buffer) {
     89                fclose(source);
     90                return ENOMEM;
     91        }
    7892        hound_stream_t *stream = hound_stream_create(ctx,
    7993            HOUND_STREAM_DRAIN_ON_EXIT, format, STREAM_BUFFER_SIZE);
    8094
    81         char * buffer = malloc(READ_SIZE);
    82         if (!buffer) {
    83                 fclose(source);
    84                 return ENOMEM;
    85         }
     95        /* Read and play */
    8696        while ((read = fread(buffer, sizeof(char), READ_SIZE, source)) > 0) {
    8797                ret = hound_stream_write(stream, buffer, read);
     
    92102                }
    93103        }
     104
     105        /* Cleanup */
    94106        free(buffer);
    95107        fclose(source);
     
    97109}
    98110
     111/**
     112 * Play audio file via hound server.
     113 * @param filename File to play.
     114 * @return Error code
     115 */
    99116static int hplay(const char *filename)
    100117{
     
    105122                return EINVAL;
    106123        }
     124
     125        /* Read and parse WAV header */
    107126        wave_header_t header;
    108127        size_t read = fread(&header, sizeof(header), 1, source);
     
    121140                return EINVAL;
    122141        }
     142
     143        /* Connect new playback context */
    123144        hound_context_t *hound = hound_context_create_playback(filename,
    124145            format, STREAM_BUFFER_SIZE);
     
    137158                return ret;
    138159        }
     160
     161        /* Read and play */
    139162        static char buffer[READ_SIZE];
    140163        while ((read = fread(buffer, sizeof(char), READ_SIZE, source)) > 0) {
     
    146169                }
    147170        }
     171
     172        /* Cleanup */
    148173        hound_context_destroy(hound);
    149174        fclose(source);
     
    151176}
    152177
     178/**
     179 * Helper structure for playback in separate fibrils
     180 */
    153181typedef struct {
    154182        hound_context_t *ctx;
     
    157185} fib_play_t;
    158186
     187/**
     188 * Fibril playback wrapper.
     189 * @param arg Argument, pointer to playback helper structure.
     190 * @return Error code.
     191 */
    159192static int play_wrapper(void *arg)
    160193{
     
    167200}
    168201
     202/**
     203 * Array of supported commandline options
     204 */
    169205static const struct option opts[] = {
    170206        {"device", required_argument, 0, 'd'},
     
    175211};
    176212
     213/**
     214 * Print usage help.
     215 * @param name Name of the program.
     216 */
    177217static void print_help(const char* name)
    178218{
     
    195235        optind = 0;
    196236        int ret = 0;
     237
     238        /* Parse command line options */
    197239        while (ret != -1) {
    198240                ret = getopt_long(argc, argv, "d:prh", opts, &idx);
     
    227269        }
    228270
     271        /* Init parallel playback variables */
    229272        hound_context_t *hound_ctx = NULL;
    230273        atomic_t playcount;
    231274        atomic_set(&playcount, 0);
     275
     276        /* Init parallel playback context if necessary */
    232277        if (parallel) {
    233278                hound_ctx = hound_context_create_playback("wavplay",
     
    247292        }
    248293
     294        /* play or record all files */
    249295        for (int i = optind; i < argc; ++i) {
    250296                const char *file = argv[i];
     
    261307                        }
    262308                }
     309
    263310                if (direct) {
    264311                        dplay(device, file);
    265312                } else {
    266313                        if (parallel) {
     314                                /* Start new fibril for parallel playback */
    267315                                fib_play_t *data = malloc(sizeof(fib_play_t));
    268316                                if (!data) {
     
    283331        }
    284332
     333        /* Wait for all fibrils to finish */
    285334        while (atomic_get(&playcount) > 0)
    286335                async_usleep(1000000);
    287336
     337        /* Destroy parallel playback context, if initialized */
    288338        if (hound_ctx)
    289339                hound_context_destroy(hound_ctx);
Note: See TracChangeset for help on using the changeset viewer.