mxpopstar
2022-05-04 fc9a39919da8db88c68ee1c41c01f6c26b88f12c
1
2
3
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*!
    \file    audio_out_itf.c
    \brief   audio OUT (playback) interface functions
 
    \version 2020-08-01, V3.0.0, firmware for GD32F30x
*/
 
/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.
 
    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
 
    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.
 
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/
 
#include "audio_core.h"
#include "audio_out_itf.h"
 
/* local function prototypes ('static') */
static uint8_t init         (uint32_t audio_freq, uint32_t volume, uint32_t options);
static uint8_t deinit       (uint32_t options);
static uint8_t audio_cmd    (uint8_t* pbuf, uint32_t size, uint8_t cmd);
static uint8_t volume_ctl   (uint8_t vol);
static uint8_t mute_ctl     (uint8_t cmd);
static uint8_t periodic_tc  (uint8_t cmd);
static uint8_t get_state    (void);
 
audio_fops_struct audio_out_fops = 
{
    init,
    deinit,
    audio_cmd,
    volume_ctl,
    mute_ctl,
    periodic_tc,
    get_state
};
 
static uint8_t audio_state = AUDIO_STATE_INACTIVE;
 
/*!
    \brief      initialize and configures all required resources for audio play function
    \param[in]  audio_freq: statrt_up audio frequency
    \param[in]  volume: start_up volume to be set
    \param[in]  options: specific options passed to low layer function
    \param[out] none
    \retval     AUDIO_OK if all operations succeed, otherwise, AUDIO_FAIL.
*/
static uint8_t init (uint32_t audio_freq, uint32_t volume, uint32_t options)
{
    static uint32_t initialized = 0U;
 
    /* check if the low layer has already been initialized */
    if (0U == initialized) {
        /* call low layer function */
        if (0U != eval_audio_init(OUTPUT_DEVICE_AUTO, volume, audio_freq)) {
            audio_state = AUDIO_STATE_ERROR;
 
            return AUDIO_FAIL;
        }
 
        /* set the initialization flag to prevent reinitializing the interface again */
        initialized = 1U;
    }
 
    /* update the audio state machine */
    audio_state = AUDIO_STATE_ACTIVE;
 
    return AUDIO_OK;
}
 
/*!
    \brief      free all resources used by low layer and stops audio-play function
    \param[in]  options: specific options passed to low layer function
    \param[out] none
    \retval     AUDIO_OK if all operations succeed, otherwise, AUDIO_FAIL.
*/
static uint8_t deinit (uint32_t options)
{
    /* update the audio state machine */
    audio_state = AUDIO_STATE_INACTIVE;
 
    return AUDIO_OK;
}
 
/*!
    \brief      play, stop, pause or resume current file
    \param[in]  pbuf: address from which file should be played
    \param[in]  size: size of the current buffer/file
    \param[in]  cmd: command to be executed, can be:
      \arg        AUDIO_CMD_PLAY
      \arg        AUDIO_CMD_PAUSE
      \arg        AUDIO_CMD_RESUME
      \arg        AUDIO_CMD_STOP
    \param[out] none
    \retval     AUDIO_OK if all operations succeed, otherwise, AUDIO_FAIL.
*/
static uint8_t audio_cmd (uint8_t* pbuf, uint32_t size, uint8_t cmd)
{
    uint8_t status = AUDIO_OK;
 
    /* check the current state */
    if ((AUDIO_STATE_INACTIVE == audio_state) || (AUDIO_STATE_ERROR == audio_state)) {
        audio_state = AUDIO_STATE_ERROR;
 
        return AUDIO_FAIL;
    }
 
    switch (cmd) {
        /* process the play command */
        case AUDIO_CMD_PLAY:
            /* if current state is active or stopped */
            if ((AUDIO_STATE_ACTIVE == audio_state) || \
                (AUDIO_STATE_STOPPED == audio_state) || \
                (AUDIO_STATE_PLAYING == audio_state)) {
                audio_mal_play((uint32_t)pbuf, (size / 2U));
                audio_state = AUDIO_STATE_PLAYING;
            } else if (AUDIO_STATE_PAUSED == audio_state) {
                if (eval_audio_pause_resume(AUDIO_RESUME, (uint32_t)pbuf, (size / 2U))) {
                    audio_state = AUDIO_STATE_ERROR;
 
                    status = AUDIO_FAIL;
                } else {
                    audio_state = AUDIO_STATE_PLAYING;
                }
            } else {
                status = AUDIO_FAIL;
            }
            break;
 
        /* process the stop command */
        case AUDIO_CMD_STOP:
            if (AUDIO_STATE_PLAYING != audio_state) {
                /* unsupported command */
                status = AUDIO_FAIL;
            } else if (eval_audio_stop(CODEC_PDWN_SW)) {
                audio_state = AUDIO_STATE_ERROR;
 
                status = AUDIO_FAIL;
            } else {
                audio_state = AUDIO_STATE_STOPPED;
            }
            break;
 
        /* process the pause command */
        case AUDIO_CMD_PAUSE:
            if (AUDIO_STATE_PLAYING != audio_state) {
                /* unsupported command */
                status = AUDIO_FAIL;
            } else if (eval_audio_pause_resume(AUDIO_PAUSE, (uint32_t)pbuf, (size / 2U))) {
                audio_state = AUDIO_STATE_ERROR;
 
                status = AUDIO_FAIL;
            } else {
                audio_state = AUDIO_STATE_PAUSED;
            }
            break;
 
        /* unsupported command */
        default:
            break;
    }
 
    return status;
}
 
/*!
    \brief      set the volume level
    \param[in]  vol: volume level to be set in % (from 0% to 100%)
    \param[out] none
    \retval     AUDIO_OK if all operations succeed, otherwise, AUDIO_FAIL.
*/
static uint8_t volume_ctl (uint8_t vol)
{
    /* call low layer volume setting function */  
    if (eval_audio_volume_ctl(vol)) {
        audio_state = AUDIO_STATE_ERROR;
 
        return AUDIO_FAIL;
    }
 
    return AUDIO_OK;
}
 
/*!
    \brief      mute or unmute the audio current output
    \param[in]  cmd: can be 0 to unmute, or 1 to mute
    \param[out] none
    \retval     AUDIO_OK if all operations succeed, otherwise, AUDIO_FAIL.
*/
static uint8_t mute_ctl (uint8_t cmd)
{
    /* call low layer mute setting function */
    if (eval_audio_mute((uint32_t)cmd)) {
        audio_state = AUDIO_STATE_ERROR;
 
        return AUDIO_FAIL;
    }
 
    return AUDIO_OK;
}
 
/*!
    \brief      periodic transfer control
    \param[in]  cmd: command
    \param[out] none
    \retval     AUDIO_OK if all operations succeed, otherwise, AUDIO_FAIL.
*/
static uint8_t periodic_tc (uint8_t cmd)
{
    return AUDIO_OK;
}
 
/*!
    \brief      return the current state of the audio machine
    \param[in]  none
    \param[out] none
    \retval     AUDIO_OK if all operations succeed, otherwise, AUDIO_FAIL.
*/
static uint8_t get_state (void)
{
    return audio_state;
}