audio.c 15.0 KB
Newer Older
Y
yygg 已提交
1
/*
2 3 4
 * File      : audio.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
Y
yygg 已提交
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-05-09     Urey      first version
Y
yygg 已提交
23 24 25 26 27 28 29 30 31
 */

#include <stdio.h>
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#include <drivers/audio.h>

32 33 34
#include "audio_pipe.h"


Y
yygg 已提交
35 36 37 38 39 40 41
#define AUDIO_DEBUG   0
#if AUDIO_DEBUG
#define AUDIO_DBG(...)     printf("[AUDIO]:"),printf(__VA_ARGS__)
#else
#define AUDIO_DBG(...)
#endif

42 43
static struct rt_audio_pipe audio_pipe;

44
static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
Y
yygg 已提交
45
{
46 47 48
    rt_err_t result = RT_EOK;
    rt_base_t level;
    struct rt_audio_frame frame;
Y
yygg 已提交
49

50
    RT_ASSERT(audio != RT_NULL);
Y
yygg 已提交
51

52 53
    //check repaly queue is empty
    if (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) != RT_EOK)
Y
yygg 已提交
54
    {
55 56
        AUDIO_DBG("TX queue is empty\n");
        result = -RT_EEMPTY;
Y
yygg 已提交
57 58

        level = rt_hw_interrupt_disable();
59
        audio->replay->activated = RT_FALSE;
Y
yygg 已提交
60 61
        rt_hw_interrupt_enable(level);

62
        goto _exit;
Y
yygg 已提交
63 64
    }

65
    if (audio->ops->transmit != RT_NULL)
Y
yygg 已提交
66
    {
67 68
        AUDIO_DBG("audio transmit...\n");
        if (audio->ops->transmit(audio, frame.data_ptr, RT_NULL, frame.data_size) != frame.data_size)
Y
yygg 已提交
69
        {
70
            result = -RT_EBUSY;
Y
yygg 已提交
71

72
            goto _exit;
Y
yygg 已提交
73 74 75
        }
    }

76 77
    //pop the head frame...
    rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
Y
yygg 已提交
78

79
    _exit: return result;
Y
yygg 已提交
80 81
}

82
static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio)
Y
yygg 已提交
83
{
84
    struct rt_audio_frame frame;
Y
yygg 已提交
85

86 87 88
    if (audio->replay == RT_NULL)
        return -RT_EIO;
    while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK)
Y
yygg 已提交
89
    {
90 91
        //pop the head frame...
        rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
Y
yygg 已提交
92

93 94 95
        /* notify transmitted complete. */
        if (audio->parent.tx_complete != RT_NULL)
            audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr);
Y
yygg 已提交
96 97 98 99 100 101 102 103 104 105 106
    }

    return RT_EOK;
}

static rt_err_t _audio_dev_init(struct rt_device *dev)
{
    rt_err_t result = RT_EOK;
    struct rt_audio_device *audio;

    RT_ASSERT(dev != RT_NULL);
107
    audio = (struct rt_audio_device *) dev;
Y
yygg 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121

    /* initialize replay & record */
    audio->replay = RT_NULL;
    audio->record = RT_NULL;

    /* apply configuration */
    if (audio->ops->init)
        result = audio->ops->init(audio);

    return result;
}

