wav.c 1.4 KB
Newer Older
B
bernard.xiong 已提交
1 2
#include <finsh.h>
#include <dfs_posix.h>
wuyangyong's avatar
wuyangyong 已提交
3
#include "stm32f10x.h"
B
bernard.xiong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

rt_uint32_t wav_length;
rt_uint8_t* wav_buffer;

#define WAV_MP_NUM		2
#define WAV_MP_BUFSZ	(1024 * 1)
#define STATIC_MEMPOOL
#ifdef STATIC_MEMPOOL
static rt_uint8_t mempool[(WAV_MP_BUFSZ + 4)* WAV_MP_NUM]; // 5k x 2
static struct rt_mempool _mp;
#endif
static rt_mp_t mp;

static rt_err_t wav_tx_done(rt_device_t dev, void *buffer)
{
	/* release memory block to memory pool */
	rt_mp_free(buffer);

	return RT_EOK;
}

void wav(char* filename)
{
    int fd;

#ifdef STATIC_MEMPOOL
	rt_mp_init(&_mp, "wav", &mempool[0], sizeof(mempool), WAV_MP_BUFSZ);
	mp = &_mp;
#else
	mp = rt_mp_create("wav", WAV_MP_NUM, WAV_MP_BUFSZ);
#endif

    fd = open(filename, O_RDONLY, 0);
    if (fd >= 0)
    {
		rt_uint8_t* buf;
		rt_size_t 	len;
		rt_device_t device;
B
bernard.xiong 已提交
42

B
bernard.xiong 已提交
43 44 45 46 47
		/* open audio device and set tx done call back */
		device = rt_device_find("snd");
		rt_device_set_tx_complete(device, wav_tx_done);
		rt_device_open(device, RT_DEVICE_OFLAG_WRONLY);

48 49
		do
		{
B
bernard.xiong 已提交
50 51 52 53
			buf = rt_mp_alloc(mp, RT_WAITING_FOREVER);
			len = read(fd, (char*)buf, WAV_MP_BUFSZ);
			if (len > 0) rt_device_write(device, 0, buf, len);
		} while (len != 0);
B
bernard.xiong 已提交
54

B
bernard.xiong 已提交
55 56 57
		/* close device and file */
		rt_device_close(device);
		close(fd);
B
bernard.xiong 已提交
58

B
bernard.xiong 已提交
59 60 61 62 63 64 65 66 67
		/* delete memory pool */
#ifdef STATIC_MEMPOOL
		rt_mp_detach(mp);
#else
		rt_mp_delete(mp);
#endif
    }
}
FINSH_FUNCTION_EXPORT(wav, wav test)