OSDN Git Service

Added volume control feature.
[kozos-expbrd/kozos_expbrd.git] / firm / sample / sample1 / os / task_audio.c
1 #include "task_audio.h"
2 #include "vs1011e.h"
3 #include "kozos.h"
4 #include "pff.h"
5 #include "lib.h"
6
7 #define AUDIO_CMD_PLAY      'd'
8 #define AUDIO_CMD_VOLUME    'v'
9
10 int audio_play(int (*readfunc)(void *buf, int siz))
11 {
12   const int siz = 1024;
13   char *p;
14   int cnt;
15
16   p = kz_kmalloc(8 + siz);
17   cnt = readfunc((void *)&p[8], siz);
18
19   p[0] = AUDIO_CMD_PLAY;
20   p[1] = 0;
21   p[2] = (siz >> 8) & 0xff;
22   p[3] = (siz >> 0) & 0xff;
23   p[4] = (cnt >> 8) & 0xff;
24   p[5] = (cnt >> 0) & 0xff;
25   p[6] = 0;
26   p[7] = 0;
27   kz_send(MSGBOX_ID_AUDIO, 8 + siz, p);
28
29   return (siz == cnt) ? 1 : 0;
30 }
31
32 int audio_volume(const int left, const int right)
33 {
34   char *p;
35   p = kz_kmalloc(4);
36   p[0] = AUDIO_CMD_VOLUME;
37   p[1] = 0;
38   p[2] = 255 - left;
39   p[3] = 255 - right;
40   if ((0 <= left) && (left <= 255) && (0 <= right) && (right <= 255)) {
41       kz_send(MSGBOX_ID_AUDIO, 4, p);
42       return 1;
43   }
44   return 0;
45 }
46
47 static int waitfunc(void)
48 {
49     kz_wait();
50     return 1;
51 }
52
53 static int audio_cmdproc(char *p)
54 {
55   int cmd = p[0];
56   switch (cmd) {
57   case AUDIO_CMD_PLAY:
58     vs1011e_play_with_data(
59         &p[8],
60         ((unsigned char)p[4] << 8) | ((unsigned char)p[5] << 0),
61         waitfunc);
62     break;
63   case AUDIO_CMD_VOLUME:
64     puts("L:"); putxval(p[2], 2); puts("\n");
65     puts("R:"); putxval(p[3], 2); puts("\n");
66     vs1011e_volume_write(p[2], p[3]);
67     break;
68   default:
69     break;
70   }
71   kz_kmfree(p);
72
73   return 0;
74 }
75
76 int task_audio(int argc, char *argv[])
77 {
78   int size;
79   char *p;
80
81   while (1) {
82     kz_recv(MSGBOX_ID_AUDIO, &size, &p);
83     audio_cmdproc(p);
84   }
85
86   return 0;
87 }
88