Statistics
| Branch: | Revision:

root / hw / pcspk.c @ 8ca209ad

History | View | Annotate | Download (4.2 kB)

1 fd06c375 bellard
/*
2 fd06c375 bellard
 * QEMU PC speaker emulation
3 fd06c375 bellard
 *
4 fd06c375 bellard
 * Copyright (c) 2006 Joachim Henke
5 fd06c375 bellard
 *
6 fd06c375 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 fd06c375 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 fd06c375 bellard
 * in the Software without restriction, including without limitation the rights
9 fd06c375 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 fd06c375 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 fd06c375 bellard
 * furnished to do so, subject to the following conditions:
12 fd06c375 bellard
 *
13 fd06c375 bellard
 * The above copyright notice and this permission notice shall be included in
14 fd06c375 bellard
 * all copies or substantial portions of the Software.
15 fd06c375 bellard
 *
16 fd06c375 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 fd06c375 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 fd06c375 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 fd06c375 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 fd06c375 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 fd06c375 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 fd06c375 bellard
 * THE SOFTWARE.
23 fd06c375 bellard
 */
24 fd06c375 bellard
25 87ecb68b pbrook
#include "hw.h"
26 87ecb68b pbrook
#include "pc.h"
27 87ecb68b pbrook
#include "isa.h"
28 87ecb68b pbrook
#include "audio/audio.h"
29 87ecb68b pbrook
#include "qemu-timer.h"
30 fd06c375 bellard
31 fd06c375 bellard
#define PCSPK_BUF_LEN 1792
32 fd06c375 bellard
#define PCSPK_SAMPLE_RATE 32000
33 fd06c375 bellard
#define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
34 fd06c375 bellard
#define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
35 fd06c375 bellard
36 fd06c375 bellard
typedef struct {
37 fd06c375 bellard
    uint8_t sample_buf[PCSPK_BUF_LEN];
38 fd06c375 bellard
    QEMUSoundCard card;
39 fd06c375 bellard
    SWVoiceOut *voice;
40 fd06c375 bellard
    PITState *pit;
41 fd06c375 bellard
    unsigned int pit_count;
42 fd06c375 bellard
    unsigned int samples;
43 fd06c375 bellard
    unsigned int play_pos;
44 fd06c375 bellard
    int data_on;
45 fd06c375 bellard
    int dummy_refresh_clock;
46 fd06c375 bellard
} PCSpkState;
47 fd06c375 bellard
48 fd06c375 bellard
static const char *s_spk = "pcspk";
49 fd06c375 bellard
static PCSpkState pcspk_state;
50 fd06c375 bellard
51 fd06c375 bellard
static inline void generate_samples(PCSpkState *s)
52 fd06c375 bellard
{
53 fd06c375 bellard
    unsigned int i;
54 fd06c375 bellard
55 fd06c375 bellard
    if (s->pit_count) {
56 fd06c375 bellard
        const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
57 fd06c375 bellard
        const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
58 fd06c375 bellard
59 fd06c375 bellard
        /* multiple of wavelength for gapless looping */
60 fd06c375 bellard
        s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1;
61 fd06c375 bellard
        for (i = 0; i < s->samples; ++i)
62 fd06c375 bellard
            s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
63 fd06c375 bellard
    } else {
64 fd06c375 bellard
        s->samples = PCSPK_BUF_LEN;
65 fd06c375 bellard
        for (i = 0; i < PCSPK_BUF_LEN; ++i)
66 fd06c375 bellard
            s->sample_buf[i] = 128; /* silence */
67 fd06c375 bellard
    }
68 fd06c375 bellard
}
69 fd06c375 bellard
70 fd06c375 bellard
static void pcspk_callback(void *opaque, int free)
71 fd06c375 bellard
{
72 fd06c375 bellard
    PCSpkState *s = opaque;
73 fd06c375 bellard
    unsigned int n;
74 fd06c375 bellard
75 fd06c375 bellard
    if (pit_get_mode(s->pit, 2) != 3)
76 fd06c375 bellard
        return;
77 fd06c375 bellard
78 fd06c375 bellard
    n = pit_get_initial_count(s->pit, 2);
79 fd06c375 bellard
    /* avoid frequencies that are not reproducible with sample rate */
80 fd06c375 bellard
    if (n < PCSPK_MIN_COUNT)
81 fd06c375 bellard
        n = 0;
82 fd06c375 bellard
83 fd06c375 bellard
    if (s->pit_count != n) {
84 fd06c375 bellard
        s->pit_count = n;
85 fd06c375 bellard
        s->play_pos = 0;
86 fd06c375 bellard
        generate_samples(s);
87 fd06c375 bellard
    }
88 fd06c375 bellard
89 fd06c375 bellard
    while (free > 0) {
90 fd06c375 bellard
        n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
91 fd06c375 bellard
        n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
92 fd06c375 bellard
        if (!n)
93 fd06c375 bellard
            break;
94 fd06c375 bellard
        s->play_pos = (s->play_pos + n) % s->samples;
95 fd06c375 bellard
        free -= n;
96 fd06c375 bellard
    }
97 fd06c375 bellard
}
98 fd06c375 bellard
99 22d83b14 Paul Brook
int pcspk_audio_init(qemu_irq *pic)
100 fd06c375 bellard
{
101 fd06c375 bellard
    PCSpkState *s = &pcspk_state;
102 1ea879e5 malc
    struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
103 fd06c375 bellard
104 1a7dafce malc
    AUD_register_card(s_spk, &s->card);
105 fd06c375 bellard
106 d929eba5 bellard
    s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
107 fd06c375 bellard
    if (!s->voice) {
108 fd06c375 bellard
        AUD_log(s_spk, "Could not open voice\n");
109 fd06c375 bellard
        return -1;
110 fd06c375 bellard
    }
111 fd06c375 bellard
112 fd06c375 bellard
    return 0;
113 fd06c375 bellard
}
114 fd06c375 bellard
115 fd06c375 bellard
static uint32_t pcspk_ioport_read(void *opaque, uint32_t addr)
116 fd06c375 bellard
{
117 fd06c375 bellard
    PCSpkState *s = opaque;
118 fd06c375 bellard
    int out;
119 fd06c375 bellard
120 fd06c375 bellard
    s->dummy_refresh_clock ^= (1 << 4);
121 fd06c375 bellard
    out = pit_get_out(s->pit, 2, qemu_get_clock(vm_clock)) << 5;
122 fd06c375 bellard
123 fd06c375 bellard
    return pit_get_gate(s->pit, 2) | (s->data_on << 1) | s->dummy_refresh_clock | out;
124 fd06c375 bellard
}
125 fd06c375 bellard
126 fd06c375 bellard
static void pcspk_ioport_write(void *opaque, uint32_t addr, uint32_t val)
127 fd06c375 bellard
{
128 fd06c375 bellard
    PCSpkState *s = opaque;
129 fd06c375 bellard
    const int gate = val & 1;
130 fd06c375 bellard
131 fd06c375 bellard
    s->data_on = (val >> 1) & 1;
132 fd06c375 bellard
    pit_set_gate(s->pit, 2, gate);
133 fd06c375 bellard
    if (s->voice) {
134 fd06c375 bellard
        if (gate) /* restart */
135 fd06c375 bellard
            s->play_pos = 0;
136 fd06c375 bellard
        AUD_set_active_out(s->voice, gate & s->data_on);
137 fd06c375 bellard
    }
138 fd06c375 bellard
}
139 fd06c375 bellard
140 fd06c375 bellard
void pcspk_init(PITState *pit)
141 fd06c375 bellard
{
142 fd06c375 bellard
    PCSpkState *s = &pcspk_state;
143 fd06c375 bellard
144 fd06c375 bellard
    s->pit = pit;
145 fd06c375 bellard
    register_ioport_read(0x61, 1, 1, pcspk_ioport_read, s);
146 fd06c375 bellard
    register_ioport_write(0x61, 1, 1, pcspk_ioport_write, s);
147 fd06c375 bellard
}