static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
{
122 123 124
    rt_err_t result = RT_EOK;
    rt_base_t level;
    struct rt_audio_device *audio;
Y
yygg 已提交
125 126

    RT_ASSERT(dev != RT_NULL);
127
    audio = (struct rt_audio_device *) dev;
Y
yygg 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140

    /* check device flag with the open flag */
    if ((oflag & RT_DEVICE_OFLAG_RDONLY) && !(dev->flag & RT_DEVICE_FLAG_RDONLY))
        return -RT_EIO;
    if ((oflag & RT_DEVICE_OFLAG_WRONLY) && !(dev->flag & RT_DEVICE_FLAG_WRONLY))
        return -RT_EIO;

    /* get open flags */
    dev->open_flag = oflag & 0xff;

    /* initialize the Rx/Tx structure according to open flag */
    if (oflag & RT_DEVICE_OFLAG_WRONLY)
    {
141 142 143 144
        AUDIO_DBG("open audio device ,oflag = %x\n",oflag);
        if (audio->replay == RT_NULL)
        {
            struct rt_audio_replay *replay = (struct rt_audio_replay *) rt_malloc(sizeof(struct rt_audio_replay));
Y
yygg 已提交
145

146 147 148 149 150
            if (replay == RT_NULL)
            {
                AUDIO_DBG("request memory for replay error\n");
                return -RT_ENOMEM;
            }
Y
yygg 已提交
151

152 153
            //init queue for audio replay
            rt_data_queue_init(&replay->queue, CFG_AUDIO_REPLAY_QUEUE_COUNT, CFG_AUDIO_REPLAY_QUEUE_COUNT / 2, RT_NULL);
Y
yygg 已提交
154

155 156 157
            replay->activated = RT_FALSE;
            audio->replay = replay;
        }
Y
yygg 已提交
158 159 160 161

        dev->open_flag |= RT_DEVICE_OFLAG_WRONLY;
    }

162
    if (oflag & RT_DEVICE_OFLAG_RDONLY)
Y
yygg 已提交
163
    {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        if (audio->record == RT_NULL)
        {
            struct rt_audio_record *record = (struct rt_audio_record *) rt_malloc(sizeof(struct rt_audio_record));

            if (record == RT_NULL)
            {
                AUDIO_DBG("request memory for record error\n");
                return -RT_ENOMEM;
            }

            //init pipe for record
            {
                rt_size_t size = CFG_AUDIO_RECORD_PIPE_SIZE;
                rt_uint8_t *buf = rt_malloc(CFG_AUDIO_RECORD_PIPE_SIZE);

                if (buf == RT_NULL)
                {
                    rt_free(record);
                    AUDIO_DBG("request pipe memory error\n");

                    return -RT_ENOMEM;
                }
186 187
                
                rt_audio_pipe_init(&audio_pipe, "recpipe", RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD, buf,
188 189 190 191 192 193 194 195 196 197
                             CFG_AUDIO_RECORD_PIPE_SIZE);
            }

            record->activated = RT_FALSE;
            audio->record = record;
        }

        //open record pipe
        if (audio->record != RT_NULL)
        {
198
            rt_device_open(RT_DEVICE(&audio_pipe), RT_DEVICE_OFLAG_RDONLY);
199
        }
Y
yygg 已提交
200 201 202 203 204 205 206 207 208

        dev->open_flag |= RT_DEVICE_OFLAG_RDONLY;
    }

    return RT_EOK;
}

static rt_err_t _audio_dev_close(struct rt_device *dev)
{
209
    struct rt_audio_device *audio;
Y
yygg 已提交
210
    RT_ASSERT(dev != RT_NULL);
211
    audio = (struct rt_audio_device *) dev;
Y
yygg 已提交
212

213 214 215
    //shutdown the lower device
    if (audio->ops->shutdown != RT_NULL)
        audio->ops->shutdown(audio);
Y
yygg 已提交
216

217
    if (dev->open_flag & RT_DEVICE_OFLAG_WRONLY)
Y
yygg 已提交
218
    {
219 220 221
        struct rt_audio_frame frame;
        //stop replay stream
        audio->ops->stop(audio, AUDIO_STREAM_REPLAY);
Y
yygg 已提交
222

223 224 225 226 227
        //flush all frame
        while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK)
        {
            //pop the head frame...
            rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
Y
yygg 已提交
228

229 230 231 232
            /* notify transmitted complete. */
            if (audio->parent.tx_complete != RT_NULL)
                audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr);
        }
Y
yygg 已提交
233

234
        dev->open_flag &= ~RT_DEVICE_OFLAG_WRONLY;
Y
yygg 已提交
235 236
    }

237
    if (dev->open_flag & RT_DEVICE_OFLAG_RDONLY)
Y
yygg 已提交
238
    {
239 240
        //stop record stream
        audio->ops->stop(audio, AUDIO_STREAM_RECORD);
Y
yygg 已提交
241

242 243
        //close record pipe
        if (audio->record != RT_NULL)
244
            rt_device_close(RT_DEVICE(&audio_pipe));
Y
yygg 已提交
245

246
        dev->open_flag &= ~RT_DEVICE_OFLAG_RDONLY;
Y
yygg 已提交
247 248
    }

249
    return RT_EOK;
Y
yygg 已提交
250 251 252 253
}

static rt_size_t _audio_dev_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{
254
    struct rt_audio_device *audio;
Y
yygg 已提交
255
    RT_ASSERT(dev != RT_NULL);
256 257 258
    audio = (struct rt_audio_device *) dev;
    if (!(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) || (audio->record == RT_NULL))
        return 0;
Y
yygg 已提交
259

260
    return rt_device_read(RT_DEVICE(&audio_pipe), pos, buffer, size);
Y
yygg 已提交
261 262 263 264
}

static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
265 266 267
    rt_err_t result = RT_EOK;
    rt_base_t level;
    struct rt_audio_device *audio;
