jcs's openbsd hax
openbsd

pcppi(4), spkr(4): ticks -> milliseconds

In pcppi(4), convert from ticks to milliseconds.

While we're doing this, we can also convert spkr(4), too.

The spkr(4) conversion from ticks to milliseconds is trickier because
the driver is written with musical intervals (whole notes, quarter
notes, etc.), not the typical units (seconds, milliseconds, etc.) used
in most software.

I think the conversion is correct... but the code is a challenging
read, so it might be subtly incorrect.

ratchov@ intends to move spkr(4) into the attic sometime soon so I
doubt it matters much if I got it wrong.

Input from schwarze@, jsg@, and ratchov@.

Tested by schwarze@.

ok ratchov@

cheloha aff96eed 2bd3c15d

+27 -27
+12 -12
sys/dev/isa/pcppi.c
··· 1 - /* $OpenBSD: pcppi.c,v 1.15 2019/12/31 10:05:32 mpi Exp $ */ 1 + /* $OpenBSD: pcppi.c,v 1.16 2020/04/06 17:54:50 cheloha Exp $ */ 2 2 /* $NetBSD: pcppi.c,v 1.1 1998/04/15 20:26:18 drochner Exp $ */ 3 3 4 4 /* ··· 184 184 } 185 185 186 186 void 187 - pcppi_bell(self, pitch, period, slp) 187 + pcppi_bell(self, pitch, period_ms, slp) 188 188 pcppi_tag_t self; 189 - int pitch, period; 189 + int pitch, period_ms; 190 190 int slp; 191 191 { 192 192 struct pcppi_softc *sc = self; ··· 197 197 else if (pitch > INT_MAX - TIMER_FREQ) 198 198 pitch = INT_MAX - TIMER_FREQ; 199 199 200 - if (period < 0) 201 - period = 0; 202 - else if (period > INT_MAX / 1000000) 203 - period = INT_MAX / 1000000; 200 + if (period_ms < 0) 201 + period_ms = 0; 202 + else if (period_ms > INT_MAX / 1000) 203 + period_ms = INT_MAX / 1000; 204 204 205 205 s1 = spltty(); /* ??? */ 206 206 if (sc->sc_bellactive) { ··· 211 211 if (sc->sc_slp) 212 212 wakeup(pcppi_bell_stop); 213 213 } 214 - if (pitch == 0 || period == 0) { 214 + if (pitch == 0 || period_ms == 0) { 215 215 pcppi_bell_stop(sc); 216 216 sc->sc_bellpitch = 0; 217 217 splx(s1); ··· 236 236 sc->sc_bellactive = 1; 237 237 238 238 if (slp & PCPPI_BELL_POLL) { 239 - delay((period * 1000000) / hz); 239 + delay(period_ms * 1000); 240 240 pcppi_bell_stop(sc); 241 241 } else { 242 242 sc->sc_timeout = 1; 243 - timeout_add(&sc->sc_bell_timeout, period); 243 + timeout_add_msec(&sc->sc_bell_timeout, period_ms); 244 244 if (slp & PCPPI_BELL_SLEEP) { 245 245 sc->sc_slp = 1; 246 246 tsleep_nsec(pcppi_bell_stop, PCPPIPRI | PCATCH, "bell", ··· 279 279 int poll; 280 280 { 281 281 /* 282 - * Comes in as ms, goes out as ticks; volume ignored. 282 + * NB: volume ignored. 283 283 */ 284 - pcppi_bell(arg, volume ? pitch : 0, (period * hz) / 1000, 284 + pcppi_bell(arg, volume ? pitch : 0, period, 285 285 poll ? PCPPI_BELL_POLL : 0); 286 286 } 287 287 #endif /* NPCKBD > 0 || NHIDKBD > 0 */
+15 -15
sys/dev/isa/spkr.c
··· 1 - /* $OpenBSD: spkr.c,v 1.24 2020/03/16 04:15:19 cheloha Exp $ */ 1 + /* $OpenBSD: spkr.c,v 1.25 2020/04/06 17:54:50 cheloha Exp $ */ 2 2 /* $NetBSD: spkr.c,v 1.1 1998/04/15 20:26:18 drochner Exp $ */ 3 3 4 4 /* ··· 81 81 static void playtone(int, int, int); 82 82 static void playstring(char *, size_t); 83 83 84 - /* emit tone of frequency hz for given number of ticks */ 84 + /* emit tone of frequency freq for given number of milliseconds */ 85 85 static void 86 - tone(hz, nticks) 87 - u_int hz, nticks; 86 + tone(freq, ms) 87 + u_int freq, ms; 88 88 { 89 - pcppi_bell(ppicookie, hz, nticks, PCPPI_BELL_SLEEP); 89 + pcppi_bell(ppicookie, freq, ms, PCPPI_BELL_SLEEP); 90 90 } 91 91 92 - /* rest for given number of ticks */ 92 + /* rest for given number of milliseconds */ 93 93 static void 94 - rest(nticks) 95 - int nticks; 94 + rest(ms) 95 + int ms; 96 96 { 97 97 /* 98 98 * Set timeout to endrest function, then give up the timeslice. ··· 100 100 * waited out. 101 101 */ 102 102 #ifdef SPKRDEBUG 103 - printf("rest: %d\n", nticks); 103 + printf("rest: %dms\n", ms); 104 104 #endif /* SPKRDEBUG */ 105 - if (nticks > 0) 106 - tsleep(rest, SPKRPRI | PCATCH, "rest", nticks); 105 + if (ms > 0) 106 + tsleep_nsec(rest, SPKRPRI | PCATCH, "rest", MSEC_TO_NSEC(ms)); 107 107 } 108 108 109 109 /**************** PLAY STRING INTERPRETER BEGINS HERE ********************** ··· 119 119 #define dtoi(c) ((c) - '0') 120 120 121 121 static int octave; /* currently selected octave */ 122 - static int whole; /* whole-note time at current tempo, in ticks */ 122 + static int whole; /* whole-note time at current tempo, in milliseconds */ 123 123 static int value; /* whole divisor for note time, quarter note = 1 */ 124 124 static int fill; /* controls spacing of notes */ 125 125 static int octtrack; /* octave-tracking on? */ ··· 169 169 playinit(void) 170 170 { 171 171 octave = DFLT_OCTAVE; 172 - whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; 172 + whole = (1000 * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; 173 173 fill = NORMAL; 174 174 value = DFLT_VALUE; 175 175 octtrack = 0; ··· 198 198 (FILLTIME * value * sdenom); 199 199 200 200 #ifdef SPKRDEBUG 201 - printf("playtone: pitch %d for %d ticks, rest for %d ticks\n", 201 + printf("playtone: pitch %d for %dms, rest for %dms\n", 202 202 pitch, sound, silence); 203 203 #endif /* SPKRDEBUG */ 204 204 ··· 353 353 GETNUM(cp, tempo); 354 354 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) 355 355 tempo = DFLT_TEMPO; 356 - whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo; 356 + whole = (1000 * SECS_PER_MIN * WHOLE_NOTE) / tempo; 357 357 break; 358 358 359 359 case 'M':