Changeset b5d2e57 in mainline


Ignore:
Timestamp:
2012-08-30T20:07:38Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
03362fbd
Parents:
017455e
Message:

wavplay: Be more careful about overflows.

File:
1 edited

Legend:

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

    r017455e rb5d2e57  
    173173        printf("Playing: %dHz, %s, %d channel(s).\n", pb->f.sampling_rate,
    174174            pcm_sample_format_str(pb->f.sample_format), pb->f.channels);
    175         static suseconds_t work_time = 8000; /* 2 ms */
     175        static useconds_t work_time = 8000; /* 8 ms */
    176176        size_t bytes = fread(pb->buffer.position, sizeof(uint8_t),
    177177                    pb->buffer.size, pb->source);
     
    183183                size_t pos = 0;
    184184                audio_pcm_get_buffer_pos(pb->device, &pos);
    185                 useconds_t usecs = pcm_format_size_to_usec(bytes - pos, &pb->f);
     185                size_t to_play = bytes - pos;
     186                useconds_t usecs = (bytes > pos) ?
     187                    pcm_format_size_to_usec(to_play, &pb->f) : 0;
    186188
    187189                pb->buffer.position += bytes;
    188190
    189191                printf("%u usecs to play %zu bytes from pos %zu.\n",
    190                     usecs, bytes, pos);
    191                 async_usleep(usecs - work_time);
    192                 audio_pcm_get_buffer_pos(pb->device, &pos);
    193                 printf("Woke up at position %zu/%zu.\n", pos, pb->buffer.size);
     192                    usecs, to_play, pos);
     193                if (usecs > work_time) {
     194                        async_usleep(usecs - work_time);
     195                        audio_pcm_get_buffer_pos(pb->device, &pos);
     196//                      printf("Woke up at position %zu/%zu.\n",
     197//                          pos, pb->buffer.size);
     198                }
    194199
    195200                /* Remove any overflow */
     
    202207                        /* This was the last part,
    203208                         * zero 200 bytes or until the end of buffer. */
    204                         bzero(pb->buffer.position, min(200, remain));
     209                        bzero(pb->buffer.position, min(1024, remain));
    205210                        if ((pb->buffer.base + pos) > pb->buffer.position) {
    206211                                printf("Overflow: %zu vs. %zu!\n",
     
    223228                        break;
    224229                audio_pcm_get_buffer_pos(pb->device, &pos);
    225                 printf("Half buffer copied at pos %zu", pos);
     230                printf("Half buffer copied at pos %zu ", pos);
    226231                /* Wait until the rest of the buffer is ready */
    227232                udelay(pcm_format_size_to_usec(pb->buffer.size - pos, &pb->f));
    228233                /* copy the other part of the buffer */
    229                 audio_pcm_get_buffer_pos(pb->device, &pos);
    230                 printf(" the other half copied at pos %zu\n", pos);
    231                 bytes += fread(pb->buffer.position + bytes, sizeof(uint8_t),
    232                             pb->buffer.size / 2, pb->source);
     234                if (bytes ==  (pb->buffer.size / 2)) {
     235                        bytes += fread(pb->buffer.position + bytes,
     236                            sizeof(uint8_t), pb->buffer.size / 2, pb->source);
     237                        audio_pcm_get_buffer_pos(pb->device, &pos);
     238                        printf("the other half copied at pos %zu\n", pos);
     239                }
    233240        } while (1);
    234241        audio_pcm_stop_playback(pb->device);
Note: See TracChangeset for help on using the changeset viewer.