1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 2000 Eric Pouech
9 * + most of the computation should be done in fixed point arithmetic
10 * instead of floating point (16 bits for integral part, and 16 bits
11 * for fractional part for example)
12 * + implement PCM_FormatSuggest function
13 * + get rid of hack for PCM_DriverProc (msacm32.dll shouldn't export
14 * a DriverProc, but this would require implementing a generic
15 * embedded driver handling scheme in msacm32.dll which isn't done yet
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(msacm);
29 /***********************************************************************
32 static DWORD PCM_drvOpen(LPCSTR str)
37 /***********************************************************************
40 static DWORD PCM_drvClose(DWORD dwDevID)
45 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
46 #define NUM_OF(a,b) (((a)+(b)-1)/(b))
48 /* flags for fdwDriver */
49 #define PCM_RESAMPLE 1
51 /* data used while converting */
52 typedef struct tagAcmPcmData {
53 /* conversion routine, depending if rate conversion is required */
55 void (*cvtKeepRate)(const unsigned char*, int, unsigned char*);
56 void (*cvtChangeRate)(struct tagAcmPcmData*, const unsigned char*,
57 LPDWORD, unsigned char*, LPDWORD);
59 /* the following fields are used only with rate conversion) */
60 DWORD srcPos; /* position in source stream */
61 double dstPos; /* position in destination stream */
62 double dstIncr; /* value to increment dst stream when src stream
63 is incremented by 1 */
64 /* last source stream value read */
66 unsigned char b; /* 8 bit value */
67 short s; /* 16 bit value */
68 } last[2]; /* two channels max (stereo) */
71 /* table to list all supported formats... those are the basic ones. this
72 * also helps given a unique index to each of the supported formats
79 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000},
80 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025},
81 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050},
82 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100},
85 /***********************************************************************
88 static DWORD PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
92 for (i = 0; i < NUM_PCM_FORMATS; i++) {
93 if (wfx->nChannels == PCM_Formats[i].nChannels &&
94 wfx->nSamplesPerSec == PCM_Formats[i].rate &&
95 wfx->wBitsPerSample == PCM_Formats[i].nBits)
104 * + 8 bit unsigned vs 16 bit signed
105 * + mono vs stereo (1 or 2 channels)
106 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo shall work
109 * mono => stereo: copy the same sample on Left & Right channels
110 * stereo =) mono: use the average value of samples from Left & Right channels
111 * resampling; we lookup for each destination sample the two source adjacent samples
112 * were src <= dst < src+1 (dst is increased by a fractional value which is
113 * equivalent to the increment by one on src); then we use a linear
114 * interpolation between src and src+1
117 /***********************************************************************
120 * Converts a 8 bit sample to a 16 bit one
122 static inline short C816(unsigned char b)
124 return (short)(b ^ 0x80) * 256;
127 /***********************************************************************
130 * Converts a 16 bit sample to a 8 bit one (data loss !!)
132 static inline unsigned char C168(short s)
134 return HIBYTE(s) ^ (unsigned char)0x80;
137 /***********************************************************************
140 * Read a 16 bit sample (correctly handles endianess)
142 static inline short R16(const unsigned char* src)
144 return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
147 /***********************************************************************
150 * Write a 16 bit sample (correctly handles endianess)
152 static inline void W16(unsigned char* dst, short s)
158 /***********************************************************************
161 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
162 * (takes the mid-point of the two values)
164 static inline short M16(short l, short r)
169 /***********************************************************************
172 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
173 * (takes the mid-point of the two values)
175 static inline unsigned char M8(unsigned char a, unsigned char b)
177 return (unsigned char)((a + b) / 2);
180 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
182 * <X> is the (M)ono/(S)tereo configuration of input channel
183 * <Y> is the (M)ono/(S)tereo configuration of output channel
184 * <N> is the number of bits of input channel (8 or 16)
185 * <M> is the number of bits of output channel (8 or 16)
187 * in the parameters, ns is always the number of samples, so the size of input
188 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
191 static void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
193 memcpy(dst, src, ns);
196 static void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
198 memcpy(dst, src, ns * 2);
201 static void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
203 memcpy(dst, src, ns * 2);
206 static void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
208 memcpy(dst, src, ns * 4);
211 static void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
219 static void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
225 W16(dst, v); dst += 2;
226 W16(dst, v); dst += 2;
230 static void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
235 v = C168(R16(src)); src += 2;
241 static void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
246 v = R16(src); src += 2;
247 W16(dst, v); dst += 2;
248 W16(dst, v); dst += 2;
252 static void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
255 *dst++ = M8(src[0], src[1]);
260 static void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
265 v = M16(C816(src[0]), C816(src[1]));
267 W16(dst, v); dst += 2;
271 static void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
274 *dst++ = C168(M16(R16(src), R16(src + 2)));
279 static void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
282 W16(dst, M16(R16(src),R16(src+2))); dst += 2;
287 static void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
290 W16(dst, C816(*src++)); dst += 2;
294 static void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
297 W16(dst, C816(*src++)); dst += 2;
298 W16(dst, C816(*src++)); dst += 2;
302 static void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
305 *dst++ = C168(R16(src)); src += 2;
309 static void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
312 *dst++ = C168(R16(src)); src += 2;
313 *dst++ = C168(R16(src)); src += 2;
317 static void (*PCM_ConvertKeepRate[16])(const unsigned char*, int, unsigned char*) = {
318 cvtSS88K, cvtSM88K, cvtMS88K, cvtMM88K,
319 cvtSS816K, cvtSM816K, cvtMS816K, cvtMM816K,
320 cvtSS168K, cvtSM168K, cvtMS168K, cvtMM168K,
321 cvtSS1616K, cvtSM1616K, cvtMS1616K, cvtMM1616K,
324 /***********************************************************************
327 * Interpolate the value at r (r in ]0, 1]) between the two points v1 and v2
328 * Linear interpolation is used
330 static inline double I(double v1, double v2, double r)
332 if (0.0 >= r || r > 1.0) FIXME("r!! %f\n", r);
333 return (1.0 - r) * v1 + r * v2;
336 static void cvtSS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
337 unsigned char* dst, LPDWORD ndst)
341 while (*nsrc != 0 && *ndst != 0) {
342 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
343 if (*nsrc == 0) return;
344 apd->last[0].b = *src++;
345 apd->last[1].b = *src++;
349 /* now do the interpolation */
350 *dst++ = I(apd->last[0].b, src[0], r);
351 *dst++ = I(apd->last[1].b, src[1], r);
352 apd->dstPos += apd->dstIncr;
357 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
359 * <X> is the (M)ono/(S)tereo configuration of input channel
360 * <Y> is the (M)ono/(S)tereo configuration of output channel
361 * <N> is the number of bits of input channel (8 or 16)
362 * <M> is the number of bits of output channel (8 or 16)
365 static void cvtSM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
366 unsigned char* dst, LPDWORD ndst)
370 while (*nsrc != 0 && *ndst != 0) {
371 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
372 if (*nsrc == 0) return;
373 apd->last[0].b = *src++;
374 apd->last[1].b = *src++;
378 /* now do the interpolation */
379 *dst++ = I(M8(apd->last[0].b, apd->last[1].b), M8(src[0], src[1]), r);
380 apd->dstPos += apd->dstIncr;
385 static void cvtMS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
386 unsigned char* dst, LPDWORD ndst)
390 while (*nsrc != 0 && *ndst != 0) {
391 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
392 if (*nsrc == 0) return;
393 apd->last[0].b = *src++;
397 /* now do the interpolation */
398 dst[0] = dst[1] = I(apd->last[0].b, src[0], r);
400 apd->dstPos += apd->dstIncr;
405 static void cvtMM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
406 unsigned char* dst, LPDWORD ndst)
410 while (*nsrc != 0 && *ndst != 0) {
411 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
412 if (*nsrc == 0) return;
413 apd->last[0].b = *src++;
417 /* now do the interpolation */
418 *dst++ = I(apd->last[0].b, src[0], r);
419 apd->dstPos += apd->dstIncr;
424 static void cvtSS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
425 unsigned char* dst, LPDWORD ndst)
429 while (*nsrc != 0 && *ndst != 0) {
430 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
431 if (*nsrc == 0) return;
432 apd->last[0].b = *src++;
433 apd->last[1].b = *src++;
437 /* now do the interpolation */
438 W16(dst, I(C816(apd->last[0].b), C816(src[0]), r)); dst += 2;
439 W16(dst, I(C816(apd->last[1].b), C816(src[1]), r)); dst += 2;
440 apd->dstPos += apd->dstIncr;
445 static void cvtSM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
446 unsigned char* dst, LPDWORD ndst)
450 while (*nsrc != 0 && *ndst != 0) {
451 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
452 if (*nsrc == 0) return;
453 apd->last[0].b = *src++;
454 apd->last[1].b = *src++;
458 /* now do the interpolation */
459 W16(dst, I(M16(C816(apd->last[0].b), C816(apd->last[1].b)),
460 M16(C816(src[0]), C816(src[1])), r));
462 apd->dstPos += apd->dstIncr;
467 static void cvtMS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
468 unsigned char* dst, LPDWORD ndst)
473 while (*nsrc != 0 && *ndst != 0) {
474 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
475 if (*nsrc == 0) return;
476 apd->last[0].b = *src++;
480 /* now do the interpolation */
481 v = I(C816(apd->last[0].b), C816(src[0]), r);
482 W16(dst, v); dst += 2;
483 W16(dst, v); dst += 2;
484 apd->dstPos += apd->dstIncr;
489 static void cvtMM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
490 unsigned char* dst, LPDWORD ndst)
494 while (*nsrc != 0 && *ndst != 0) {
495 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
496 if (*nsrc == 0) return;
497 apd->last[0].b = *src++;
501 /* now do the interpolation */
502 W16(dst, I(C816(apd->last[0].b), C816(src[0]), r));
504 apd->dstPos += apd->dstIncr;
509 static void cvtSS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
510 unsigned char* dst, LPDWORD ndst)
514 while (*nsrc != 0 && *ndst != 0) {
515 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
516 if (*nsrc == 0) return;
517 apd->last[0].s = R16(src); src += 2;
518 apd->last[1].s = R16(src); src += 2;
522 /* now do the interpolation */
523 *dst++ = C168(I(apd->last[0].s, R16(src) , r));
524 *dst++ = C168(I(apd->last[1].s, R16(src+2), r));
525 apd->dstPos += apd->dstIncr;
530 static void cvtSM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
531 unsigned char* dst, LPDWORD ndst)
535 while (*nsrc != 0 && *ndst != 0) {
536 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
537 if (*nsrc == 0) return;
538 apd->last[0].s = R16(src); src += 2;
539 apd->last[1].s = R16(src); src += 2;
543 /* now do the interpolation */
544 *dst++ = C168(I(M16(apd->last[0].s, apd->last[1].s),
545 M16(R16(src), R16(src + 2)), r));
546 apd->dstPos += apd->dstIncr;
552 static void cvtMS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
553 unsigned char* dst, LPDWORD ndst)
557 while (*nsrc != 0 && *ndst != 0) {
558 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
559 if (*nsrc == 0) return;
560 apd->last[0].s = R16(src); src += 2;
564 /* now do the interpolation */
565 dst[0] = dst[1] = C168(I(apd->last[0].s, R16(src), r)); dst += 2;
566 apd->dstPos += apd->dstIncr;
572 static void cvtMM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
573 unsigned char* dst, LPDWORD ndst)
577 while (*nsrc != 0 && *ndst != 0) {
578 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
579 if (*nsrc == 0) return;
580 apd->last[0].s = R16(src); src += 2;
584 /* now do the interpolation */
585 *dst++ = C168(I(apd->last[0].s, R16(src), r));
586 apd->dstPos += apd->dstIncr;
591 static void cvtSS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
592 unsigned char* dst, LPDWORD ndst)
596 while (*nsrc != 0 && *ndst != 0) {
597 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
598 if (*nsrc == 0) return;
599 apd->last[0].s = R16(src); src += 2;
600 apd->last[1].s = R16(src); src += 2;
604 /* now do the interpolation */
605 W16(dst, I(apd->last[0].s, R16(src) , r)); dst += 2;
606 W16(dst, I(apd->last[1].s, R16(src+2), r)); dst += 2;
607 apd->dstPos += apd->dstIncr;
612 static void cvtSM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
613 unsigned char* dst, LPDWORD ndst)
617 while (*nsrc != 0 && *ndst != 0) {
618 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
619 if (*nsrc == 0) return;
620 apd->last[0].s = R16(src); src += 2;
621 apd->last[1].s = R16(src); src += 2;
625 /* now do the interpolation */
626 W16(dst, I(M16(apd->last[0].s, apd->last[1].s),
627 M16(R16(src), R16(src+2)), r));
629 apd->dstPos += apd->dstIncr;
634 static void cvtMS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
635 unsigned char* dst, LPDWORD ndst)
640 while (*nsrc != 0 && *ndst != 0) {
641 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
642 if (*nsrc == 0) return;
643 apd->last[0].s = R16(src); src += 2;
647 /* now do the interpolation */
648 v = I(apd->last[0].s, R16(src), r);
649 W16(dst, v); dst += 2;
650 W16(dst, v); dst += 2;
651 apd->dstPos += apd->dstIncr;
656 static void cvtMM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
657 unsigned char* dst, LPDWORD ndst)
661 while (*nsrc != 0 && *ndst != 0) {
662 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
663 if (*nsrc == 0) return;
664 apd->last[0].s = R16(src); src += 2;
668 /* now do the interpolation */
669 W16(dst, I(apd->last[0].s, R16(src), r)); dst += 2;
670 apd->dstPos += apd->dstIncr;
675 static void (*PCM_ConvertChangeRate[16])(AcmPcmData* apd,
676 const unsigned char* src, LPDWORD nsrc,
677 unsigned char* dst, LPDWORD ndst) = {
678 cvtSS88C, cvtSM88C, cvtMS88C, cvtMM88C,
679 cvtSS816C, cvtSM816C, cvtMS816C, cvtMM816C,
680 cvtSS168C, cvtSM168C, cvtMS168C, cvtMM168C,
681 cvtSS1616C, cvtSM1616C, cvtMS1616C, cvtMM1616C,
684 /***********************************************************************
688 static LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
690 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
691 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
694 add->vdwACM = 0x01000000;
695 add->vdwDriver = 0x01000000;
696 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
697 add->cFormatTags = 1;
698 add->cFilterTags = 0;
699 add->hicon = (HICON)0;
700 MultiByteToWideChar( CP_ACP, 0, "WINE-PCM", -1,
701 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
702 MultiByteToWideChar( CP_ACP, 0, "Wine PCM converter", -1,
703 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
704 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
705 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
706 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
707 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
708 add->szFeatures[0] = 0;
710 return MMSYSERR_NOERROR;
713 /***********************************************************************
714 * PCM_FormatTagDetails
717 static LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
720 case ACM_FORMATTAGDETAILSF_INDEX:
721 if (aftd->dwFormatTagIndex != 0) return ACMERR_NOTPOSSIBLE;
723 case ACM_FORMATTAGDETAILSF_FORMATTAG:
724 if (aftd->dwFormatTag != WAVE_FORMAT_PCM) return ACMERR_NOTPOSSIBLE;
726 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
727 if (aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN &&
728 aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN)
729 return ACMERR_NOTPOSSIBLE;
732 WARN("Unsupported query %08lx\n", dwQuery);
733 return MMSYSERR_NOTSUPPORTED;
736 aftd->dwFormatTagIndex = 0;
737 aftd->dwFormatTag = WAVE_FORMAT_PCM;
738 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
739 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
740 aftd->cStandardFormats = NUM_PCM_FORMATS;
741 aftd->szFormatTag[0] = 0;
743 return MMSYSERR_NOERROR;
746 /***********************************************************************
750 static LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
753 case ACM_FORMATDETAILSF_FORMAT:
754 afd->dwFormatIndex = PCM_GetFormatIndex(afd->pwfx);
755 if (afd->dwFormatIndex == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
757 case ACM_FORMATDETAILSF_INDEX:
758 assert(afd->dwFormatIndex < NUM_PCM_FORMATS);
759 afd->pwfx->wFormatTag = WAVE_FORMAT_PCM;
760 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
761 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
762 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
763 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
764 * afd->pwfx->cbSize = 0;
766 afd->pwfx->nBlockAlign =
767 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
768 afd->pwfx->nAvgBytesPerSec =
769 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
772 WARN("Unsupported query %08lx\n", dwQuery);
773 return MMSYSERR_NOTSUPPORTED;
776 afd->dwFormatTag = WAVE_FORMAT_PCM;
777 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
778 afd->szFormat[0] = 0; /* let MSACM format this for us... */
780 return MMSYSERR_NOERROR;
783 /***********************************************************************
787 static LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
789 FIXME("(%p);\n", adfs);
790 return MMSYSERR_NOTSUPPORTED;
793 /***********************************************************************
797 static void PCM_Reset(AcmPcmData* apd, int srcNumBits)
801 /* initialize with neutral value */
802 if (srcNumBits == 16) {
806 apd->last[0].b = (BYTE)0x80;
807 apd->last[1].b = (BYTE)0x80;
811 /***********************************************************************
815 static LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
820 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
822 if (PCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
823 PCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
824 return ACMERR_NOTPOSSIBLE;
826 apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
827 if (apd == 0) return MMSYSERR_NOMEM;
829 adsi->dwDriver = (DWORD)apd;
832 if (adsi->pwfxSrc->wBitsPerSample == 16) idx += 8;
833 if (adsi->pwfxDst->wBitsPerSample == 16) idx += 4;
834 if (adsi->pwfxSrc->nChannels == 1) idx += 2;
835 if (adsi->pwfxDst->nChannels == 1) idx += 1;
837 if (adsi->pwfxSrc->nSamplesPerSec == adsi->pwfxDst->nSamplesPerSec) {
838 apd->cvt.cvtKeepRate = PCM_ConvertKeepRate[idx];
840 adsi->fdwDriver |= PCM_RESAMPLE;
841 apd->dstIncr = (double)(adsi->pwfxSrc->nSamplesPerSec) /
842 (double)(adsi->pwfxDst->nSamplesPerSec);
843 PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
844 apd->cvt.cvtChangeRate = PCM_ConvertChangeRate[idx];
847 return MMSYSERR_NOERROR;
850 /***********************************************************************
854 static LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
856 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
857 return MMSYSERR_NOERROR;
860 /***********************************************************************
864 static inline DWORD PCM_round(DWORD a, DWORD b, DWORD c)
867 /* to be sure, always return an entire number of c... */
868 return ((double)a * (double)b + (double)c - 1) / (double)c;
871 /***********************************************************************
875 static LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
877 switch (adss->fdwSize) {
878 case ACM_STREAMSIZEF_DESTINATION:
879 /* cbDstLength => cbSrcLength */
880 adss->cbSrcLength = PCM_round(adss->cbDstLength,
881 adsi->pwfxSrc->nAvgBytesPerSec,
882 adsi->pwfxDst->nAvgBytesPerSec);
884 case ACM_STREAMSIZEF_SOURCE:
885 /* cbSrcLength => cbDstLength */
886 adss->cbDstLength = PCM_round(adss->cbSrcLength,
887 adsi->pwfxDst->nAvgBytesPerSec,
888 adsi->pwfxSrc->nAvgBytesPerSec);
891 WARN("Unsupported query %08lx\n", adss->fdwSize);
892 return MMSYSERR_NOTSUPPORTED;
894 return MMSYSERR_NOERROR;
897 /***********************************************************************
901 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
903 AcmPcmData* apd = (AcmPcmData*)adsi->dwDriver;
904 DWORD nsrc = NUM_OF(adsh->cbSrcLength, adsi->pwfxSrc->nBlockAlign);
905 DWORD ndst = NUM_OF(adsh->cbDstLength, adsi->pwfxDst->nBlockAlign);
907 if (adsh->fdwConvert &
908 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
909 ACM_STREAMCONVERTF_END|
910 ACM_STREAMCONVERTF_START)) {
911 FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
913 /* ACM_STREAMCONVERTF_BLOCKALIGN
914 * currently all conversions are block aligned, so do nothing for this flag
915 * ACM_STREAMCONVERTF_END
916 * no pending data, so do nothing for this flag
918 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
919 (adsi->fdwDriver & PCM_RESAMPLE)) {
920 PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
924 if (adsi->fdwDriver & PCM_RESAMPLE) {
928 apd->cvt.cvtChangeRate(apd, adsh->pbSrc, &nsrc2, adsh->pbDst, &ndst2);
932 if (nsrc < ndst) ndst = nsrc; else nsrc = ndst;
934 /* nsrc is now equal to ndst */
935 apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
938 adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
939 adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
941 return MMSYSERR_NOERROR;
944 /**************************************************************************
945 * PCM_DriverProc [exported]
947 LRESULT CALLBACK PCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg,
948 LPARAM dwParam1, LPARAM dwParam2)
950 TRACE("(%08lx %08lx %u %08lx %08lx);\n",
951 dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
954 case DRV_LOAD: return 1;
955 case DRV_FREE: return 1;
956 case DRV_OPEN: return PCM_drvOpen((LPSTR)dwParam1);
957 case DRV_CLOSE: return PCM_drvClose(dwDevID);
958 case DRV_ENABLE: return 1;
959 case DRV_DISABLE: return 1;
960 case DRV_QUERYCONFIGURE: return 1;
961 case DRV_CONFIGURE: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK); return 1;
962 case DRV_INSTALL: return DRVCNF_RESTART;
963 case DRV_REMOVE: return DRVCNF_RESTART;
965 case ACMDM_DRIVER_NOTIFY:
966 /* no caching from other ACM drivers is done so far */
967 return MMSYSERR_NOERROR;
969 case ACMDM_DRIVER_DETAILS:
970 return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
972 case ACMDM_FORMATTAG_DETAILS:
973 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
975 case ACMDM_FORMAT_DETAILS:
976 return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
978 case ACMDM_FORMAT_SUGGEST:
979 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
981 case ACMDM_STREAM_OPEN:
982 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
984 case ACMDM_STREAM_CLOSE:
985 return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
987 case ACMDM_STREAM_SIZE:
988 return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
990 case ACMDM_STREAM_CONVERT:
991 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
993 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
994 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
995 /* this converter is not a hardware driver */
996 case ACMDM_FILTERTAG_DETAILS:
997 case ACMDM_FILTER_DETAILS:
998 /* this converter is not a filter */
999 case ACMDM_STREAM_RESET:
1000 /* only needed for asynchronous driver... we aren't, so just say it */
1001 case ACMDM_STREAM_PREPARE:
1002 case ACMDM_STREAM_UNPREPARE:
1003 /* nothing special to do here... so don't do anything */
1004 return MMSYSERR_NOTSUPPORTED;
1007 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);