Y
yygg 已提交
268 269

    RT_ASSERT(dev != RT_NULL);
270
    audio = (struct rt_audio_device *) dev;
Y
yygg 已提交
271

272 273
    if (!(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) || (audio->replay == RT_NULL))
        return 0;
Y
yygg 已提交
274 275 276 277

    AUDIO_DBG("audio write : pos = %d,buffer = %x,size = %d\n",pos,(rt_uint32_t)buffer,size);
    //push a new frame to tx queue
    {
278 279 280
        result = rt_data_queue_push(&audio->replay->queue, buffer, size,
        RT_WAITING_FOREVER);
        if (result != RT_EOK)
Y
yygg 已提交
281
        {
282 283
            AUDIO_DBG("TX frame queue push error\n");
            rt_set_errno(-RT_EFULL);
Y
yygg 已提交
284 285 286 287 288 289
            return 0;
        }
    }

    //check tx state...
    level = rt_hw_interrupt_disable();
290
    if (audio->replay->activated != RT_TRUE)
Y
yygg 已提交
291
    {
292 293
        audio->replay->activated = RT_TRUE;
        rt_hw_interrupt_enable(level);
Y
yygg 已提交
294

295
        _audio_send_replay_frame(audio);
Y
yygg 已提交
296
    }
H
heyuanjie87 已提交
297 298 299 300
    else
    {
        rt_hw_interrupt_enable(level);
    }
Y
yygg 已提交
301 302 303 304

    return size;
}

B
bernard 已提交
305
static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args)
Y
yygg 已提交
306
{
307 308
    rt_err_t result = RT_EOK;
    struct rt_audio_device *audio;
Y
yygg 已提交
309
    RT_ASSERT(dev != RT_NULL);
310
    audio = (struct rt_audio_device *) dev;
Y
yygg 已提交
311 312

    //dev stat...
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    switch (cmd)
    {
        case AUDIO_CTL_GETCAPS:
        {
            struct rt_audio_caps *caps = (struct rt_audio_caps *) args;

            AUDIO_DBG("AUDIO_CTL_GETCAPS: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
            if (audio->ops->getcaps != RT_NULL)
            {
                result = audio->ops->getcaps(audio, caps);
            }
        }
        break;
        case AUDIO_CTL_CONFIGURE:
        {
            struct rt_audio_caps *caps = (struct rt_audio_caps *) args;

            AUDIO_DBG("AUDIO_CTL_CONFIGURE: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
            if (audio->ops->configure != RT_NULL)
            {
                result = audio->ops->configure(audio, caps);
            }
        }

        break;
        case AUDIO_CTL_SHUTDOWN:
        {
            AUDIO_DBG("AUDIO_CTL_SHUTDOWN\n");

            if (audio->ops->shutdown != RT_NULL)
                result = audio->ops->shutdown(audio);

            //flush replay frame...
            _audio_flush_replay_frame(audio);
        }
        break;

        case AUDIO_CTL_START:
        {
            int stream = *(int *) args;

            AUDIO_DBG("AUDIO_CTL_START: stream = %d\n",stream);
            if (audio->ops->start != RT_NULL)
                result = audio->ops->start(audio, stream);
        }
        break;
        case AUDIO_CTL_STOP:
        {
            int stream = *(int *) args;

            AUDIO_DBG("AUDIO_CTL_STOP: stream = %d\n",stream);
            if (audio->ops->start != RT_NULL)
                result = audio->ops->stop(audio, stream);

            if (stream == AUDIO_STREAM_REPLAY)
            {
                _audio_flush_replay_frame(audio);
            }
        }
        break;
        case AUDIO_CTL_PAUSE:
        {
            int stream = *(int *) args;

            AUDIO_DBG("AUDIO_CTL_PAUSE: stream = %d\n",stream);
            if (audio->ops->start != RT_NULL)
                result = audio->ops->suspend(audio, stream);
        }
        break;
        case AUDIO_CTL_RESUME:
        {
            int stream = *(int *) args;

            AUDIO_DBG("AUDIO_CTL_RESUME: stream = %d\n",stream);
            if (audio->ops->start != RT_NULL)
                result = audio->ops->resume(audio, stream);

            //resume tx frame...
            if (stream == AUDIO_STREAM_REPLAY)
                _audio_send_replay_frame(audio);
        }
        break;
        case AUDIO_CTL_ALLOCBUFFER:
        {
            struct rt_audio_buf_desc *desc = (struct rt_audio_buf_desc *) args;

            if (desc)
            {
                desc->data_size = AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2;
                desc->data_ptr = rt_mp_alloc(&audio->mp, RT_WAITING_FOREVER);

                result = RT_EOK;
            }
            else result = -RT_EIO;
        }
        break;
        case AUDIO_CTL_FREEBUFFER:
        {
            rt_uint8_t *data_ptr = (rt_uint8_t *) args;
            if (data_ptr)
                rt_mp_free(data_ptr);
        }
        break;
        default:
            result = audio->ops->control(audio, cmd, args);
        break;
    }
Y
yygg 已提交
420 421 422 423 424 425 426 427 428 429

    return result;
}

rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data)
{
    struct rt_device *device;
    RT_ASSERT(audio != RT_NULL);
    device = &(audio->parent);

430
    device->type = RT_Device_Class_Sound;
Y
yygg 已提交
431 432 433
    device->rx_indicate = RT_NULL;
    device->tx_complete = RT_NULL;

434 435 436 437 438 439 440 441 442 443 444 445 446 447
    device->init = _audio_dev_init;
    device->open = _audio_dev_open;
    device->close = _audio_dev_close;
    device->read = _audio_dev_read;
    device->write = _audio_dev_write;
    device->control = _audio_dev_control;
    device->user_data = data;

    //init memory pool for replay
    {
        rt_uint8_t *mempool = rt_malloc(AUDIO_DEVICE_DECODE_MP_SZ);
        rt_mp_init(&audio->mp, "adu_mp", mempool, AUDIO_DEVICE_DECODE_MP_SZ,
        AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2);
    }
Y
yygg 已提交
448 449 450 451 452 453 454

    /* register a character device */
    return rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE);
}

