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

Changeset 346643c in mainline


Ignore:
Timestamp:
2012-07-11T12:05:30Z (10 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial
Children:
039337e8
Parents:
94694a4
Message:

audio: Use enum for sample format.

Location:
uspace
Files:
1 added
11 edited

Legend:

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

    r94694a4 r346643c  
    112112
    113113
    114 static void play(playback_t *pb, unsigned sampling_rate, unsigned sample_size,
    115     unsigned channels, bool sign)
     114static void play(playback_t *pb, unsigned channels,  unsigned sampling_rate,
     115    pcm_sample_format_t format)
    116116{
    117117        assert(pb);
    118118        assert(pb->device);
    119119        pb->buffer.position = pb->buffer.base;
    120         printf("Playing: %dHz, %d-bit %ssigned samples, %d channel(s).\n",
    121             sampling_rate, sample_size, sign ? "": "un", channels);
     120        printf("Playing: %dHz, %s, %d channel(s).\n",
     121            sampling_rate, pcm_sample_format_str(format), channels);
    122122        const size_t bytes = fread(pb->buffer.base, sizeof(uint8_t),
    123123            pb->buffer.size, pb->source);
     
    127127        fibril_mutex_lock(&pb->mutex);
    128128        int ret = audio_pcm_start_playback(pb->device, pb->buffer.id,
    129             SUBBUFFERS, sampling_rate, sample_size, channels, sign);
     129            SUBBUFFERS, channels, sampling_rate, format);
    130130        if (ret != EOK) {
    131131                fibril_mutex_unlock(&pb->mutex);
     
    212212        wave_header_t header;
    213213        fread(&header, sizeof(header), 1, pb.source);
    214         unsigned rate, sample_size, channels;
    215         bool sign;
     214        unsigned rate, channels;
     215        pcm_sample_format_t format;
    216216        const char *error;
    217         ret = wav_parse_header(&header, NULL, NULL, &rate, &sample_size,
    218             &channels, &sign, &error);
     217        ret = wav_parse_header(&header, NULL, NULL, &channels, &rate, &format,
     218            &error);
    219219        if (ret != EOK) {
    220220                printf("Error parsing wav header: %s.\n", error);
     
    223223        }
    224224
    225         play(&pb, rate, sample_size, channels, sign);
     225        play(&pb, channels, rate, format);
    226226        fclose(pb.source);
    227227
  • uspace/app/dplay/wave.c

    r94694a4 r346643c  
    4040
    4141int wav_parse_header(void *file, const void **data, size_t *data_size,
    42     unsigned *sampling_rate, unsigned *sample_size, unsigned *channels,
    43     bool *sign, const char **error)
     42    unsigned *channels, unsigned *sampling_rate, pcm_sample_format_t *format,
     43    const char **error)
    4444{
    4545        if (!file) {
     
    8686        }
    8787
     88
    8889        if (data)
    8990                *data = header->data;
     
    9394        if (sampling_rate)
    9495                *sampling_rate = uint32_t_le2host(header->sampling_rate);
    95         if (sample_size)
    96                 *sample_size = uint32_t_le2host(header->sample_size);
    9796        if (channels)
    9897                *channels = uint16_t_le2host(header->channels);
    99         if (sign)
    100                 *sign = uint32_t_le2host(header->sample_size) == 16
    101                     ? true : false;
     98        if (format) {
     99                const unsigned size = uint32_t_le2host(header->sample_size);
     100                switch (size) {
     101                case 8: *format = PCM_SAMPLE_UINT8; break;
     102                case 16: *format = PCM_SAMPLE_SINT16_LE; break;
     103                case 24: *format = PCM_SAMPLE_SINT24_LE; break;
     104                case 32: *format = PCM_SAMPLE_SINT32_LE; break;
     105                default:
     106                        *error = "Unknown format";
     107                        return ENOTSUP;
     108                }
     109        }
    102110        if (error)
    103111                *error = "no error";
  • uspace/app/dplay/wave.h

    r94694a4 r346643c  
    3737#include <stdint.h>
    3838#include <bool.h>
     39#include <pcm_sample_format.h>
    3940
    4041/** Wave file header format.
     
    9091
    9192int wav_parse_header(void *, const void**, size_t *, unsigned *, unsigned *,
    92     unsigned *, bool *, const char **error_str);
     93    pcm_sample_format_t *, const char **);
    9394
    9495#endif
  • uspace/app/drec/drec.c

    r94694a4 r346643c  
    4747#include <stdio.h>
    4848#include <macros.h>
     49#include <pcm_sample_format.h>
    4950
    5051#include "wave.h"
     
    5354#define SUBBUFFERS 2
    5455
    55 const unsigned sampling_rate = 44100, sample_size = 16, channels = 2;
    56 bool sign = true;
     56const unsigned sampling_rate = 44100, channels = 2, sample_size = 16;
     57const pcm_sample_format_t format = PCM_SAMPLE_SINT16_LE;
    5758
    5859typedef struct {
     
    104105
    105106
    106 static void record(record_t *rec, unsigned sampling_rate, unsigned sample_size,
    107     unsigned channels, bool sign)
     107static void record(record_t *rec, unsigned channels, unsigned sampling_rate,
     108    pcm_sample_format_t format)
    108109{
    109110        assert(rec);
    110111        assert(rec->device);
    111112        rec->buffer.position = rec->buffer.base;
    112         printf("Recording: %dHz, %d-bit %ssigned samples, %d channel(s).\n",
    113             sampling_rate, sample_size, sign ? "": "un", channels);
     113        printf("Recording: %dHz, %s, %d channel(s).\n",
     114            sampling_rate, pcm_sample_format_str(format), channels);
    114115        int ret = audio_pcm_start_record(rec->device, rec->buffer.id,
    115             SUBBUFFERS, sampling_rate, sample_size, channels, sign);
     116            SUBBUFFERS, channels, sampling_rate, format);
    116117        if (ret != EOK) {
    117118                printf("Failed to start recording: %s.\n", str_error(ret));
     
    201202                .sampling_rate = sampling_rate,
    202203                .sample_size = sample_size,
    203                 .byte_rate = (sampling_rate / 8) * channels,
    204                 .block_align = (sampling_rate / 8) * channels,
     204                .byte_rate = sampling_rate * (sample_size / 8) * channels,
     205                .block_align = (sample_size / 8) * channels,
    205206                .subchunk2_id = SUBCHUNK2_ID,
    206207        };
    207208        fwrite(&header, sizeof(header), 1, rec.file);
    208         record(&rec, sampling_rate, sample_size, channels, sign);
     209        record(&rec, sampling_rate, channels, format);
    209210        fclose(rec.file);
    210211
  • uspace/app/drec/wave.c

    r94694a4 r346643c  
    4040
    4141int wav_parse_header(void *file, const void **data, size_t *data_size,
    42     unsigned *sampling_rate, unsigned *sample_size, unsigned *channels,
    43     bool *sign, const char **error)
     42    unsigned *channels, unsigned *sampling_rate, pcm_sample_format_t *format,
     43    const char **error)
    4444{
    4545        if (!file) {
     
    8686        }
    8787
     88
    8889        if (data)
    8990                *data = header->data;
     
    9394        if (sampling_rate)
    9495                *sampling_rate = uint32_t_le2host(header->sampling_rate);
    95         if (sample_size)
    96                 *sample_size = uint32_t_le2host(header->sample_size);
    9796        if (channels)
    9897                *channels = uint16_t_le2host(header->channels);
    99         if (sign)
    100                 *sign = uint32_t_le2host(header->sample_size) == 16
    101                     ? true : false;
     98        if (format) {
     99                const unsigned size = uint32_t_le2host(header->sample_size);
     100                switch (size) {
     101                case 8: *format = PCM_SAMPLE_UINT8; break;
     102                case 16: *format = PCM_SAMPLE_SINT16_LE; break;
     103                case 24: *format = PCM_SAMPLE_SINT24_LE; break;
     104                case 32: *format = PCM_SAMPLE_SINT32_LE; break;
     105                default:
     106                        *error = "Unknown format";
     107                        return ENOTSUP;
     108                }
     109        }
    102110        if (error)
    103111                *error = "no error";
  • uspace/app/drec/wave.h

    r94694a4 r346643c  
    3737#include <stdint.h>
    3838#include <bool.h>
     39#include <pcm_sample_format.h>
    3940
    4041/** Wave file header format.
     
    9091
    9192int wav_parse_header(void *, const void**, size_t *, unsigned *, unsigned *,
    92     unsigned *, bool *, const char **error_str);
     93    pcm_sample_format_t *, const char **);
    9394
    9495#endif
  • uspace/drv/audio/sb16/dsp.c

    r94694a4 r346643c  
    147147}
    148148
    149 static inline size_t sample_count(unsigned sample_size, size_t byte_count)
    150 {
    151         if (sample_size == 16) {
    152                 return byte_count / 2;
    153         }
    154         return byte_count;
     149static inline size_t sample_count(pcm_sample_format_t format, size_t byte_count)
     150{
     151        return byte_count / pcm_sample_format_size(format);
    155152}
    156153
     
    261258
    262259int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned id, unsigned parts,
    263     unsigned sampling_rate, unsigned sample_size, unsigned channels, bool sign)
     260    unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
    264261{
    265262        assert(dsp);
     
    276273        /* Check supported parameters */
    277274        ddf_log_debug("Requested playback on buffer \"%u\" (%u parts): %uHz, "
    278             "%ssinged %u bit, %u channel(s).", id, parts, sampling_rate,
    279             sign ? "" : "un", sample_size, channels);
    280         if (id != BUFFER_ID)
    281                 return ENOENT;
    282         if (sample_size != 16) // FIXME We only support 16 bit playback
    283                 return ENOTSUP;
     275            "%s, %u channel(s).", id, parts, sampling_rate,
     276            pcm_sample_format_str(format), channels);
     277        if (id != BUFFER_ID)
     278                return ENOENT;
    284279        if (channels != 1 && channels != 2)
    285280                return ENOTSUP;
    286281        if (sampling_rate > 44100)
     282                return ENOTSUP;
     283        // FIXME We only support 16 bit playback
     284        if (format != PCM_SAMPLE_UINT16_LE && format != PCM_SAMPLE_SINT16_LE)
    287285                return ENOTSUP;
    288286
     
    291289                return ENOMEM;
    292290
     291        const bool sign = (format == PCM_SAMPLE_SINT16_LE);
     292
    293293        sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
    294294        sb_dsp_write(dsp, sampling_rate >> 8);
    295295        sb_dsp_write(dsp, sampling_rate & 0xff);
    296296
    297         ddf_log_verbose("Sampling rate: %hhx:%hhx.",
     297        ddf_log_verbose("Sample rate: %hhx:%hhx.",
    298298            sampling_rate >> 8, sampling_rate & 0xff);
    299299
     
    308308        sb_dsp_write(dsp, dsp->active.mode);
    309309
    310         dsp->active.samples = sample_count(sample_size, play_block_size);
     310        dsp->active.samples = sample_count(format, play_block_size);
    311311        sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
    312312        sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
     
    314314        ddf_log_verbose("Playback started, interrupt every %u samples "
    315315            "(~1/%u sec)", dsp->active.samples,
    316             sampling_rate / dsp->active.samples);
     316            sampling_rate / (dsp->active.samples * channels));
    317317
    318318        dsp->active.playing = true;
     
    332332
    333333int sb_dsp_start_record(sb_dsp_t *dsp, unsigned id, unsigned parts,
    334     unsigned sampling_rate, unsigned sample_size, unsigned channels, bool sign)
     334    unsigned channels, unsigned sampling_rate, pcm_sample_format_t format)
    335335{
    336336        assert(dsp);
     
    347347        /* Check supported parameters */
    348348        ddf_log_debug("Requested recording on buffer \"%u\" (%u parts): %uHz, "
    349             "%ssinged %u bit, %u channel(s).", id, parts, sampling_rate,
    350             sign ? "" : "un", sample_size, channels);
    351         if (id != BUFFER_ID)
    352                 return ENOENT;
    353         if (sample_size != 16) // FIXME We only support 16 bit playback
    354                 return ENOTSUP;
     349            "%s, %u channel(s).", id, parts, sampling_rate,
     350            pcm_sample_format_str(format), channels);
     351        if (id != BUFFER_ID)
     352                return ENOENT;
    355353        if (channels != 1 && channels != 2)
    356354                return ENOTSUP;
    357355        if (sampling_rate > 44100)
     356                return ENOTSUP;
     357        // FIXME We only support 16 bit recording
     358        if (format != PCM_SAMPLE_UINT16_LE && format != PCM_SAMPLE_SINT16_LE)
    358359                return ENOTSUP;
    359360
     
    361362        if (!dsp->event_exchange)
    362363                return ENOMEM;
     364
     365        const bool sign = (format == PCM_SAMPLE_SINT16_LE);
    363366
    364367        sb_dsp_write(dsp, SET_SAMPLING_RATE_OUTPUT);
     
    379382        sb_dsp_write(dsp, dsp->active.mode);
    380383
    381         dsp->active.samples = sample_count(sample_size, play_block_size);
     384        dsp->active.samples = sample_count(format, play_block_size);
    382385        sb_dsp_write(dsp, (dsp->active.samples - 1) & 0xff);
    383386        sb_dsp_write(dsp, (dsp->active.samples - 1) >> 8);
     
    385388        ddf_log_verbose("Recording started started, interrupt every %u samples "
    386389            "(~1/%u sec)", dsp->active.samples,
    387             sampling_rate / dsp->active.samples);
     390            sampling_rate / (dsp->active.samples * channels));
    388391        dsp->active.playing = false;
    389392
  • uspace/drv/audio/sb16/dsp.h

    r94694a4 r346643c  
    3838#include <libarch/ddi.h>
    3939#include <errno.h>
     40#include <pcm_sample_format.h>
    4041
    4142#include "registers.h"
     
    7172int sb_dsp_release_buffer(sb_dsp_t *dsp, unsigned id);
    7273int sb_dsp_start_playback(sb_dsp_t *dsp, unsigned id, unsigned parts,
    73     unsigned sample_rate, unsigned sample_size, unsigned channels, bool sign);
     74    unsigned channels, unsigned sample_rate, pcm_sample_format_t format);
    7475int sb_dsp_stop_playback(sb_dsp_t *dsp, unsigned id);
    7576int sb_dsp_start_record(sb_dsp_t *dsp, unsigned id, unsigned parts,
    76     unsigned sample_rate, unsigned sample_size, unsigned channels, bool sign);
     77    unsigned channels, unsigned sample_rate, pcm_sample_format_t format);
    7778int sb_dsp_stop_record(sb_dsp_t *dsp, unsigned id);
    7879
  • uspace/drv/audio/sb16/pcm_iface.c

    r94694a4 r346643c  
    3636#include <errno.h>
    3737#include <audio_pcm_iface.h>
     38#include <pcm_sample_format.h>
    3839
    3940#include "dsp.h"
     
    7172
    7273static int sb_start_playback(ddf_fun_t *fun, unsigned id, unsigned parts,
    73     unsigned sample_rate, unsigned sample_size, unsigned channels, bool sign)
     74    unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
    7475{
    7576        assert(fun);
     
    7778        sb_dsp_t *dsp = fun->driver_data;
    7879        return sb_dsp_start_playback(
    79             dsp, id, parts, sample_rate, sample_size, channels, sign);
     80            dsp, id, parts, channels, sample_rate, format);
    8081}
    8182
     
    8990
    9091static int sb_start_record(ddf_fun_t *fun, unsigned id, unsigned parts,
    91     unsigned sample_rate, unsigned sample_size, unsigned channels, bool sign)
     92    unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
    9293{
    9394        assert(fun);
     
    9596        sb_dsp_t *dsp = fun->driver_data;
    9697        return sb_dsp_start_record(
    97             dsp, id, parts, sample_rate, sample_size, channels, sign);
     98            dsp, id, parts, channels, sample_rate, format);
    9899}
    99100
  • uspace/lib/drv/generic/remote_audio_pcm.c

    r94694a4 r346643c  
    118118}
    119119
    120 int audio_pcm_start_playback(async_exch_t *exch, unsigned id,
    121     unsigned parts, unsigned sample_rate, uint16_t sample_size,
    122     uint8_t channels, bool sign)
     120int audio_pcm_start_playback(async_exch_t *exch, unsigned id, unsigned parts,
     121    unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
    123122{
    124123        if (!exch)
    125124                return EINVAL;
     125        if (parts > UINT8_MAX || channels > UINT8_MAX)
     126                return EINVAL;
     127        assert((format & UINT16_MAX) == format);
    126128        const sysarg_t packed =
    127             (sample_size << 16) | (channels << 8) |
    128             ((parts & 0x7f) << 1) | (sign ? 1 : 0);
     129            (parts << 24) | (channels << 16) | (format & UINT16_MAX);
    129130        return async_req_4_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
    130131            IPC_M_AUDIO_PCM_START_PLAYBACK, id, sample_rate, packed);
     
    139140}
    140141
    141 int audio_pcm_start_record(async_exch_t *exch, unsigned id,
    142     unsigned parts, unsigned sample_rate, uint16_t sample_size,
    143     uint8_t channels, bool sign)
     142int audio_pcm_start_record(async_exch_t *exch, unsigned id, unsigned parts,
     143    unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
    144144{
    145145        if (!exch)
    146146                return EINVAL;
    147         sysarg_t packed =
    148             (sample_size << 16) | (channels << 8) |
    149             ((parts & 0x7f) << 1) | (sign ? 1 : 0);
     147        if (parts > UINT8_MAX || channels > UINT8_MAX)
     148                return EINVAL;
     149        assert((format & UINT16_MAX) == format);
     150        const sysarg_t packed =
     151            (parts << 24) | (channels << 16) | (format & UINT16_MAX);
    150152        return async_req_4_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
    151153            IPC_M_AUDIO_PCM_START_RECORD, id, sample_rate, packed);
     
    309311        const unsigned id = DEV_IPC_GET_ARG1(*call);
    310312        const unsigned rate = DEV_IPC_GET_ARG2(*call);
    311         const unsigned size = DEV_IPC_GET_ARG3(*call) >> 16;
    312         const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 8) & UINT8_MAX;
    313         const unsigned parts = (DEV_IPC_GET_ARG3(*call) >> 1) & 0x7f;
    314         const bool sign = (bool)(DEV_IPC_GET_ARG3(*call) & 1);
     313        const unsigned parts = (DEV_IPC_GET_ARG3(*call) >> 24) & UINT8_MAX;
     314        const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT8_MAX;
     315        const pcm_sample_format_t format =DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
    315316
    316317        const int ret = pcm_iface->start_playback
    317             ? pcm_iface->start_playback(fun, id, parts, rate, size, channels, sign)
     318            ? pcm_iface->start_playback(fun, id, parts, channels, rate, format)
    318319            : ENOTSUP;
    319320        async_answer_0(callid, ret);
     
    338339        const unsigned id = DEV_IPC_GET_ARG1(*call);
    339340        const unsigned rate = DEV_IPC_GET_ARG2(*call);
    340         const unsigned size = DEV_IPC_GET_ARG3(*call) >> 16;
    341         const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 8) & UINT8_MAX;
    342         const unsigned parts = (DEV_IPC_GET_ARG3(*call) >> 1) & 0x7f;
    343         const bool sign = (bool)(DEV_IPC_GET_ARG3(*call) & 1);
     341        const unsigned parts = (DEV_IPC_GET_ARG3(*call) >> 24) & UINT8_MAX;
     342        const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT8_MAX;
     343        const pcm_sample_format_t format =DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
    344344
    345345        const int ret = pcm_iface->start_record
    346             ? pcm_iface->start_record(fun, id, parts, rate, size, channels, sign)
     346            ? pcm_iface->start_record(fun, id, parts, channels, rate, format)
    347347            : ENOTSUP;
    348348        async_answer_0(callid, ret);
  • uspace/lib/drv/include/audio_pcm_iface.h

    r94694a4 r346643c  
    3939#include <async.h>
    4040#include <bool.h>
     41#include <pcm_sample_format.h>
    4142
    4243#include "ddf/driver.h"
     
    4849
    4950int audio_pcm_start_playback(async_exch_t *, unsigned, unsigned,
    50     unsigned, uint16_t, uint8_t, bool);
     51    unsigned, unsigned, pcm_sample_format_t);
    5152int audio_pcm_stop_playback(async_exch_t *, unsigned);
    5253
    5354int audio_pcm_start_record(async_exch_t *, unsigned, unsigned,
    54     unsigned, uint16_t, uint8_t, bool);
     55    unsigned, unsigned, pcm_sample_format_t);
    5556int audio_pcm_stop_record(async_exch_t *, unsigned);
    5657
     
    6263        int (*set_event_session)(ddf_fun_t *, unsigned, async_sess_t *);
    6364        int (*start_playback)(ddf_fun_t *, unsigned, unsigned,
    64             unsigned, unsigned, unsigned, bool);
     65            unsigned, unsigned, pcm_sample_format_t);
    6566        int (*stop_playback)(ddf_fun_t *, unsigned);
    6667        int (*start_record)(ddf_fun_t *, unsigned, unsigned,
    67             unsigned, unsigned, unsigned, bool);
     68            unsigned, unsigned, pcm_sample_format_t);
    6869        int (*stop_record)(ddf_fun_t *, unsigned);
    6970} audio_pcm_iface_t;
Note: See TracChangeset for help on using the changeset viewer.