bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 1 | /* |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 2 | * QEMU Timer based audio emulation |
| 3 | * |
| 4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) |
| 5 | * |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 24 | #include "qemu-common.h" |
| 25 | #include "audio.h" |
| 26 | #include "qemu-timer.h" |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 27 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 28 | #define AUDIO_CAP "noaudio" |
| 29 | #include "audio_int.h" |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 30 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 31 | typedef struct NoVoiceOut { |
| 32 | HWVoiceOut hw; |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 33 | int64_t old_ticks; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 34 | } NoVoiceOut; |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 35 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 36 | typedef struct NoVoiceIn { |
| 37 | HWVoiceIn hw; |
| 38 | int64_t old_ticks; |
| 39 | } NoVoiceIn; |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 40 | |
malc | bdff253 | 2009-09-18 11:37:39 +0400 | [diff] [blame] | 41 | static int no_run_out (HWVoiceOut *hw, int live) |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 42 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 43 | NoVoiceOut *no = (NoVoiceOut *) hw; |
malc | bdff253 | 2009-09-18 11:37:39 +0400 | [diff] [blame] | 44 | int decr, samples; |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 45 | int64_t now; |
| 46 | int64_t ticks; |
| 47 | int64_t bytes; |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 48 | |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 49 | now = qemu_get_clock (vm_clock); |
| 50 | ticks = now - no->old_ticks; |
malc | 4f4cc0e | 2009-09-18 08:16:03 +0400 | [diff] [blame] | 51 | bytes = muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ()); |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 52 | bytes = audio_MIN (bytes, INT_MAX); |
| 53 | samples = bytes >> hw->info.shift; |
| 54 | |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 55 | no->old_ticks = now; |
| 56 | decr = audio_MIN (live, samples); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 57 | hw->rpos = (hw->rpos + decr) % hw->samples; |
| 58 | return decr; |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 59 | } |
| 60 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 61 | static int no_write (SWVoiceOut *sw, void *buf, int len) |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 62 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 63 | return audio_pcm_sw_write (sw, buf, len); |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 64 | } |
| 65 | |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 66 | static int no_init_out (HWVoiceOut *hw, struct audsettings *as) |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 67 | { |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 68 | audio_pcm_init_info (&hw->info, as); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 69 | hw->samples = 1024; |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 73 | static void no_fini_out (HWVoiceOut *hw) |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 74 | { |
| 75 | (void) hw; |
| 76 | } |
| 77 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 78 | static int no_ctl_out (HWVoiceOut *hw, int cmd, ...) |
| 79 | { |
| 80 | (void) hw; |
| 81 | (void) cmd; |
| 82 | return 0; |
| 83 | } |
| 84 | |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 85 | static int no_init_in (HWVoiceIn *hw, struct audsettings *as) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 86 | { |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 87 | audio_pcm_init_info (&hw->info, as); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 88 | hw->samples = 1024; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static void no_fini_in (HWVoiceIn *hw) |
| 93 | { |
| 94 | (void) hw; |
| 95 | } |
| 96 | |
| 97 | static int no_run_in (HWVoiceIn *hw) |
| 98 | { |
| 99 | NoVoiceIn *no = (NoVoiceIn *) hw; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 100 | int live = audio_pcm_hw_get_live_in (hw); |
| 101 | int dead = hw->samples - live; |
bellard | ec36b69 | 2006-07-16 18:57:03 +0000 | [diff] [blame] | 102 | int samples = 0; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 103 | |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 104 | if (dead) { |
| 105 | int64_t now = qemu_get_clock (vm_clock); |
| 106 | int64_t ticks = now - no->old_ticks; |
malc | 4f4cc0e | 2009-09-18 08:16:03 +0400 | [diff] [blame] | 107 | int64_t bytes = |
| 108 | muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ()); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 109 | |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 110 | no->old_ticks = now; |
| 111 | bytes = audio_MIN (bytes, INT_MAX); |
| 112 | samples = bytes >> hw->info.shift; |
| 113 | samples = audio_MIN (samples, dead); |
| 114 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 115 | return samples; |
| 116 | } |
| 117 | |
| 118 | static int no_read (SWVoiceIn *sw, void *buf, int size) |
| 119 | { |
| 120 | int samples = size >> sw->info.shift; |
| 121 | int total = sw->hw->total_samples_captured - sw->total_hw_samples_acquired; |
| 122 | int to_clear = audio_MIN (samples, total); |
| 123 | audio_pcm_info_clear_buf (&sw->info, buf, to_clear); |
| 124 | return to_clear; |
| 125 | } |
| 126 | |
| 127 | static int no_ctl_in (HWVoiceIn *hw, int cmd, ...) |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 128 | { |
| 129 | (void) hw; |
| 130 | (void) cmd; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static void *no_audio_init (void) |
| 135 | { |
| 136 | return &no_audio_init; |
| 137 | } |
| 138 | |
| 139 | static void no_audio_fini (void *opaque) |
| 140 | { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 141 | (void) opaque; |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 142 | } |
| 143 | |
blueswir1 | 35f4b58 | 2008-10-06 18:08:30 +0000 | [diff] [blame] | 144 | static struct audio_pcm_ops no_pcm_ops = { |
Juan Quintela | 1dd3e4d | 2009-08-11 02:31:15 +0200 | [diff] [blame] | 145 | .init_out = no_init_out, |
| 146 | .fini_out = no_fini_out, |
| 147 | .run_out = no_run_out, |
| 148 | .write = no_write, |
| 149 | .ctl_out = no_ctl_out, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 150 | |
Juan Quintela | 1dd3e4d | 2009-08-11 02:31:15 +0200 | [diff] [blame] | 151 | .init_in = no_init_in, |
| 152 | .fini_in = no_fini_in, |
| 153 | .run_in = no_run_in, |
| 154 | .read = no_read, |
| 155 | .ctl_in = no_ctl_in |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 158 | struct audio_driver no_audio_driver = { |
Juan Quintela | bee37f3 | 2009-08-11 02:31:14 +0200 | [diff] [blame] | 159 | .name = "none", |
| 160 | .descr = "Timer based audio emulation", |
| 161 | .options = NULL, |
| 162 | .init = no_audio_init, |
| 163 | .fini = no_audio_fini, |
| 164 | .pcm_ops = &no_pcm_ops, |
| 165 | .can_be_default = 1, |
| 166 | .max_voices_out = INT_MAX, |
| 167 | .max_voices_in = INT_MAX, |
| 168 | .voice_size_out = sizeof (NoVoiceOut), |
| 169 | .voice_size_in = sizeof (NoVoiceIn) |
bellard | 7372f88 | 2004-11-11 16:55:09 +0000 | [diff] [blame] | 170 | }; |