Harald Welte | 2ae47af | 2017-05-27 23:35:23 +0200 | [diff] [blame] | 1 | /* Process Queue: ALSA PCM output */ |
| 2 | /* (C) 2017 by Harald Welte <laforge@gnumonks.org> */ |
| 3 | |
| 4 | /* |
| 5 | * This file is part of gapk (GSM Audio Pocket Knife). |
| 6 | * |
| 7 | * gapk is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * gapk is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with gapk. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #include <gapk/codecs.h> |
| 27 | #include <gapk/formats.h> |
| 28 | #include <gapk/procqueue.h> |
| 29 | |
| 30 | #include "config.h" |
| 31 | |
| 32 | #ifdef HAVE_ALSA |
| 33 | |
| 34 | #include <alsa/asoundlib.h> |
| 35 | |
| 36 | |
| 37 | struct pq_state_alsa { |
| 38 | snd_pcm_t *pcm_handle; |
| 39 | unsigned int blk_len; |
| 40 | }; |
| 41 | |
| 42 | static int |
Harald Welte | 5912848 | 2017-05-28 10:20:54 +0200 | [diff] [blame^] | 43 | pq_cb_alsa_input(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_len) |
Harald Welte | 2ae47af | 2017-05-27 23:35:23 +0200 | [diff] [blame] | 44 | { |
| 45 | struct pq_state_alsa *state = _state; |
| 46 | unsigned int num_samples = state->blk_len/2; |
| 47 | int rv; |
| 48 | rv = snd_pcm_readi(state->pcm_handle, out, num_samples); |
Harald Welte | 5912848 | 2017-05-28 10:20:54 +0200 | [diff] [blame^] | 49 | if (rv < 0) |
| 50 | return rv; |
| 51 | return rv * sizeof(uint16_t); |
Harald Welte | 2ae47af | 2017-05-27 23:35:23 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static int |
Harald Welte | 5912848 | 2017-05-28 10:20:54 +0200 | [diff] [blame^] | 55 | pq_cb_alsa_output(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_len) |
Harald Welte | 2ae47af | 2017-05-27 23:35:23 +0200 | [diff] [blame] | 56 | { |
| 57 | struct pq_state_alsa *state = _state; |
Harald Welte | 5912848 | 2017-05-28 10:20:54 +0200 | [diff] [blame^] | 58 | unsigned int num_samples = in_len/2; |
Harald Welte | 2ae47af | 2017-05-27 23:35:23 +0200 | [diff] [blame] | 59 | int rv; |
| 60 | rv = snd_pcm_writei(state->pcm_handle, in, num_samples); |
| 61 | return rv == num_samples ? 0 : -1; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | pq_cb_alsa_exit(void *_state) |
| 66 | { |
| 67 | struct pq_state_alsa *state = _state; |
| 68 | snd_pcm_close(state->pcm_handle); |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | pq_queue_alsa_op(struct pq *pq, const char *alsa_dev, unsigned int blk_len, int in_out_n) |
| 73 | { |
| 74 | struct pq_item *item; |
| 75 | struct pq_state_alsa *state; |
| 76 | snd_pcm_hw_params_t *hw_params; |
| 77 | int rc = -1; |
| 78 | |
| 79 | state = calloc(1, sizeof(struct pq_state_alsa)); |
| 80 | if (!state) |
| 81 | return -ENOMEM; |
| 82 | |
| 83 | rc = snd_pcm_open(&state->pcm_handle, alsa_dev, |
| 84 | in_out_n ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, 0); |
| 85 | if (rc < 0) |
| 86 | return rc; |
| 87 | state->blk_len = blk_len; |
| 88 | |
| 89 | rc = snd_pcm_hw_params_malloc(&hw_params); |
| 90 | if (rc < 0) |
| 91 | goto out_close; |
| 92 | |
| 93 | rc = snd_pcm_hw_params_any(state->pcm_handle, hw_params); |
| 94 | if (rc < 0) |
| 95 | goto out_free_par; |
| 96 | |
| 97 | rc = snd_pcm_hw_params_set_access(state->pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); |
| 98 | if (rc < 0) |
| 99 | goto out_free_par; |
| 100 | |
| 101 | rc = snd_pcm_hw_params_set_format(state->pcm_handle, hw_params, SND_PCM_FORMAT_S16_LE); |
| 102 | if (rc < 0) |
| 103 | goto out_free_par; |
| 104 | |
| 105 | rc = snd_pcm_hw_params_set_rate(state->pcm_handle, hw_params, 8000, 0); |
| 106 | if (rc < 0) |
| 107 | goto out_free_par; |
| 108 | |
| 109 | rc = snd_pcm_hw_params_set_channels (state->pcm_handle, hw_params, 1); |
| 110 | if (rc < 0) |
| 111 | goto out_free_par; |
| 112 | |
| 113 | rc = snd_pcm_hw_params(state->pcm_handle, hw_params); |
| 114 | if (rc < 0) |
| 115 | goto out_free_par; |
| 116 | |
| 117 | snd_pcm_hw_params_free(hw_params); |
| 118 | |
| 119 | item = pq_add_item(pq); |
| 120 | if (!item) { |
| 121 | rc = -ENOMEM; |
| 122 | goto out_close; |
| 123 | } |
| 124 | |
| 125 | item->len_in = in_out_n ? 0 : blk_len; |
| 126 | item->len_out = in_out_n ? blk_len : 0; |
| 127 | item->state = state; |
| 128 | item->proc = in_out_n ? pq_cb_alsa_input : pq_cb_alsa_output; |
| 129 | item->exit = pq_cb_alsa_exit; |
| 130 | |
| 131 | return 0; |
| 132 | |
| 133 | out_free_par: |
| 134 | snd_pcm_hw_params_free(hw_params); |
| 135 | out_close: |
| 136 | snd_pcm_close(state->pcm_handle); |
| 137 | free(state); |
| 138 | return rc; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /*! Add ALSA input to given processing queue |
| 143 | * This usually only makes sense as first item in the queue |
| 144 | * \param pq Processing Queue to add the ALSA input to |
| 145 | * \param[in] hwdev ALSA hardware device to use |
| 146 | * \param[in] blk_len block length to be read from device |
| 147 | * \returns 0 on sucess; negative on error */ |
| 148 | int |
| 149 | pq_queue_alsa_input(struct pq *pq, const char *hwdev, unsigned int blk_len) |
| 150 | { |
| 151 | return pq_queue_alsa_op(pq, hwdev, blk_len, 1); |
| 152 | } |
| 153 | |
| 154 | /*! Add ALSA output to given processing queue |
| 155 | * This usually only makes sense as first item in the queue |
| 156 | * \param pq Processing Queue to add the ALSA output to |
| 157 | * \param[in] hwdev ALSA hardware device to use |
| 158 | * \param[in] blk_len block length to be written to device |
| 159 | * \returns 0 on sucess; negative on error */ |
| 160 | int |
| 161 | pq_queue_alsa_output(struct pq *pq, const char *hwdev, unsigned int blk_len) |
| 162 | { |
| 163 | return pq_queue_alsa_op(pq, hwdev, blk_len, 0); |
| 164 | } |
| 165 | |
| 166 | #endif /* HAVE_ALSA */ |