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
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(msacm);
30 /***********************************************************************
33 static DWORD PCM_drvOpen(LPCSTR str, PACMDRVOPENDESCW adod)
36 adod->fccType == ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC &&
37 adod->fccComp == ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
40 /***********************************************************************
43 static DWORD PCM_drvClose(DWORD dwDevID)
48 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
49 #define NUM_OF(a,b) (((a)+(b)-1)/(b))
51 /* flags for fdwDriver */
52 #define PCM_RESAMPLE 1
54 /* data used while converting */
55 typedef struct tagAcmPcmData {
56 /* conversion routine, depending if rate conversion is required */
58 void (*cvtKeepRate)(const unsigned char*, int, unsigned char*);
59 void (*cvtChangeRate)(struct tagAcmPcmData*, const unsigned char*,
60 LPDWORD, unsigned char*, LPDWORD);
62 /* the following fields are used only with rate conversion) */
63 DWORD srcPos; /* position in source stream */
64 double dstPos; /* position in destination stream */
65 double dstIncr; /* value to increment dst stream when src stream
66 is incremented by 1 */
67 /* last source stream value read */
69 unsigned char b; /* 8 bit value */
70 short s; /* 16 bit value */
71 } last[2]; /* two channels max (stereo) */
74 /* table to list all supported formats... those are the basic ones. this
75 * also helps given a unique index to each of the supported formats
82 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000},
83 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025},
84 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050},
85 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100},
88 /***********************************************************************
91 static DWORD PCM_GetFormatIndex(LPWAVEFORMATEX wfx)
95 for (i = 0; i < NUM_PCM_FORMATS; i++) {
96 if (wfx->nChannels == PCM_Formats[i].nChannels &&
97 wfx->nSamplesPerSec == PCM_Formats[i].rate &&
98 wfx->wBitsPerSample == PCM_Formats[i].nBits)
107 * + 8 bit unsigned vs 16 bit signed
108 * + mono vs stereo (1 or 2 channels)
109 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo shall work
112 * mono => stereo: copy the same sample on Left & Right channels
113 * stereo =) mono: use the average value of samples from Left & Right channels
114 * resampling; we lookup for each destination sample the two source adjacent samples
115 * were src <= dst < src+1 (dst is increased by a fractional value which is
116 * equivalent to the increment by one on src); then we use a linear
117 * interpolation between src and src+1
120 /***********************************************************************
123 * Converts a 8 bit sample to a 16 bit one
125 static inline short C816(unsigned char b)
127 return (short)(b ^ 0x80) * 256;
130 /***********************************************************************
133 * Converts a 16 bit sample to a 8 bit one (data loss !!)
135 static inline unsigned char C168(short s)
137 return HIBYTE(s) ^ (unsigned char)0x80;
140 /***********************************************************************
143 * Read a 16 bit sample (correctly handles endianess)
145 static inline short R16(const unsigned char* src)
147 return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
150 /***********************************************************************
153 * Write a 16 bit sample (correctly handles endianess)
155 static inline void W16(unsigned char* dst, short s)
161 /***********************************************************************
164 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
165 * (takes the mid-point of the two values)
167 static inline short M16(short l, short r)
172 /***********************************************************************
175 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
176 * (takes the mid-point of the two values)
178 static inline unsigned char M8(unsigned char a, unsigned char b)
180 return (unsigned char)((a + b) / 2);
183 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
185 * <X> is the (M)ono/(S)tereo configuration of input channel
186 * <Y> is the (M)ono/(S)tereo configuration of output channel
187 * <N> is the number of bits of input channel (8 or 16)
188 * <M> is the number of bits of output channel (8 or 16)
190 * in the parameters, ns is always the number of samples, so the size of input
191 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
194 static void cvtMM88K(const unsigned char* src, int ns, unsigned char* dst)
196 memcpy(dst, src, ns);
199 static void cvtSS88K(const unsigned char* src, int ns, unsigned char* dst)
201 memcpy(dst, src, ns * 2);
204 static void cvtMM1616K(const unsigned char* src, int ns, unsigned char* dst)
206 memcpy(dst, src, ns * 2);
209 static void cvtSS1616K(const unsigned char* src, int ns, unsigned char* dst)
211 memcpy(dst, src, ns * 4);
214 static void cvtMS88K(const unsigned char* src, int ns, unsigned char* dst)
222 static void cvtMS816K(const unsigned char* src, int ns, unsigned char* dst)
228 W16(dst, v); dst += 2;
229 W16(dst, v); dst += 2;
233 static void cvtMS168K(const unsigned char* src, int ns, unsigned char* dst)
238 v = C168(R16(src)); src += 2;
244 static void cvtMS1616K(const unsigned char* src, int ns, unsigned char* dst)
249 v = R16(src); src += 2;
250 W16(dst, v); dst += 2;
251 W16(dst, v); dst += 2;
255 static void cvtSM88K(const unsigned char* src, int ns, unsigned char* dst)
258 *dst++ = M8(src[0], src[1]);
263 static void cvtSM816K(const unsigned char* src, int ns, unsigned char* dst)
268 v = M16(C816(src[0]), C816(src[1]));
270 W16(dst, v); dst += 2;
274 static void cvtSM168K(const unsigned char* src, int ns, unsigned char* dst)
277 *dst++ = C168(M16(R16(src), R16(src + 2)));
282 static void cvtSM1616K(const unsigned char* src, int ns, unsigned char* dst)
285 W16(dst, M16(R16(src),R16(src+2))); dst += 2;
290 static void cvtMM816K(const unsigned char* src, int ns, unsigned char* dst)
293 W16(dst, C816(*src++)); dst += 2;
297 static void cvtSS816K(const unsigned char* src, int ns, unsigned char* dst)
300 W16(dst, C816(*src++)); dst += 2;
301 W16(dst, C816(*src++)); dst += 2;
305 static void cvtMM168K(const unsigned char* src, int ns, unsigned char* dst)
308 *dst++ = C168(R16(src)); src += 2;
312 static void cvtSS168K(const unsigned char* src, int ns, unsigned char* dst)
315 *dst++ = C168(R16(src)); src += 2;
316 *dst++ = C168(R16(src)); src += 2;
320 static void (*PCM_ConvertKeepRate[16])(const unsigned char*, int, unsigned char*) = {
321 cvtSS88K, cvtSM88K, cvtMS88K, cvtMM88K,
322 cvtSS816K, cvtSM816K, cvtMS816K, cvtMM816K,
323 cvtSS168K, cvtSM168K, cvtMS168K, cvtMM168K,
324 cvtSS1616K, cvtSM1616K, cvtMS1616K, cvtMM1616K,
327 /***********************************************************************
330 * Interpolate the value at r (r in ]0, 1]) between the two points v1 and v2
331 * Linear interpolation is used
333 static inline double I(double v1, double v2, double r)
335 if (0.0 >= r || r > 1.0) FIXME("r!! %f\n", r);
336 return (1.0 - r) * v1 + r * v2;
339 static void cvtSS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
340 unsigned char* dst, LPDWORD ndst)
344 while (*nsrc != 0 && *ndst != 0) {
345 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
346 if (*nsrc == 0) return;
347 apd->last[0].b = *src++;
348 apd->last[1].b = *src++;
352 /* now do the interpolation */
353 *dst++ = I(apd->last[0].b, src[0], r);
354 *dst++ = I(apd->last[1].b, src[1], r);
355 apd->dstPos += apd->dstIncr;
360 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
362 * <X> is the (M)ono/(S)tereo configuration of input channel
363 * <Y> is the (M)ono/(S)tereo configuration of output channel
364 * <N> is the number of bits of input channel (8 or 16)
365 * <M> is the number of bits of output channel (8 or 16)
368 static void cvtSM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
369 unsigned char* dst, LPDWORD ndst)
373 while (*nsrc != 0 && *ndst != 0) {
374 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
375 if (*nsrc == 0) return;
376 apd->last[0].b = *src++;
377 apd->last[1].b = *src++;
381 /* now do the interpolation */
382 *dst++ = I(M8(apd->last[0].b, apd->last[1].b), M8(src[0], src[1]), r);
383 apd->dstPos += apd->dstIncr;
388 static void cvtMS88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
389 unsigned char* dst, LPDWORD ndst)
393 while (*nsrc != 0 && *ndst != 0) {
394 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
395 if (*nsrc == 0) return;
396 apd->last[0].b = *src++;
400 /* now do the interpolation */
401 dst[0] = dst[1] = I(apd->last[0].b, src[0], r);
403 apd->dstPos += apd->dstIncr;
408 static void cvtMM88C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
409 unsigned char* dst, LPDWORD ndst)
413 while (*nsrc != 0 && *ndst != 0) {
414 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
415 if (*nsrc == 0) return;
416 apd->last[0].b = *src++;
420 /* now do the interpolation */
421 *dst++ = I(apd->last[0].b, src[0], r);
422 apd->dstPos += apd->dstIncr;
427 static void cvtSS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
428 unsigned char* dst, LPDWORD ndst)
432 while (*nsrc != 0 && *ndst != 0) {
433 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
434 if (*nsrc == 0) return;
435 apd->last[0].b = *src++;
436 apd->last[1].b = *src++;
440 /* now do the interpolation */
441 W16(dst, I(C816(apd->last[0].b), C816(src[0]), r)); dst += 2;
442 W16(dst, I(C816(apd->last[1].b), C816(src[1]), r)); dst += 2;
443 apd->dstPos += apd->dstIncr;
448 static void cvtSM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
449 unsigned char* dst, LPDWORD ndst)
453 while (*nsrc != 0 && *ndst != 0) {
454 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
455 if (*nsrc == 0) return;
456 apd->last[0].b = *src++;
457 apd->last[1].b = *src++;
461 /* now do the interpolation */
462 W16(dst, I(M16(C816(apd->last[0].b), C816(apd->last[1].b)),
463 M16(C816(src[0]), C816(src[1])), r));
465 apd->dstPos += apd->dstIncr;
470 static void cvtMS816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
471 unsigned char* dst, LPDWORD ndst)
476 while (*nsrc != 0 && *ndst != 0) {
477 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
478 if (*nsrc == 0) return;
479 apd->last[0].b = *src++;
483 /* now do the interpolation */
484 v = I(C816(apd->last[0].b), C816(src[0]), r);
485 W16(dst, v); dst += 2;
486 W16(dst, v); dst += 2;
487 apd->dstPos += apd->dstIncr;
492 static void cvtMM816C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
493 unsigned char* dst, LPDWORD ndst)
497 while (*nsrc != 0 && *ndst != 0) {
498 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
499 if (*nsrc == 0) return;
500 apd->last[0].b = *src++;
504 /* now do the interpolation */
505 W16(dst, I(C816(apd->last[0].b), C816(src[0]), r));
507 apd->dstPos += apd->dstIncr;
512 static void cvtSS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
513 unsigned char* dst, LPDWORD ndst)
517 while (*nsrc != 0 && *ndst != 0) {
518 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
519 if (*nsrc == 0) return;
520 apd->last[0].s = R16(src); src += 2;
521 apd->last[1].s = R16(src); src += 2;
525 /* now do the interpolation */
526 *dst++ = C168(I(apd->last[0].s, R16(src) , r));
527 *dst++ = C168(I(apd->last[1].s, R16(src+2), r));
528 apd->dstPos += apd->dstIncr;
533 static void cvtSM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
534 unsigned char* dst, LPDWORD ndst)
538 while (*nsrc != 0 && *ndst != 0) {
539 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
540 if (*nsrc == 0) return;
541 apd->last[0].s = R16(src); src += 2;
542 apd->last[1].s = R16(src); src += 2;
546 /* now do the interpolation */
547 *dst++ = C168(I(M16(apd->last[0].s, apd->last[1].s),
548 M16(R16(src), R16(src + 2)), r));
549 apd->dstPos += apd->dstIncr;
555 static void cvtMS168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
556 unsigned char* dst, LPDWORD ndst)
560 while (*nsrc != 0 && *ndst != 0) {
561 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
562 if (*nsrc == 0) return;
563 apd->last[0].s = R16(src); src += 2;
567 /* now do the interpolation */
568 dst[0] = dst[1] = C168(I(apd->last[0].s, R16(src), r)); dst += 2;
569 apd->dstPos += apd->dstIncr;
575 static void cvtMM168C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
576 unsigned char* dst, LPDWORD ndst)
580 while (*nsrc != 0 && *ndst != 0) {
581 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
582 if (*nsrc == 0) return;
583 apd->last[0].s = R16(src); src += 2;
587 /* now do the interpolation */
588 *dst++ = C168(I(apd->last[0].s, R16(src), r));
589 apd->dstPos += apd->dstIncr;
594 static void cvtSS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
595 unsigned char* dst, LPDWORD ndst)
599 while (*nsrc != 0 && *ndst != 0) {
600 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
601 if (*nsrc == 0) return;
602 apd->last[0].s = R16(src); src += 2;
603 apd->last[1].s = R16(src); src += 2;
607 /* now do the interpolation */
608 W16(dst, I(apd->last[0].s, R16(src) , r)); dst += 2;
609 W16(dst, I(apd->last[1].s, R16(src+2), r)); dst += 2;
610 apd->dstPos += apd->dstIncr;
615 static void cvtSM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
616 unsigned char* dst, LPDWORD ndst)
620 while (*nsrc != 0 && *ndst != 0) {
621 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
622 if (*nsrc == 0) return;
623 apd->last[0].s = R16(src); src += 2;
624 apd->last[1].s = R16(src); src += 2;
628 /* now do the interpolation */
629 W16(dst, I(M16(apd->last[0].s, apd->last[1].s),
630 M16(R16(src), R16(src+2)), r));
632 apd->dstPos += apd->dstIncr;
637 static void cvtMS1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
638 unsigned char* dst, LPDWORD ndst)
643 while (*nsrc != 0 && *ndst != 0) {
644 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
645 if (*nsrc == 0) return;
646 apd->last[0].s = R16(src); src += 2;
650 /* now do the interpolation */
651 v = I(apd->last[0].s, R16(src), r);
652 W16(dst, v); dst += 2;
653 W16(dst, v); dst += 2;
654 apd->dstPos += apd->dstIncr;
659 static void cvtMM1616C(AcmPcmData* apd, const unsigned char* src, LPDWORD nsrc,
660 unsigned char* dst, LPDWORD ndst)
664 while (*nsrc != 0 && *ndst != 0) {
665 while ((r = (double)apd->srcPos - apd->dstPos) <= 0) {
666 if (*nsrc == 0) return;
667 apd->last[0].s = R16(src); src += 2;
671 /* now do the interpolation */
672 W16(dst, I(apd->last[0].s, R16(src), r)); dst += 2;
673 apd->dstPos += apd->dstIncr;
678 static void (*PCM_ConvertChangeRate[16])(AcmPcmData* apd,
679 const unsigned char* src, LPDWORD nsrc,
680 unsigned char* dst, LPDWORD ndst) = {
681 cvtSS88C, cvtSM88C, cvtMS88C, cvtMM88C,
682 cvtSS816C, cvtSM816C, cvtMS816C, cvtMM816C,
683 cvtSS168C, cvtSM168C, cvtMS168C, cvtMM168C,
684 cvtSS1616C, cvtSM1616C, cvtMS1616C, cvtMM1616C,
687 /***********************************************************************
691 static LRESULT PCM_DriverDetails(PACMDRIVERDETAILSW add)
693 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
694 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
697 add->vdwACM = 0x01000000;
698 add->vdwDriver = 0x01000000;
699 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
700 add->cFormatTags = 1;
701 add->cFilterTags = 0;
702 add->hicon = (HICON)0;
703 MultiByteToWideChar( CP_ACP, 0, "WINE-PCM", -1,
704 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
705 MultiByteToWideChar( CP_ACP, 0, "Wine PCM converter", -1,
706 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
707 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
708 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
709 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
710 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
711 add->szFeatures[0] = 0;
713 return MMSYSERR_NOERROR;
716 /***********************************************************************
717 * PCM_FormatTagDetails
720 static LRESULT PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
723 case ACM_FORMATTAGDETAILSF_INDEX:
724 if (aftd->dwFormatTagIndex != 0) return ACMERR_NOTPOSSIBLE;
726 case ACM_FORMATTAGDETAILSF_FORMATTAG:
727 if (aftd->dwFormatTag != WAVE_FORMAT_PCM) return ACMERR_NOTPOSSIBLE;
729 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
730 if (aftd->dwFormatTag != WAVE_FORMAT_UNKNOWN &&
731 aftd->dwFormatTag != WAVE_FORMAT_PCM)
732 return ACMERR_NOTPOSSIBLE;
735 WARN("Unsupported query %08lx\n", dwQuery);
736 return MMSYSERR_NOTSUPPORTED;
739 aftd->dwFormatTagIndex = 0;
740 aftd->dwFormatTag = WAVE_FORMAT_PCM;
741 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
742 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
743 aftd->cStandardFormats = NUM_PCM_FORMATS;
744 aftd->szFormatTag[0] = 0;
746 return MMSYSERR_NOERROR;
749 /***********************************************************************
753 static LRESULT PCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
756 case ACM_FORMATDETAILSF_FORMAT:
757 if (PCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
759 case ACM_FORMATDETAILSF_INDEX:
760 assert(afd->dwFormatIndex < NUM_PCM_FORMATS);
761 afd->pwfx->wFormatTag = WAVE_FORMAT_PCM;
762 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
763 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
764 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
765 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
766 * afd->pwfx->cbSize = 0;
768 afd->pwfx->nBlockAlign =
769 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
770 afd->pwfx->nAvgBytesPerSec =
771 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
774 WARN("Unsupported query %08lx\n", dwQuery);
775 return MMSYSERR_NOTSUPPORTED;
778 afd->dwFormatTag = WAVE_FORMAT_PCM;
779 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CONVERTER;
780 afd->szFormat[0] = 0; /* let MSACM format this for us... */
782 return MMSYSERR_NOERROR;
785 /***********************************************************************
789 static LRESULT PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
791 FIXME("(%p);\n", adfs);
792 return MMSYSERR_NOTSUPPORTED;
795 /***********************************************************************
799 static void PCM_Reset(AcmPcmData* apd, int srcNumBits)
803 /* initialize with neutral value */
804 if (srcNumBits == 16) {
808 apd->last[0].b = (BYTE)0x80;
809 apd->last[1].b = (BYTE)0x80;
813 /***********************************************************************
817 static LRESULT PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
822 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
824 if (PCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
825 PCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
826 return ACMERR_NOTPOSSIBLE;
828 apd = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData));
829 if (apd == 0) return MMSYSERR_NOMEM;
831 adsi->dwDriver = (DWORD)apd;
834 if (adsi->pwfxSrc->wBitsPerSample == 16) idx += 8;
835 if (adsi->pwfxDst->wBitsPerSample == 16) idx += 4;
836 if (adsi->pwfxSrc->nChannels == 1) idx += 2;
837 if (adsi->pwfxDst->nChannels == 1) idx += 1;
839 if (adsi->pwfxSrc->nSamplesPerSec == adsi->pwfxDst->nSamplesPerSec) {
840 apd->cvt.cvtKeepRate = PCM_ConvertKeepRate[idx];
842 adsi->fdwDriver |= PCM_RESAMPLE;
843 apd->dstIncr = (double)(adsi->pwfxSrc->nSamplesPerSec) /
844 (double)(adsi->pwfxDst->nSamplesPerSec);
845 PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
846 apd->cvt.cvtChangeRate = PCM_ConvertChangeRate[idx];
849 return MMSYSERR_NOERROR;
852 /***********************************************************************
856 static LRESULT PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
858 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
859 return MMSYSERR_NOERROR;
862 /***********************************************************************
866 static inline DWORD PCM_round(DWORD a, DWORD b, DWORD c)
869 /* to be sure, always return an entire number of c... */
870 return ((double)a * (double)b + (double)c - 1) / (double)c;
873 /***********************************************************************
877 static LRESULT PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
879 DWORD srcMask = ~(adsi->pwfxSrc->nBlockAlign - 1);
880 DWORD dstMask = ~(adsi->pwfxDst->nBlockAlign - 1);
882 switch (adss->fdwSize) {
883 case ACM_STREAMSIZEF_DESTINATION:
884 /* cbDstLength => cbSrcLength */
885 adss->cbSrcLength = PCM_round(adss->cbDstLength & dstMask,
886 adsi->pwfxSrc->nAvgBytesPerSec,
887 adsi->pwfxDst->nAvgBytesPerSec) & srcMask;
889 case ACM_STREAMSIZEF_SOURCE:
890 /* cbSrcLength => cbDstLength */
891 adss->cbDstLength = PCM_round(adss->cbSrcLength & srcMask,
892 adsi->pwfxDst->nAvgBytesPerSec,
893 adsi->pwfxSrc->nAvgBytesPerSec) & dstMask;
896 WARN("Unsupported query %08lx\n", adss->fdwSize);
897 return MMSYSERR_NOTSUPPORTED;
899 return MMSYSERR_NOERROR;
902 /***********************************************************************
906 static LRESULT PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
908 AcmPcmData* apd = (AcmPcmData*)adsi->dwDriver;
909 DWORD nsrc = NUM_OF(adsh->cbSrcLength, adsi->pwfxSrc->nBlockAlign);
910 DWORD ndst = NUM_OF(adsh->cbDstLength, adsi->pwfxDst->nBlockAlign);
912 if (adsh->fdwConvert &
913 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
914 ACM_STREAMCONVERTF_END|
915 ACM_STREAMCONVERTF_START)) {
916 FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
918 /* ACM_STREAMCONVERTF_BLOCKALIGN
919 * currently all conversions are block aligned, so do nothing for this flag
920 * ACM_STREAMCONVERTF_END
921 * no pending data, so do nothing for this flag
923 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START) &&
924 (adsi->fdwDriver & PCM_RESAMPLE)) {
925 PCM_Reset(apd, adsi->pwfxSrc->wBitsPerSample);
929 if (adsi->fdwDriver & PCM_RESAMPLE) {
933 apd->cvt.cvtChangeRate(apd, adsh->pbSrc, &nsrc2, adsh->pbDst, &ndst2);
937 if (nsrc < ndst) ndst = nsrc; else nsrc = ndst;
939 /* nsrc is now equal to ndst */
940 apd->cvt.cvtKeepRate(adsh->pbSrc, nsrc, adsh->pbDst);
943 adsh->cbSrcLengthUsed = nsrc * adsi->pwfxSrc->nBlockAlign;
944 adsh->cbDstLengthUsed = ndst * adsi->pwfxDst->nBlockAlign;
946 return MMSYSERR_NOERROR;
949 /**************************************************************************
950 * PCM_DriverProc [exported]
952 LRESULT CALLBACK PCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg,
953 LPARAM dwParam1, LPARAM dwParam2)
955 TRACE("(%08lx %08lx %u %08lx %08lx);\n",
956 dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
959 case DRV_LOAD: return 1;
960 case DRV_FREE: return 1;
961 case DRV_OPEN: return PCM_drvOpen((LPSTR)dwParam1, (PACMDRVOPENDESCW)dwParam2);
962 case DRV_CLOSE: return PCM_drvClose(dwDevID);
963 case DRV_ENABLE: return 1;
964 case DRV_DISABLE: return 1;
965 case DRV_QUERYCONFIGURE: return 1;
966 case DRV_CONFIGURE: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK); return 1;
967 case DRV_INSTALL: return DRVCNF_RESTART;
968 case DRV_REMOVE: return DRVCNF_RESTART;
970 case ACMDM_DRIVER_NOTIFY:
971 /* no caching from other ACM drivers is done so far */
972 return MMSYSERR_NOERROR;
974 case ACMDM_DRIVER_DETAILS:
975 return PCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
977 case ACMDM_FORMATTAG_DETAILS:
978 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
980 case ACMDM_FORMAT_DETAILS:
981 return PCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
983 case ACMDM_FORMAT_SUGGEST:
984 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
986 case ACMDM_STREAM_OPEN:
987 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
989 case ACMDM_STREAM_CLOSE:
990 return PCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
992 case ACMDM_STREAM_SIZE:
993 return PCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
995 case ACMDM_STREAM_CONVERT:
996 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
998 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
999 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
1000 /* this converter is not a hardware driver */
1001 case ACMDM_FILTERTAG_DETAILS:
1002 case ACMDM_FILTER_DETAILS:
1003 /* this converter is not a filter */
1004 case ACMDM_STREAM_RESET:
1005 /* only needed for asynchronous driver... we aren't, so just say it */
1006 case ACMDM_STREAM_PREPARE:
1007 case ACMDM_STREAM_UNPREPARE:
1008 /* nothing special to do here... so don't do anything */
1009 return MMSYSERR_NOTSUPPORTED;
1012 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);