int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
{
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    int speed = 0;
    switch (bitValue)
    {
        case AUDIO_SAMP_RATE_8K:
            speed = 8000;
        break;
        case AUDIO_SAMP_RATE_11K:
            speed = 11052;
        break;
        case AUDIO_SAMP_RATE_16K:
            speed = 16000;
        break;
        case AUDIO_SAMP_RATE_22K:
            speed = 22050;
        break;
        case AUDIO_SAMP_RATE_32K:
            speed = 32000;
        break;
        case AUDIO_SAMP_RATE_44K:
            speed = 44100;
        break;
        case AUDIO_SAMP_RATE_48K:
            speed = 48000;
        break;
        case AUDIO_SAMP_RATE_96K:
            speed = 96000;
        break;
        case AUDIO_SAMP_RATE_128K:
            speed = 128000;
        break;
        case AUDIO_SAMP_RATE_160K:
            speed = 160000;
        break;
        case AUDIO_SAMP_RATE_172K:
            speed = 176400;
        break;
        case AUDIO_SAMP_RATE_192K:
            speed = 192000;
        break;
        default:

        break;
    }
Y
yygg 已提交
498

499 500
    return speed;
}
Y
yygg 已提交
501 502 503 504 505

rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format)
{
    switch (format)
    {
506 507 508 509 510 511 512 513 514 515
        case AUDIO_FMT_PCM_U8:
        case AUDIO_FMT_PCM_S8:
            return 8;
        case AUDIO_FMT_PCM_S16_LE:
        case AUDIO_FMT_PCM_S16_BE:
        case AUDIO_FMT_PCM_U16_LE:
        case AUDIO_FMT_PCM_U16_BE:
            return 16;
        default:
            return 32;
Y
yygg 已提交
516 517 518
    };
}

519
void rt_audio_tx_complete(struct rt_audio_device *audio, rt_uint8_t *pbuf)
Y
yygg 已提交
520
{
521 522 523 524 525 526 527 528 529 530 531 532
    rt_err_t result;
    AUDIO_DBG("audio tx complete ptr=%x...\n",(rt_uint32_t)pbuf);

    //try to send all frame
    do
    {
        result = _audio_send_replay_frame(audio);
    } while (result == RT_EOK);

    /* notify transmitted complete. */
    if (audio->parent.tx_complete != RT_NULL)
        audio->parent.tx_complete(&audio->parent, (void *) pbuf);
Y
yygg 已提交
533 534
}

535
void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len)
Y
yygg 已提交
536
{
537
    rt_err_t result = RT_EOK;
Y
yygg 已提交
538 539

    //save data to record pipe
540
    rt_device_write(RT_DEVICE(RT_DEVICE(&audio_pipe)), 0, pbuf, len);
Y
yygg 已提交
541 542

    /* invoke callback */
543 544
    if (audio->parent.rx_indicate != RT_NULL)
        audio->parent.rx_indicate(&audio->parent, len);
Y
yygg 已提交
545 546
}