AudioMixer.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  1. /*
  2. **
  3. ** Copyright 2007, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #define LOG_TAG "AudioMixer"
  18. #define LOG_NDEBUG 1
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <sys/types.h>
  24. #include "audio/android/audio.h"
  25. #include "audio/android/audio_utils/include/audio_utils/primitives.h"
  26. #include "audio/android/AudioMixerOps.h"
  27. #include "audio/android/AudioMixer.h"
  28. // The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer.
  29. #ifndef FCC_2
  30. #define FCC_2 2
  31. #endif
  32. // Look for MONO_HACK for any Mono hack involving legacy mono channel to
  33. // stereo channel conversion.
  34. /* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is
  35. * being used. This is a considerable amount of log spam, so don't enable unless you
  36. * are verifying the hook based code.
  37. */
  38. //#define VERY_VERY_VERBOSE_LOGGING
  39. #ifdef VERY_VERY_VERBOSE_LOGGING
  40. #define ALOGVV ALOGV
  41. //define ALOGVV printf // for test-mixer.cpp
  42. #else
  43. #define ALOGVV(a...) do { } while (0)
  44. #endif
  45. #ifndef ARRAY_SIZE
  46. #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
  47. #endif
  48. // TODO: Move these macro/inlines to a header file.
  49. template <typename T>
  50. static inline
  51. T max(const T& x, const T& y) {
  52. return x > y ? x : y;
  53. }
  54. // Set kUseNewMixer to true to use the new mixer engine always. Otherwise the
  55. // original code will be used for stereo sinks, the new mixer for multichannel.
  56. static const bool kUseNewMixer = false;
  57. // Set kUseFloat to true to allow floating input into the mixer engine.
  58. // If kUseNewMixer is false, this is ignored or may be overridden internally
  59. // because of downmix/upmix support.
  60. static const bool kUseFloat = false;
  61. // Set to default copy buffer size in frames for input processing.
  62. static const size_t kCopyBufferFrameCount = 256;
  63. namespace cocos2d { namespace experimental {
  64. // ----------------------------------------------------------------------------
  65. template <typename T>
  66. T min(const T& a, const T& b)
  67. {
  68. return a < b ? a : b;
  69. }
  70. // ----------------------------------------------------------------------------
  71. // Ensure mConfiguredNames bitmask is initialized properly on all architectures.
  72. // The value of 1 << x is undefined in C when x >= 32.
  73. AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
  74. : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
  75. mSampleRate(sampleRate)
  76. {
  77. ALOGVV("AudioMixer constructed, frameCount: %d, sampleRate: %d", (int)frameCount, (int)sampleRate);
  78. ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
  79. maxNumTracks, MAX_NUM_TRACKS);
  80. // AudioMixer is not yet capable of more than 32 active track inputs
  81. ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS);
  82. pthread_once(&sOnceControl, &sInitRoutine);
  83. mState.enabledTracks= 0;
  84. mState.needsChanged = 0;
  85. mState.frameCount = frameCount;
  86. mState.hook = process__nop;
  87. mState.outputTemp = NULL;
  88. mState.resampleTemp = NULL;
  89. //cjh mState.mLog = &mDummyLog;
  90. // mState.reserved
  91. // FIXME Most of the following initialization is probably redundant since
  92. // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
  93. // and mTrackNames is initially 0. However, leave it here until that's verified.
  94. track_t* t = mState.tracks;
  95. for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
  96. t->resampler = NULL;
  97. //cjh t->downmixerBufferProvider = NULL;
  98. // t->mReformatBufferProvider = NULL;
  99. // t->mTimestretchBufferProvider = NULL;
  100. t++;
  101. }
  102. }
  103. AudioMixer::~AudioMixer()
  104. {
  105. track_t* t = mState.tracks;
  106. for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
  107. delete t->resampler;
  108. //cjh delete t->downmixerBufferProvider;
  109. // delete t->mReformatBufferProvider;
  110. // delete t->mTimestretchBufferProvider;
  111. t++;
  112. }
  113. delete [] mState.outputTemp;
  114. delete [] mState.resampleTemp;
  115. }
  116. //cjh void AudioMixer::setLog(NBLog::Writer *log)
  117. //{
  118. // mState.mLog = log;
  119. //}
  120. static inline audio_format_t selectMixerInFormat(audio_format_t inputFormat __unused) {
  121. return kUseFloat && kUseNewMixer ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
  122. }
  123. int AudioMixer::getTrackName(audio_channel_mask_t channelMask,
  124. audio_format_t format, int sessionId)
  125. {
  126. if (!isValidPcmTrackFormat(format)) {
  127. ALOGE("AudioMixer::getTrackName invalid format (%#x)", format);
  128. return -1;
  129. }
  130. uint32_t names = (~mTrackNames) & mConfiguredNames;
  131. if (names != 0) {
  132. int n = __builtin_ctz(names);
  133. ALOGV("add track (%d)", n);
  134. // assume default parameters for the track, except where noted below
  135. track_t* t = &mState.tracks[n];
  136. t->needs = 0;
  137. // Integer volume.
  138. // Currently integer volume is kept for the legacy integer mixer.
  139. // Will be removed when the legacy mixer path is removed.
  140. t->volume[0] = UNITY_GAIN_INT;
  141. t->volume[1] = UNITY_GAIN_INT;
  142. t->prevVolume[0] = UNITY_GAIN_INT << 16;
  143. t->prevVolume[1] = UNITY_GAIN_INT << 16;
  144. t->volumeInc[0] = 0;
  145. t->volumeInc[1] = 0;
  146. t->auxLevel = 0;
  147. t->auxInc = 0;
  148. t->prevAuxLevel = 0;
  149. // Floating point volume.
  150. t->mVolume[0] = UNITY_GAIN_FLOAT;
  151. t->mVolume[1] = UNITY_GAIN_FLOAT;
  152. t->mPrevVolume[0] = UNITY_GAIN_FLOAT;
  153. t->mPrevVolume[1] = UNITY_GAIN_FLOAT;
  154. t->mVolumeInc[0] = 0.;
  155. t->mVolumeInc[1] = 0.;
  156. t->mAuxLevel = 0.;
  157. t->mAuxInc = 0.;
  158. t->mPrevAuxLevel = 0.;
  159. // no initialization needed
  160. // t->frameCount
  161. t->channelCount = audio_channel_count_from_out_mask(channelMask);
  162. t->enabled = false;
  163. ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO,
  164. "Non-stereo channel mask: %d\n", channelMask);
  165. t->channelMask = channelMask;
  166. t->sessionId = sessionId;
  167. // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
  168. t->bufferProvider = NULL;
  169. t->buffer.raw = NULL;
  170. // no initialization needed
  171. // t->buffer.frameCount
  172. t->hook = NULL;
  173. t->in = NULL;
  174. t->resampler = NULL;
  175. t->sampleRate = mSampleRate;
  176. // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
  177. t->mainBuffer = NULL;
  178. t->auxBuffer = NULL;
  179. t->mInputBufferProvider = NULL;
  180. //cjh t->mReformatBufferProvider = NULL;
  181. // t->downmixerBufferProvider = NULL;
  182. // t->mPostDownmixReformatBufferProvider = NULL;
  183. // t->mTimestretchBufferProvider = NULL;
  184. t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
  185. t->mFormat = format;
  186. t->mMixerInFormat = selectMixerInFormat(format);
  187. t->mDownmixRequiresFormat = AUDIO_FORMAT_INVALID; // no format required
  188. t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits(
  189. AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO);
  190. t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask);
  191. ALOGVV("t->mMixerChannelCount: %d", t->mMixerChannelCount);
  192. t->mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
  193. // Check the downmixing (or upmixing) requirements.
  194. status_t status = t->prepareForDownmix();
  195. if (status != OK) {
  196. ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask);
  197. return -1;
  198. }
  199. // prepareForDownmix() may change mDownmixRequiresFormat
  200. ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat);
  201. t->prepareForReformat();
  202. mTrackNames |= 1 << n;
  203. ALOGVV("getTrackName return: %d", TRACK0 + n);
  204. return TRACK0 + n;
  205. }
  206. ALOGE("AudioMixer::getTrackName out of available tracks");
  207. return -1;
  208. }
  209. void AudioMixer::invalidateState(uint32_t mask)
  210. {
  211. if (mask != 0) {
  212. mState.needsChanged |= mask;
  213. mState.hook = process__validate;
  214. }
  215. }
  216. // Called when channel masks have changed for a track name
  217. // TODO: Fix DownmixerBufferProvider not to (possibly) change mixer input format,
  218. // which will simplify this logic.
  219. bool AudioMixer::setChannelMasks(int name,
  220. audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) {
  221. track_t &track = mState.tracks[name];
  222. ALOGVV("AudioMixer::setChannelMask ...");
  223. if (trackChannelMask == track.channelMask
  224. && mixerChannelMask == track.mMixerChannelMask) {
  225. ALOGVV("No need to change channel mask ...");
  226. return false; // no need to change
  227. }
  228. // always recompute for both channel masks even if only one has changed.
  229. const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask);
  230. const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask);
  231. const bool mixerChannelCountChanged = track.mMixerChannelCount != mixerChannelCount;
  232. ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX)
  233. && trackChannelCount
  234. && mixerChannelCount);
  235. track.channelMask = trackChannelMask;
  236. track.channelCount = trackChannelCount;
  237. track.mMixerChannelMask = mixerChannelMask;
  238. track.mMixerChannelCount = mixerChannelCount;
  239. // channel masks have changed, does this track need a downmixer?
  240. // update to try using our desired format (if we aren't already using it)
  241. const audio_format_t prevDownmixerFormat = track.mDownmixRequiresFormat;
  242. const status_t status = mState.tracks[name].prepareForDownmix();
  243. ALOGE_IF(status != OK,
  244. "prepareForDownmix error %d, track channel mask %#x, mixer channel mask %#x",
  245. status, track.channelMask, track.mMixerChannelMask);
  246. if (prevDownmixerFormat != track.mDownmixRequiresFormat) {
  247. track.prepareForReformat(); // because of downmixer, track format may change!
  248. }
  249. if (track.resampler && mixerChannelCountChanged) {
  250. // resampler channels may have changed.
  251. const uint32_t resetToSampleRate = track.sampleRate;
  252. delete track.resampler;
  253. track.resampler = NULL;
  254. track.sampleRate = mSampleRate; // without resampler, track rate is device sample rate.
  255. // recreate the resampler with updated format, channels, saved sampleRate.
  256. track.setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/);
  257. }
  258. return true;
  259. }
  260. void AudioMixer::track_t::unprepareForDownmix() {
  261. ALOGV("AudioMixer::unprepareForDownmix(%p)", this);
  262. mDownmixRequiresFormat = AUDIO_FORMAT_INVALID;
  263. //cjh if (downmixerBufferProvider != NULL) {
  264. // // this track had previously been configured with a downmixer, delete it
  265. // ALOGV(" deleting old downmixer");
  266. // delete downmixerBufferProvider;
  267. // downmixerBufferProvider = NULL;
  268. // reconfigureBufferProviders();
  269. // } else
  270. {
  271. ALOGV(" nothing to do, no downmixer to delete");
  272. }
  273. }
  274. status_t AudioMixer::track_t::prepareForDownmix()
  275. {
  276. ALOGV("AudioMixer::prepareForDownmix(%p) with mask 0x%x",
  277. this, channelMask);
  278. // discard the previous downmixer if there was one
  279. unprepareForDownmix();
  280. // MONO_HACK Only remix (upmix or downmix) if the track and mixer/device channel masks
  281. // are not the same and not handled internally, as mono -> stereo currently is.
  282. if (channelMask == mMixerChannelMask
  283. || (channelMask == AUDIO_CHANNEL_OUT_MONO
  284. && mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) {
  285. return NO_ERROR;
  286. }
  287. // DownmixerBufferProvider is only used for position masks.
  288. //cjh if (audio_channel_mask_get_representation(channelMask)
  289. // == AUDIO_CHANNEL_REPRESENTATION_POSITION
  290. // && DownmixerBufferProvider::isMultichannelCapable()) {
  291. // DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(channelMask,
  292. // mMixerChannelMask,
  293. // AUDIO_FORMAT_PCM_16_BIT /* TODO: use mMixerInFormat, now only PCM 16 */,
  294. // sampleRate, sessionId, kCopyBufferFrameCount);
  295. //
  296. // if (pDbp->isValid()) { // if constructor completed properly
  297. // mDownmixRequiresFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix
  298. // downmixerBufferProvider = pDbp;
  299. // reconfigureBufferProviders();
  300. // return NO_ERROR;
  301. // }
  302. // delete pDbp;
  303. // }
  304. //
  305. // // Effect downmixer does not accept the channel conversion. Let's use our remixer.
  306. // RemixBufferProvider* pRbp = new RemixBufferProvider(channelMask,
  307. // mMixerChannelMask, mMixerInFormat, kCopyBufferFrameCount);
  308. // // Remix always finds a conversion whereas Downmixer effect above may fail.
  309. // downmixerBufferProvider = pRbp;
  310. // reconfigureBufferProviders();
  311. return NO_ERROR;
  312. }
  313. void AudioMixer::track_t::unprepareForReformat() {
  314. ALOGV("AudioMixer::unprepareForReformat(%p)", this);
  315. bool requiresReconfigure = false;
  316. //cjh if (mReformatBufferProvider != NULL) {
  317. // delete mReformatBufferProvider;
  318. // mReformatBufferProvider = NULL;
  319. // requiresReconfigure = true;
  320. // }
  321. // if (mPostDownmixReformatBufferProvider != NULL) {
  322. // delete mPostDownmixReformatBufferProvider;
  323. // mPostDownmixReformatBufferProvider = NULL;
  324. // requiresReconfigure = true;
  325. // }
  326. if (requiresReconfigure) {
  327. reconfigureBufferProviders();
  328. }
  329. }
  330. status_t AudioMixer::track_t::prepareForReformat()
  331. {
  332. ALOGV("AudioMixer::prepareForReformat(%p) with format %#x", this, mFormat);
  333. // discard previous reformatters
  334. unprepareForReformat();
  335. // only configure reformatters as needed
  336. const audio_format_t targetFormat = mDownmixRequiresFormat != AUDIO_FORMAT_INVALID
  337. ? mDownmixRequiresFormat : mMixerInFormat;
  338. bool requiresReconfigure = false;
  339. //cjh if (mFormat != targetFormat) {
  340. // mReformatBufferProvider = new ReformatBufferProvider(
  341. // audio_channel_count_from_out_mask(channelMask),
  342. // mFormat,
  343. // targetFormat,
  344. // kCopyBufferFrameCount);
  345. // requiresReconfigure = true;
  346. // }
  347. // if (targetFormat != mMixerInFormat) {
  348. // mPostDownmixReformatBufferProvider = new ReformatBufferProvider(
  349. // audio_channel_count_from_out_mask(mMixerChannelMask),
  350. // targetFormat,
  351. // mMixerInFormat,
  352. // kCopyBufferFrameCount);
  353. // requiresReconfigure = true;
  354. // }
  355. if (requiresReconfigure) {
  356. reconfigureBufferProviders();
  357. }
  358. ALOGVV("prepareForReformat return ...");
  359. return NO_ERROR;
  360. }
  361. void AudioMixer::track_t::reconfigureBufferProviders()
  362. {
  363. bufferProvider = mInputBufferProvider;
  364. //cjh if (mReformatBufferProvider) {
  365. // mReformatBufferProvider->setBufferProvider(bufferProvider);
  366. // bufferProvider = mReformatBufferProvider;
  367. // }
  368. // if (downmixerBufferProvider) {
  369. // downmixerBufferProvider->setBufferProvider(bufferProvider);
  370. // bufferProvider = downmixerBufferProvider;
  371. // }
  372. // if (mPostDownmixReformatBufferProvider) {
  373. // mPostDownmixReformatBufferProvider->setBufferProvider(bufferProvider);
  374. // bufferProvider = mPostDownmixReformatBufferProvider;
  375. // }
  376. // if (mTimestretchBufferProvider) {
  377. // mTimestretchBufferProvider->setBufferProvider(bufferProvider);
  378. // bufferProvider = mTimestretchBufferProvider;
  379. // }
  380. }
  381. void AudioMixer::deleteTrackName(int name)
  382. {
  383. ALOGV("AudioMixer::deleteTrackName(%d)", name);
  384. name -= TRACK0;
  385. ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
  386. ALOGV("deleteTrackName(%d)", name);
  387. track_t& track(mState.tracks[ name ]);
  388. if (track.enabled) {
  389. track.enabled = false;
  390. invalidateState(1<<name);
  391. }
  392. // delete the resampler
  393. delete track.resampler;
  394. track.resampler = NULL;
  395. // delete the downmixer
  396. mState.tracks[name].unprepareForDownmix();
  397. // delete the reformatter
  398. mState.tracks[name].unprepareForReformat();
  399. // delete the timestretch provider
  400. //cjh delete track.mTimestretchBufferProvider;
  401. // track.mTimestretchBufferProvider = NULL;
  402. mTrackNames &= ~(1<<name);
  403. }
  404. void AudioMixer::enable(int name)
  405. {
  406. name -= TRACK0;
  407. ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
  408. track_t& track = mState.tracks[name];
  409. if (!track.enabled) {
  410. track.enabled = true;
  411. ALOGV("enable(%d)", name);
  412. invalidateState(1 << name);
  413. }
  414. }
  415. void AudioMixer::disable(int name)
  416. {
  417. name -= TRACK0;
  418. ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
  419. track_t& track = mState.tracks[name];
  420. if (track.enabled) {
  421. track.enabled = false;
  422. ALOGV("disable(%d)", name);
  423. invalidateState(1 << name);
  424. }
  425. }
  426. /* Sets the volume ramp variables for the AudioMixer.
  427. *
  428. * The volume ramp variables are used to transition from the previous
  429. * volume to the set volume. ramp controls the duration of the transition.
  430. * Its value is typically one state framecount period, but may also be 0,
  431. * meaning "immediate."
  432. *
  433. * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment
  434. * even if there is a nonzero floating point increment (in that case, the volume
  435. * change is immediate). This restriction should be changed when the legacy mixer
  436. * is removed (see #2).
  437. * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed
  438. * when no longer needed.
  439. *
  440. * @param newVolume set volume target in floating point [0.0, 1.0].
  441. * @param ramp number of frames to increment over. if ramp is 0, the volume
  442. * should be set immediately. Currently ramp should not exceed 65535 (frames).
  443. * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return.
  444. * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return.
  445. * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return.
  446. * @param pSetVolume pointer to the float target volume, set on return.
  447. * @param pPrevVolume pointer to the float previous volume, set on return.
  448. * @param pVolumeInc pointer to the float increment per output audio frame, set on return.
  449. * @return true if the volume has changed, false if volume is same.
  450. */
  451. static inline bool setVolumeRampVariables(float newVolume, int32_t ramp,
  452. int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc,
  453. float *pSetVolume, float *pPrevVolume, float *pVolumeInc) {
  454. // check floating point volume to see if it is identical to the previously
  455. // set volume.
  456. // We do not use a tolerance here (and reject changes too small)
  457. // as it may be confusing to use a different value than the one set.
  458. // If the resulting volume is too small to ramp, it is a direct set of the volume.
  459. if (newVolume == *pSetVolume) {
  460. return false;
  461. }
  462. if (newVolume < 0) {
  463. newVolume = 0; // should not have negative volumes
  464. } else {
  465. switch (fpclassify(newVolume)) {
  466. case FP_SUBNORMAL:
  467. case FP_NAN:
  468. newVolume = 0;
  469. break;
  470. case FP_ZERO:
  471. break; // zero volume is fine
  472. case FP_INFINITE:
  473. // Infinite volume could be handled consistently since
  474. // floating point math saturates at infinities,
  475. // but we limit volume to unity gain float.
  476. // ramp = 0; break;
  477. //
  478. newVolume = AudioMixer::UNITY_GAIN_FLOAT;
  479. break;
  480. case FP_NORMAL:
  481. default:
  482. // Floating point does not have problems with overflow wrap
  483. // that integer has. However, we limit the volume to
  484. // unity gain here.
  485. // TODO: Revisit the volume limitation and perhaps parameterize.
  486. if (newVolume > AudioMixer::UNITY_GAIN_FLOAT) {
  487. newVolume = AudioMixer::UNITY_GAIN_FLOAT;
  488. }
  489. break;
  490. }
  491. }
  492. // set floating point volume ramp
  493. if (ramp != 0) {
  494. // when the ramp completes, *pPrevVolume is set to *pSetVolume, so there
  495. // is no computational mismatch; hence equality is checked here.
  496. ALOGD_IF(*pPrevVolume != *pSetVolume, "previous float ramp hasn't finished,"
  497. " prev:%f set_to:%f", *pPrevVolume, *pSetVolume);
  498. const float inc = (newVolume - *pPrevVolume) / ramp; // could be inf, nan, subnormal
  499. const float maxv = max(newVolume, *pPrevVolume); // could be inf, cannot be nan, subnormal
  500. if (isnormal(inc) // inc must be a normal number (no subnormals, infinite, nan)
  501. && maxv + inc != maxv) { // inc must make forward progress
  502. *pVolumeInc = inc;
  503. // ramp is set now.
  504. // Note: if newVolume is 0, then near the end of the ramp,
  505. // it may be possible that the ramped volume may be subnormal or
  506. // temporarily negative by a small amount or subnormal due to floating
  507. // point inaccuracies.
  508. } else {
  509. ramp = 0; // ramp not allowed
  510. }
  511. }
  512. // compute and check integer volume, no need to check negative values
  513. // The integer volume is limited to "unity_gain" to avoid wrapping and other
  514. // audio artifacts, so it never reaches the range limit of U4.28.
  515. // We safely use signed 16 and 32 bit integers here.
  516. const float scaledVolume = newVolume * AudioMixer::UNITY_GAIN_INT; // not neg, subnormal, nan
  517. const int32_t intVolume = (scaledVolume >= (float)AudioMixer::UNITY_GAIN_INT) ?
  518. AudioMixer::UNITY_GAIN_INT : (int32_t)scaledVolume;
  519. // set integer volume ramp
  520. if (ramp != 0) {
  521. // integer volume is U4.12 (to use 16 bit multiplies), but ramping uses U4.28.
  522. // when the ramp completes, *pIntPrevVolume is set to *pIntSetVolume << 16, so there
  523. // is no computational mismatch; hence equality is checked here.
  524. ALOGD_IF(*pIntPrevVolume != *pIntSetVolume << 16, "previous int ramp hasn't finished,"
  525. " prev:%d set_to:%d", *pIntPrevVolume, *pIntSetVolume << 16);
  526. const int32_t inc = ((intVolume << 16) - *pIntPrevVolume) / ramp;
  527. if (inc != 0) { // inc must make forward progress
  528. *pIntVolumeInc = inc;
  529. } else {
  530. ramp = 0; // ramp not allowed
  531. }
  532. }
  533. // if no ramp, or ramp not allowed, then clear float and integer increments
  534. if (ramp == 0) {
  535. *pVolumeInc = 0;
  536. *pPrevVolume = newVolume;
  537. *pIntVolumeInc = 0;
  538. *pIntPrevVolume = intVolume << 16;
  539. }
  540. *pSetVolume = newVolume;
  541. *pIntSetVolume = intVolume;
  542. return true;
  543. }
  544. void AudioMixer::setParameter(int name, int target, int param, void *value)
  545. {
  546. name -= TRACK0;
  547. ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
  548. track_t& track = mState.tracks[name];
  549. int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value));
  550. int32_t *valueBuf = reinterpret_cast<int32_t*>(value);
  551. switch (target) {
  552. case TRACK:
  553. switch (param) {
  554. case CHANNEL_MASK: {
  555. const audio_channel_mask_t trackChannelMask =
  556. static_cast<audio_channel_mask_t>(valueInt);
  557. if (setChannelMasks(name, trackChannelMask, track.mMixerChannelMask)) {
  558. ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask);
  559. invalidateState(1 << name);
  560. }
  561. } break;
  562. case MAIN_BUFFER:
  563. if (track.mainBuffer != valueBuf) {
  564. track.mainBuffer = valueBuf;
  565. ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
  566. invalidateState(1 << name);
  567. }
  568. break;
  569. case AUX_BUFFER:
  570. if (track.auxBuffer != valueBuf) {
  571. track.auxBuffer = valueBuf;
  572. ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
  573. invalidateState(1 << name);
  574. }
  575. break;
  576. case FORMAT: {
  577. audio_format_t format = static_cast<audio_format_t>(valueInt);
  578. if (track.mFormat != format) {
  579. ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format);
  580. track.mFormat = format;
  581. ALOGV("setParameter(TRACK, FORMAT, %#x)", format);
  582. track.prepareForReformat();
  583. invalidateState(1 << name);
  584. }
  585. } break;
  586. // FIXME do we want to support setting the downmix type from AudioMixerController?
  587. // for a specific track? or per mixer?
  588. /* case DOWNMIX_TYPE:
  589. break */
  590. case MIXER_FORMAT: {
  591. audio_format_t format = static_cast<audio_format_t>(valueInt);
  592. if (track.mMixerFormat != format) {
  593. track.mMixerFormat = format;
  594. ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format);
  595. }
  596. } break;
  597. case MIXER_CHANNEL_MASK: {
  598. const audio_channel_mask_t mixerChannelMask =
  599. static_cast<audio_channel_mask_t>(valueInt);
  600. if (setChannelMasks(name, track.channelMask, mixerChannelMask)) {
  601. ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask);
  602. invalidateState(1 << name);
  603. }
  604. } break;
  605. default:
  606. LOG_ALWAYS_FATAL("setParameter track: bad param %d", param);
  607. }
  608. break;
  609. case RESAMPLE:
  610. switch (param) {
  611. case SAMPLE_RATE:
  612. ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
  613. if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
  614. ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
  615. uint32_t(valueInt));
  616. invalidateState(1 << name);
  617. }
  618. break;
  619. case RESET:
  620. track.resetResampler();
  621. invalidateState(1 << name);
  622. break;
  623. case REMOVE:
  624. delete track.resampler;
  625. track.resampler = NULL;
  626. track.sampleRate = mSampleRate;
  627. invalidateState(1 << name);
  628. break;
  629. default:
  630. LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param);
  631. }
  632. break;
  633. case RAMP_VOLUME:
  634. case VOLUME:
  635. switch (param) {
  636. case AUXLEVEL:
  637. if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
  638. target == RAMP_VOLUME ? mState.frameCount : 0,
  639. &track.auxLevel, &track.prevAuxLevel, &track.auxInc,
  640. &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) {
  641. ALOGV("setParameter(%s, AUXLEVEL: %04x)",
  642. target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel);
  643. invalidateState(1 << name);
  644. }
  645. break;
  646. default:
  647. if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) {
  648. if (setVolumeRampVariables(*reinterpret_cast<float*>(value),
  649. target == RAMP_VOLUME ? mState.frameCount : 0,
  650. &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0],
  651. &track.volumeInc[param - VOLUME0],
  652. &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0],
  653. &track.mVolumeInc[param - VOLUME0])) {
  654. ALOGV("setParameter(%s, VOLUME%d: %04x)",
  655. target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0,
  656. track.volume[param - VOLUME0]);
  657. invalidateState(1 << name);
  658. }
  659. } else {
  660. LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param);
  661. }
  662. }
  663. break;
  664. case TIMESTRETCH:
  665. switch (param) {
  666. case PLAYBACK_RATE: {
  667. const AudioPlaybackRate *playbackRate =
  668. reinterpret_cast<AudioPlaybackRate*>(value);
  669. ALOGW_IF(!isAudioPlaybackRateValid(*playbackRate),
  670. "bad parameters speed %f, pitch %f",playbackRate->mSpeed,
  671. playbackRate->mPitch);
  672. if (track.setPlaybackRate(*playbackRate)) {
  673. ALOGV("setParameter(TIMESTRETCH, PLAYBACK_RATE, STRETCH_MODE, FALLBACK_MODE "
  674. "%f %f %d %d",
  675. playbackRate->mSpeed,
  676. playbackRate->mPitch,
  677. playbackRate->mStretchMode,
  678. playbackRate->mFallbackMode);
  679. // invalidateState(1 << name);
  680. }
  681. } break;
  682. default:
  683. LOG_ALWAYS_FATAL("setParameter timestretch: bad param %d", param);
  684. }
  685. break;
  686. default:
  687. LOG_ALWAYS_FATAL("setParameter: bad target %d", target);
  688. }
  689. }
  690. bool AudioMixer::track_t::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate)
  691. {
  692. if (trackSampleRate != devSampleRate || resampler != NULL) {
  693. if (sampleRate != trackSampleRate) {
  694. sampleRate = trackSampleRate;
  695. if (resampler == NULL) {
  696. ALOGV("Creating resampler from track %d Hz to device %d Hz",
  697. trackSampleRate, devSampleRate);
  698. AudioResampler::src_quality quality;
  699. // force lowest quality level resampler if use case isn't music or video
  700. // FIXME this is flawed for dynamic sample rates, as we choose the resampler
  701. // quality level based on the initial ratio, but that could change later.
  702. // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
  703. //cjh if (isMusicRate(trackSampleRate)) {
  704. quality = AudioResampler::DEFAULT_QUALITY;
  705. //cjh } else {
  706. // quality = AudioResampler::DYN_LOW_QUALITY;
  707. // }
  708. // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
  709. // but if none exists, it is the channel count (1 for mono).
  710. const int resamplerChannelCount = false/*downmixerBufferProvider != NULL*/
  711. ? mMixerChannelCount : channelCount;
  712. ALOGVV("Creating resampler:"
  713. " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n",
  714. mMixerInFormat, resamplerChannelCount, devSampleRate, quality);
  715. resampler = AudioResampler::create(
  716. mMixerInFormat,
  717. resamplerChannelCount,
  718. devSampleRate, quality);
  719. resampler->setLocalTimeFreq(sLocalTimeFreq);
  720. }
  721. return true;
  722. }
  723. }
  724. return false;
  725. }
  726. bool AudioMixer::track_t::setPlaybackRate(const AudioPlaybackRate &playbackRate)
  727. {
  728. //cjh if ((mTimestretchBufferProvider == NULL &&
  729. // fabs(playbackRate.mSpeed - mPlaybackRate.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA &&
  730. // fabs(playbackRate.mPitch - mPlaybackRate.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA) ||
  731. // isAudioPlaybackRateEqual(playbackRate, mPlaybackRate)) {
  732. // return false;
  733. // }
  734. mPlaybackRate = playbackRate;
  735. // if (mTimestretchBufferProvider == NULL) {
  736. // // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer
  737. // // but if none exists, it is the channel count (1 for mono).
  738. // const int timestretchChannelCount = downmixerBufferProvider != NULL
  739. // ? mMixerChannelCount : channelCount;
  740. // mTimestretchBufferProvider = new TimestretchBufferProvider(timestretchChannelCount,
  741. // mMixerInFormat, sampleRate, playbackRate);
  742. // reconfigureBufferProviders();
  743. // } else {
  744. // reinterpret_cast<TimestretchBufferProvider*>(mTimestretchBufferProvider)
  745. // ->setPlaybackRate(playbackRate);
  746. // }
  747. return true;
  748. }
  749. /* Checks to see if the volume ramp has completed and clears the increment
  750. * variables appropriately.
  751. *
  752. * FIXME: There is code to handle int/float ramp variable switchover should it not
  753. * complete within a mixer buffer processing call, but it is preferred to avoid switchover
  754. * due to precision issues. The switchover code is included for legacy code purposes
  755. * and can be removed once the integer volume is removed.
  756. *
  757. * It is not sufficient to clear only the volumeInc integer variable because
  758. * if one channel requires ramping, all channels are ramped.
  759. *
  760. * There is a bit of duplicated code here, but it keeps backward compatibility.
  761. */
  762. inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat)
  763. {
  764. if (useFloat) {
  765. for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
  766. if ((mVolumeInc[i] > 0 && mPrevVolume[i] + mVolumeInc[i] >= mVolume[i]) ||
  767. (mVolumeInc[i] < 0 && mPrevVolume[i] + mVolumeInc[i] <= mVolume[i])) {
  768. volumeInc[i] = 0;
  769. prevVolume[i] = volume[i] << 16;
  770. mVolumeInc[i] = 0.;
  771. mPrevVolume[i] = mVolume[i];
  772. } else {
  773. //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]);
  774. prevVolume[i] = u4_28_from_float(mPrevVolume[i]);
  775. }
  776. }
  777. } else {
  778. for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) {
  779. if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
  780. ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
  781. volumeInc[i] = 0;
  782. prevVolume[i] = volume[i] << 16;
  783. mVolumeInc[i] = 0.;
  784. mPrevVolume[i] = mVolume[i];
  785. } else {
  786. //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]);
  787. mPrevVolume[i] = float_from_u4_28(prevVolume[i]);
  788. }
  789. }
  790. }
  791. /* TODO: aux is always integer regardless of output buffer type */
  792. if (aux) {
  793. if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
  794. ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
  795. auxInc = 0;
  796. prevAuxLevel = auxLevel << 16;
  797. mAuxInc = 0.;
  798. mPrevAuxLevel = mAuxLevel;
  799. } else {
  800. //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc);
  801. }
  802. }
  803. }
  804. size_t AudioMixer::getUnreleasedFrames(int name) const
  805. {
  806. name -= TRACK0;
  807. if (uint32_t(name) < MAX_NUM_TRACKS) {
  808. return mState.tracks[name].getUnreleasedFrames();
  809. }
  810. return 0;
  811. }
  812. void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
  813. {
  814. name -= TRACK0;
  815. ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
  816. if (mState.tracks[name].mInputBufferProvider == bufferProvider) {
  817. return; // don't reset any buffer providers if identical.
  818. }
  819. //cjh if (mState.tracks[name].mReformatBufferProvider != NULL) {
  820. // mState.tracks[name].mReformatBufferProvider->reset();
  821. // } else if (mState.tracks[name].downmixerBufferProvider != NULL) {
  822. // mState.tracks[name].downmixerBufferProvider->reset();
  823. // } else if (mState.tracks[name].mPostDownmixReformatBufferProvider != NULL) {
  824. // mState.tracks[name].mPostDownmixReformatBufferProvider->reset();
  825. // } else if (mState.tracks[name].mTimestretchBufferProvider != NULL) {
  826. // mState.tracks[name].mTimestretchBufferProvider->reset();
  827. // }
  828. mState.tracks[name].mInputBufferProvider = bufferProvider;
  829. mState.tracks[name].reconfigureBufferProviders();
  830. }
  831. void AudioMixer::process(int64_t pts)
  832. {
  833. mState.hook(&mState, pts);
  834. }
  835. void AudioMixer::process__validate(state_t* state, int64_t pts)
  836. {
  837. ALOGW_IF(!state->needsChanged,
  838. "in process__validate() but nothing's invalid");
  839. uint32_t changed = state->needsChanged;
  840. state->needsChanged = 0; // clear the validation flag
  841. // recompute which tracks are enabled / disabled
  842. uint32_t enabled = 0;
  843. uint32_t disabled = 0;
  844. while (changed) {
  845. const int i = 31 - __builtin_clz(changed);
  846. const uint32_t mask = 1<<i;
  847. changed &= ~mask;
  848. track_t& t = state->tracks[i];
  849. (t.enabled ? enabled : disabled) |= mask;
  850. }
  851. state->enabledTracks &= ~disabled;
  852. state->enabledTracks |= enabled;
  853. // compute everything we need...
  854. int countActiveTracks = 0;
  855. // TODO: fix all16BitsStereNoResample logic to
  856. // either properly handle muted tracks (it should ignore them)
  857. // or remove altogether as an obsolete optimization.
  858. bool all16BitsStereoNoResample = true;
  859. bool resampling = false;
  860. bool volumeRamp = false;
  861. uint32_t en = state->enabledTracks;
  862. while (en) {
  863. const int i = 31 - __builtin_clz(en);
  864. en &= ~(1<<i);
  865. countActiveTracks++;
  866. track_t& t = state->tracks[i];
  867. uint32_t n = 0;
  868. // FIXME can overflow (mask is only 3 bits)
  869. n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
  870. if (t.doesResample()) {
  871. n |= NEEDS_RESAMPLE;
  872. }
  873. if (t.auxLevel != 0 && t.auxBuffer != NULL) {
  874. n |= NEEDS_AUX;
  875. }
  876. if (t.volumeInc[0]|t.volumeInc[1]) {
  877. volumeRamp = true;
  878. } else if (!t.doesResample() && t.volumeRL == 0) {
  879. n |= NEEDS_MUTE;
  880. }
  881. t.needs = n;
  882. if (n & NEEDS_MUTE) {
  883. t.hook = track__nop;
  884. } else {
  885. if (n & NEEDS_AUX) {
  886. all16BitsStereoNoResample = false;
  887. }
  888. if (n & NEEDS_RESAMPLE) {
  889. all16BitsStereoNoResample = false;
  890. resampling = true;
  891. t.hook = getTrackHook(TRACKTYPE_RESAMPLE, t.mMixerChannelCount,
  892. t.mMixerInFormat, t.mMixerFormat);
  893. ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
  894. "Track %d needs downmix + resample", i);
  895. } else {
  896. if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
  897. t.hook = getTrackHook(
  898. (t.mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO // TODO: MONO_HACK
  899. && t.channelMask == AUDIO_CHANNEL_OUT_MONO)
  900. ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE,
  901. t.mMixerChannelCount,
  902. t.mMixerInFormat, t.mMixerFormat);
  903. all16BitsStereoNoResample = false;
  904. }
  905. if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){
  906. t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, t.mMixerChannelCount,
  907. t.mMixerInFormat, t.mMixerFormat);
  908. ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2,
  909. "Track %d needs downmix", i);
  910. }
  911. }
  912. }
  913. }
  914. // select the processing hooks
  915. state->hook = process__nop;
  916. if (countActiveTracks > 0) {
  917. if (resampling) {
  918. if (!state->outputTemp) {
  919. state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
  920. }
  921. if (!state->resampleTemp) {
  922. state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
  923. }
  924. state->hook = process__genericResampling;
  925. } else {
  926. if (state->outputTemp) {
  927. delete [] state->outputTemp;
  928. state->outputTemp = NULL;
  929. }
  930. if (state->resampleTemp) {
  931. delete [] state->resampleTemp;
  932. state->resampleTemp = NULL;
  933. }
  934. state->hook = process__genericNoResampling;
  935. if (all16BitsStereoNoResample && !volumeRamp) {
  936. if (countActiveTracks == 1) {
  937. const int i = 31 - __builtin_clz(state->enabledTracks);
  938. track_t& t = state->tracks[i];
  939. if ((t.needs & NEEDS_MUTE) == 0) {
  940. // The check prevents a muted track from acquiring a process hook.
  941. //
  942. // This is dangerous if the track is MONO as that requires
  943. // special case handling due to implicit channel duplication.
  944. // Stereo or Multichannel should actually be fine here.
  945. state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
  946. t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
  947. }
  948. }
  949. }
  950. }
  951. }
  952. ALOGV("mixer configuration change: %d activeTracks (%08x) "
  953. "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
  954. countActiveTracks, state->enabledTracks,
  955. all16BitsStereoNoResample, resampling, volumeRamp);
  956. state->hook(state, pts);
  957. // Now that the volume ramp has been done, set optimal state and
  958. // track hooks for subsequent mixer process
  959. if (countActiveTracks > 0) {
  960. bool allMuted = true;
  961. uint32_t en = state->enabledTracks;
  962. while (en) {
  963. const int i = 31 - __builtin_clz(en);
  964. en &= ~(1<<i);
  965. track_t& t = state->tracks[i];
  966. if (!t.doesResample() && t.volumeRL == 0) {
  967. t.needs |= NEEDS_MUTE;
  968. t.hook = track__nop;
  969. } else {
  970. allMuted = false;
  971. }
  972. }
  973. if (allMuted) {
  974. state->hook = process__nop;
  975. } else if (all16BitsStereoNoResample) {
  976. if (countActiveTracks == 1) {
  977. const int i = 31 - __builtin_clz(state->enabledTracks);
  978. track_t& t = state->tracks[i];
  979. // Muted single tracks handled by allMuted above.
  980. state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK,
  981. t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat);
  982. }
  983. }
  984. }
  985. }
  986. void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount,
  987. int32_t* temp, int32_t* aux)
  988. {
  989. ALOGVV("track__genericResample\n");
  990. t->resampler->setSampleRate(t->sampleRate);
  991. // ramp gain - resample to temp buffer and scale/mix in 2nd step
  992. if (aux != NULL) {
  993. // always resample with unity gain when sending to auxiliary buffer to be able
  994. // to apply send level after resampling
  995. t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
  996. memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(int32_t));
  997. t->resampler->resample(temp, outFrameCount, t->bufferProvider);
  998. if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
  999. volumeRampStereo(t, out, outFrameCount, temp, aux);
  1000. } else {
  1001. volumeStereo(t, out, outFrameCount, temp, aux);
  1002. }
  1003. } else {
  1004. if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
  1005. t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
  1006. memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
  1007. t->resampler->resample(temp, outFrameCount, t->bufferProvider);
  1008. volumeRampStereo(t, out, outFrameCount, temp, aux);
  1009. }
  1010. // constant gain
  1011. else {
  1012. t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
  1013. t->resampler->resample(out, outFrameCount, t->bufferProvider);
  1014. }
  1015. }
  1016. }
  1017. void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused,
  1018. size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused)
  1019. {
  1020. }
  1021. void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
  1022. int32_t* aux)
  1023. {
  1024. int32_t vl = t->prevVolume[0];
  1025. int32_t vr = t->prevVolume[1];
  1026. const int32_t vlInc = t->volumeInc[0];
  1027. const int32_t vrInc = t->volumeInc[1];
  1028. //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
  1029. // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
  1030. // (vl + vlInc*frameCount)/65536.0f, frameCount);
  1031. // ramp volume
  1032. if (CC_UNLIKELY(aux != NULL)) {
  1033. int32_t va = t->prevAuxLevel;
  1034. const int32_t vaInc = t->auxInc;
  1035. int32_t l;
  1036. int32_t r;
  1037. do {
  1038. l = (*temp++ >> 12);
  1039. r = (*temp++ >> 12);
  1040. *out++ += (vl >> 16) * l;
  1041. *out++ += (vr >> 16) * r;
  1042. *aux++ += (va >> 17) * (l + r);
  1043. vl += vlInc;
  1044. vr += vrInc;
  1045. va += vaInc;
  1046. } while (--frameCount);
  1047. t->prevAuxLevel = va;
  1048. } else {
  1049. do {
  1050. *out++ += (vl >> 16) * (*temp++ >> 12);
  1051. *out++ += (vr >> 16) * (*temp++ >> 12);
  1052. vl += vlInc;
  1053. vr += vrInc;
  1054. } while (--frameCount);
  1055. }
  1056. t->prevVolume[0] = vl;
  1057. t->prevVolume[1] = vr;
  1058. t->adjustVolumeRamp(aux != NULL);
  1059. }
  1060. void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp,
  1061. int32_t* aux)
  1062. {
  1063. const int16_t vl = t->volume[0];
  1064. const int16_t vr = t->volume[1];
  1065. if (CC_UNLIKELY(aux != NULL)) {
  1066. const int16_t va = t->auxLevel;
  1067. do {
  1068. int16_t l = (int16_t)(*temp++ >> 12);
  1069. int16_t r = (int16_t)(*temp++ >> 12);
  1070. out[0] = mulAdd(l, vl, out[0]);
  1071. int16_t a = (int16_t)(((int32_t)l + r) >> 1);
  1072. out[1] = mulAdd(r, vr, out[1]);
  1073. out += 2;
  1074. aux[0] = mulAdd(a, va, aux[0]);
  1075. aux++;
  1076. } while (--frameCount);
  1077. } else {
  1078. do {
  1079. int16_t l = (int16_t)(*temp++ >> 12);
  1080. int16_t r = (int16_t)(*temp++ >> 12);
  1081. out[0] = mulAdd(l, vl, out[0]);
  1082. out[1] = mulAdd(r, vr, out[1]);
  1083. out += 2;
  1084. } while (--frameCount);
  1085. }
  1086. }
  1087. void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount,
  1088. int32_t* temp __unused, int32_t* aux)
  1089. {
  1090. ALOGVV("track__16BitsStereo\n");
  1091. const int16_t *in = static_cast<const int16_t *>(t->in);
  1092. if (CC_UNLIKELY(aux != NULL)) {
  1093. int32_t l;
  1094. int32_t r;
  1095. // ramp gain
  1096. if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
  1097. int32_t vl = t->prevVolume[0];
  1098. int32_t vr = t->prevVolume[1];
  1099. int32_t va = t->prevAuxLevel;
  1100. const int32_t vlInc = t->volumeInc[0];
  1101. const int32_t vrInc = t->volumeInc[1];
  1102. const int32_t vaInc = t->auxInc;
  1103. // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
  1104. // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
  1105. // (vl + vlInc*frameCount)/65536.0f, frameCount);
  1106. do {
  1107. l = (int32_t)*in++;
  1108. r = (int32_t)*in++;
  1109. *out++ += (vl >> 16) * l;
  1110. *out++ += (vr >> 16) * r;
  1111. *aux++ += (va >> 17) * (l + r);
  1112. vl += vlInc;
  1113. vr += vrInc;
  1114. va += vaInc;
  1115. } while (--frameCount);
  1116. t->prevVolume[0] = vl;
  1117. t->prevVolume[1] = vr;
  1118. t->prevAuxLevel = va;
  1119. t->adjustVolumeRamp(true);
  1120. }
  1121. // constant gain
  1122. else {
  1123. const uint32_t vrl = t->volumeRL;
  1124. const int16_t va = (int16_t)t->auxLevel;
  1125. do {
  1126. uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
  1127. int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
  1128. in += 2;
  1129. out[0] = mulAddRL(1, rl, vrl, out[0]);
  1130. out[1] = mulAddRL(0, rl, vrl, out[1]);
  1131. out += 2;
  1132. aux[0] = mulAdd(a, va, aux[0]);
  1133. aux++;
  1134. } while (--frameCount);
  1135. }
  1136. } else {
  1137. // ramp gain
  1138. if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
  1139. int32_t vl = t->prevVolume[0];
  1140. int32_t vr = t->prevVolume[1];
  1141. const int32_t vlInc = t->volumeInc[0];
  1142. const int32_t vrInc = t->volumeInc[1];
  1143. // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
  1144. // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
  1145. // (vl + vlInc*frameCount)/65536.0f, frameCount);
  1146. do {
  1147. *out++ += (vl >> 16) * (int32_t) *in++;
  1148. *out++ += (vr >> 16) * (int32_t) *in++;
  1149. vl += vlInc;
  1150. vr += vrInc;
  1151. } while (--frameCount);
  1152. t->prevVolume[0] = vl;
  1153. t->prevVolume[1] = vr;
  1154. t->adjustVolumeRamp(false);
  1155. }
  1156. // constant gain
  1157. else {
  1158. const uint32_t vrl = t->volumeRL;
  1159. do {
  1160. uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
  1161. in += 2;
  1162. out[0] = mulAddRL(1, rl, vrl, out[0]);
  1163. out[1] = mulAddRL(0, rl, vrl, out[1]);
  1164. out += 2;
  1165. } while (--frameCount);
  1166. }
  1167. }
  1168. t->in = in;
  1169. }
  1170. void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount,
  1171. int32_t* temp __unused, int32_t* aux)
  1172. {
  1173. ALOGVV("track__16BitsMono\n");
  1174. const int16_t *in = static_cast<int16_t const *>(t->in);
  1175. if (CC_UNLIKELY(aux != NULL)) {
  1176. // ramp gain
  1177. if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
  1178. int32_t vl = t->prevVolume[0];
  1179. int32_t vr = t->prevVolume[1];
  1180. int32_t va = t->prevAuxLevel;
  1181. const int32_t vlInc = t->volumeInc[0];
  1182. const int32_t vrInc = t->volumeInc[1];
  1183. const int32_t vaInc = t->auxInc;
  1184. // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
  1185. // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
  1186. // (vl + vlInc*frameCount)/65536.0f, frameCount);
  1187. do {
  1188. int32_t l = *in++;
  1189. *out++ += (vl >> 16) * l;
  1190. *out++ += (vr >> 16) * l;
  1191. *aux++ += (va >> 16) * l;
  1192. vl += vlInc;
  1193. vr += vrInc;
  1194. va += vaInc;
  1195. } while (--frameCount);
  1196. t->prevVolume[0] = vl;
  1197. t->prevVolume[1] = vr;
  1198. t->prevAuxLevel = va;
  1199. t->adjustVolumeRamp(true);
  1200. }
  1201. // constant gain
  1202. else {
  1203. const int16_t vl = t->volume[0];
  1204. const int16_t vr = t->volume[1];
  1205. const int16_t va = (int16_t)t->auxLevel;
  1206. do {
  1207. int16_t l = *in++;
  1208. out[0] = mulAdd(l, vl, out[0]);
  1209. out[1] = mulAdd(l, vr, out[1]);
  1210. out += 2;
  1211. aux[0] = mulAdd(l, va, aux[0]);
  1212. aux++;
  1213. } while (--frameCount);
  1214. }
  1215. } else {
  1216. // ramp gain
  1217. if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
  1218. int32_t vl = t->prevVolume[0];
  1219. int32_t vr = t->prevVolume[1];
  1220. const int32_t vlInc = t->volumeInc[0];
  1221. const int32_t vrInc = t->volumeInc[1];
  1222. // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
  1223. // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
  1224. // (vl + vlInc*frameCount)/65536.0f, frameCount);
  1225. do {
  1226. int32_t l = *in++;
  1227. *out++ += (vl >> 16) * l;
  1228. *out++ += (vr >> 16) * l;
  1229. vl += vlInc;
  1230. vr += vrInc;
  1231. } while (--frameCount);
  1232. t->prevVolume[0] = vl;
  1233. t->prevVolume[1] = vr;
  1234. t->adjustVolumeRamp(false);
  1235. }
  1236. // constant gain
  1237. else {
  1238. const int16_t vl = t->volume[0];
  1239. const int16_t vr = t->volume[1];
  1240. do {
  1241. int16_t l = *in++;
  1242. out[0] = mulAdd(l, vl, out[0]);
  1243. out[1] = mulAdd(l, vr, out[1]);
  1244. out += 2;
  1245. } while (--frameCount);
  1246. }
  1247. }
  1248. t->in = in;
  1249. }
  1250. // no-op case
  1251. void AudioMixer::process__nop(state_t* state, int64_t pts)
  1252. {
  1253. ALOGVV("process__nop\n");
  1254. uint32_t e0 = state->enabledTracks;
  1255. while (e0) {
  1256. // process by group of tracks with same output buffer to
  1257. // avoid multiple memset() on same buffer
  1258. uint32_t e1 = e0, e2 = e0;
  1259. int i = 31 - __builtin_clz(e1);
  1260. {
  1261. track_t& t1 = state->tracks[i];
  1262. e2 &= ~(1<<i);
  1263. while (e2) {
  1264. i = 31 - __builtin_clz(e2);
  1265. e2 &= ~(1<<i);
  1266. track_t& t2 = state->tracks[i];
  1267. if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
  1268. e1 &= ~(1<<i);
  1269. }
  1270. }
  1271. e0 &= ~(e1);
  1272. memset(t1.mainBuffer, 0, state->frameCount * t1.mMixerChannelCount
  1273. * audio_bytes_per_sample(t1.mMixerFormat));
  1274. }
  1275. while (e1) {
  1276. i = 31 - __builtin_clz(e1);
  1277. e1 &= ~(1<<i);
  1278. {
  1279. track_t& t3 = state->tracks[i];
  1280. size_t outFrames = state->frameCount;
  1281. while (outFrames) {
  1282. t3.buffer.frameCount = outFrames;
  1283. int64_t outputPTS = calculateOutputPTS(
  1284. t3, pts, state->frameCount - outFrames);
  1285. t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS);
  1286. if (t3.buffer.raw == NULL) break;
  1287. outFrames -= t3.buffer.frameCount;
  1288. t3.bufferProvider->releaseBuffer(&t3.buffer);
  1289. }
  1290. }
  1291. }
  1292. }
  1293. }
  1294. // generic code without resampling
  1295. void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
  1296. {
  1297. ALOGVV("process__genericNoResampling\n");
  1298. int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
  1299. // acquire each track's buffer
  1300. uint32_t enabledTracks = state->enabledTracks;
  1301. uint32_t e0 = enabledTracks;
  1302. while (e0) {
  1303. const int i = 31 - __builtin_clz(e0);
  1304. e0 &= ~(1<<i);
  1305. track_t& t = state->tracks[i];
  1306. t.buffer.frameCount = state->frameCount;
  1307. t.bufferProvider->getNextBuffer(&t.buffer, pts);
  1308. t.frameCount = t.buffer.frameCount;
  1309. t.in = t.buffer.raw;
  1310. }
  1311. e0 = enabledTracks;
  1312. while (e0) {
  1313. // process by group of tracks with same output buffer to
  1314. // optimize cache use
  1315. uint32_t e1 = e0, e2 = e0;
  1316. int j = 31 - __builtin_clz(e1);
  1317. track_t& t1 = state->tracks[j];
  1318. e2 &= ~(1<<j);
  1319. while (e2) {
  1320. j = 31 - __builtin_clz(e2);
  1321. e2 &= ~(1<<j);
  1322. track_t& t2 = state->tracks[j];
  1323. if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
  1324. e1 &= ~(1<<j);
  1325. }
  1326. }
  1327. e0 &= ~(e1);
  1328. // this assumes output 16 bits stereo, no resampling
  1329. int32_t *out = t1.mainBuffer;
  1330. size_t numFrames = 0;
  1331. do {
  1332. memset(outTemp, 0, sizeof(outTemp));
  1333. e2 = e1;
  1334. while (e2) {
  1335. const int i = 31 - __builtin_clz(e2);
  1336. e2 &= ~(1<<i);
  1337. track_t& t = state->tracks[i];
  1338. size_t outFrames = BLOCKSIZE;
  1339. int32_t *aux = NULL;
  1340. if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
  1341. aux = t.auxBuffer + numFrames;
  1342. }
  1343. while (outFrames) {
  1344. // t.in == NULL can happen if the track was flushed just after having
  1345. // been enabled for mixing.
  1346. if (t.in == NULL) {
  1347. enabledTracks &= ~(1<<i);
  1348. e1 &= ~(1<<i);
  1349. break;
  1350. }
  1351. size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
  1352. if (inFrames > 0) {
  1353. t.hook(&t, outTemp + (BLOCKSIZE - outFrames) * t.mMixerChannelCount,
  1354. inFrames, state->resampleTemp, aux);
  1355. t.frameCount -= inFrames;
  1356. outFrames -= inFrames;
  1357. if (CC_UNLIKELY(aux != NULL)) {
  1358. aux += inFrames;
  1359. }
  1360. }
  1361. if (t.frameCount == 0 && outFrames) {
  1362. t.bufferProvider->releaseBuffer(&t.buffer);
  1363. t.buffer.frameCount = (state->frameCount - numFrames) -
  1364. (BLOCKSIZE - outFrames);
  1365. int64_t outputPTS = calculateOutputPTS(
  1366. t, pts, numFrames + (BLOCKSIZE - outFrames));
  1367. t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
  1368. t.in = t.buffer.raw;
  1369. if (t.in == NULL) {
  1370. enabledTracks &= ~(1<<i);
  1371. e1 &= ~(1<<i);
  1372. break;
  1373. }
  1374. t.frameCount = t.buffer.frameCount;
  1375. }
  1376. }
  1377. }
  1378. convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat,
  1379. BLOCKSIZE * t1.mMixerChannelCount);
  1380. // TODO: fix ugly casting due to choice of out pointer type
  1381. out = reinterpret_cast<int32_t*>((uint8_t*)out
  1382. + BLOCKSIZE * t1.mMixerChannelCount
  1383. * audio_bytes_per_sample(t1.mMixerFormat));
  1384. numFrames += BLOCKSIZE;
  1385. } while (numFrames < state->frameCount);
  1386. }
  1387. // release each track's buffer
  1388. e0 = enabledTracks;
  1389. while (e0) {
  1390. const int i = 31 - __builtin_clz(e0);
  1391. e0 &= ~(1<<i);
  1392. track_t& t = state->tracks[i];
  1393. t.bufferProvider->releaseBuffer(&t.buffer);
  1394. }
  1395. }
  1396. // generic code with resampling
  1397. void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
  1398. {
  1399. ALOGVV("process__genericResampling\n");
  1400. // this const just means that local variable outTemp doesn't change
  1401. int32_t* const outTemp = state->outputTemp;
  1402. size_t numFrames = state->frameCount;
  1403. uint32_t e0 = state->enabledTracks;
  1404. while (e0) {
  1405. // process by group of tracks with same output buffer
  1406. // to optimize cache use
  1407. uint32_t e1 = e0, e2 = e0;
  1408. int j = 31 - __builtin_clz(e1);
  1409. track_t& t1 = state->tracks[j];
  1410. e2 &= ~(1<<j);
  1411. while (e2) {
  1412. j = 31 - __builtin_clz(e2);
  1413. e2 &= ~(1<<j);
  1414. track_t& t2 = state->tracks[j];
  1415. if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
  1416. e1 &= ~(1<<j);
  1417. }
  1418. }
  1419. e0 &= ~(e1);
  1420. int32_t *out = t1.mainBuffer;
  1421. memset(outTemp, 0, sizeof(*outTemp) * t1.mMixerChannelCount * state->frameCount);
  1422. while (e1) {
  1423. const int i = 31 - __builtin_clz(e1);
  1424. e1 &= ~(1<<i);
  1425. track_t& t = state->tracks[i];
  1426. int32_t *aux = NULL;
  1427. if (CC_UNLIKELY(t.needs & NEEDS_AUX)) {
  1428. aux = t.auxBuffer;
  1429. }
  1430. // this is a little goofy, on the resampling case we don't
  1431. // acquire/release the buffers because it's done by
  1432. // the resampler.
  1433. if (t.needs & NEEDS_RESAMPLE) {
  1434. t.resampler->setPTS(pts);
  1435. t.hook(&t, outTemp, numFrames, state->resampleTemp, aux);
  1436. } else {
  1437. size_t outFrames = 0;
  1438. while (outFrames < numFrames) {
  1439. t.buffer.frameCount = numFrames - outFrames;
  1440. int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
  1441. t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
  1442. t.in = t.buffer.raw;
  1443. // t.in == NULL can happen if the track was flushed just after having
  1444. // been enabled for mixing.
  1445. if (t.in == NULL) break;
  1446. if (CC_UNLIKELY(aux != NULL)) {
  1447. aux += outFrames;
  1448. }
  1449. t.hook(&t, outTemp + outFrames * t.mMixerChannelCount, t.buffer.frameCount,
  1450. state->resampleTemp, aux);
  1451. outFrames += t.buffer.frameCount;
  1452. t.bufferProvider->releaseBuffer(&t.buffer);
  1453. }
  1454. }
  1455. }
  1456. convertMixerFormat(out, t1.mMixerFormat,
  1457. outTemp, t1.mMixerInFormat, numFrames * t1.mMixerChannelCount);
  1458. }
  1459. }
  1460. // one track, 16 bits stereo without resampling is the most common case
  1461. void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
  1462. int64_t pts)
  1463. {
  1464. ALOGVV("process__OneTrack16BitsStereoNoResampling\n");
  1465. // This method is only called when state->enabledTracks has exactly
  1466. // one bit set. The asserts below would verify this, but are commented out
  1467. // since the whole point of this method is to optimize performance.
  1468. //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled");
  1469. const int i = 31 - __builtin_clz(state->enabledTracks);
  1470. //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
  1471. const track_t& t = state->tracks[i];
  1472. AudioBufferProvider::Buffer& b(t.buffer);
  1473. int32_t* out = t.mainBuffer;
  1474. float *fout = reinterpret_cast<float*>(out);
  1475. size_t numFrames = state->frameCount;
  1476. const int16_t vl = t.volume[0];
  1477. const int16_t vr = t.volume[1];
  1478. const uint32_t vrl = t.volumeRL;
  1479. while (numFrames) {
  1480. b.frameCount = numFrames;
  1481. int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
  1482. t.bufferProvider->getNextBuffer(&b, outputPTS);
  1483. const int16_t *in = b.i16;
  1484. // in == NULL can happen if the track was flushed just after having
  1485. // been enabled for mixing.
  1486. if (in == NULL || (((uintptr_t)in) & 3)) {
  1487. memset(out, 0, numFrames
  1488. * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat));
  1489. ALOGE_IF((((uintptr_t)in) & 3),
  1490. "process__OneTrack16BitsStereoNoResampling: misaligned buffer"
  1491. " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f",
  1492. in, i, t.channelCount, t.needs, vrl, t.mVolume[0], t.mVolume[1]);
  1493. return;
  1494. }
  1495. size_t outFrames = b.frameCount;
  1496. switch (t.mMixerFormat) {
  1497. case AUDIO_FORMAT_PCM_FLOAT:
  1498. do {
  1499. uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
  1500. in += 2;
  1501. int32_t l = mulRL(1, rl, vrl);
  1502. int32_t r = mulRL(0, rl, vrl);
  1503. *fout++ = float_from_q4_27(l);
  1504. *fout++ = float_from_q4_27(r);
  1505. // Note: In case of later int16_t sink output,
  1506. // conversion and clamping is done by memcpy_to_i16_from_float().
  1507. } while (--outFrames);
  1508. break;
  1509. case AUDIO_FORMAT_PCM_16_BIT:
  1510. if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) {
  1511. // volume is boosted, so we might need to clamp even though
  1512. // we process only one track.
  1513. do {
  1514. uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
  1515. in += 2;
  1516. int32_t l = mulRL(1, rl, vrl) >> 12;
  1517. int32_t r = mulRL(0, rl, vrl) >> 12;
  1518. // clamping...
  1519. l = clamp16(l);
  1520. r = clamp16(r);
  1521. *out++ = (r<<16) | (l & 0xFFFF);
  1522. } while (--outFrames);
  1523. } else {
  1524. do {
  1525. uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
  1526. in += 2;
  1527. int32_t l = mulRL(1, rl, vrl) >> 12;
  1528. int32_t r = mulRL(0, rl, vrl) >> 12;
  1529. *out++ = (r<<16) | (l & 0xFFFF);
  1530. } while (--outFrames);
  1531. }
  1532. break;
  1533. default:
  1534. LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat);
  1535. }
  1536. numFrames -= b.frameCount;
  1537. t.bufferProvider->releaseBuffer(&b);
  1538. }
  1539. }
  1540. int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
  1541. int outputFrameIndex)
  1542. {
  1543. if (AudioBufferProvider::kInvalidPTS == basePTS) {
  1544. return AudioBufferProvider::kInvalidPTS;
  1545. }
  1546. return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate);
  1547. }
  1548. /*static*/ uint64_t AudioMixer::sLocalTimeFreq;
  1549. /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT;
  1550. /*static*/ void AudioMixer::sInitRoutine()
  1551. {
  1552. //cjh LocalClock lc;
  1553. // sLocalTimeFreq = lc.getLocalFreq(); // for the resampler
  1554. //
  1555. // DownmixerBufferProvider::init(); // for the downmixer
  1556. }
  1557. /* TODO: consider whether this level of optimization is necessary.
  1558. * Perhaps just stick with a single for loop.
  1559. */
  1560. // Needs to derive a compile time constant (constexpr). Could be targeted to go
  1561. // to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication.
  1562. #define MIXTYPE_MONOVOL(mixtype) (mixtype == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \
  1563. mixtype == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : mixtype)
  1564. /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
  1565. * TO: int32_t (Q4.27) or float
  1566. * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
  1567. * TA: int32_t (Q4.27)
  1568. */
  1569. template <int MIXTYPE,
  1570. typename TO, typename TI, typename TV, typename TA, typename TAV>
  1571. static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount,
  1572. const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc)
  1573. {
  1574. switch (channels) {
  1575. case 1:
  1576. volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc);
  1577. break;
  1578. case 2:
  1579. volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc);
  1580. break;
  1581. case 3:
  1582. volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out,
  1583. frameCount, in, aux, vol, volinc, vola, volainc);
  1584. break;
  1585. case 4:
  1586. volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out,
  1587. frameCount, in, aux, vol, volinc, vola, volainc);
  1588. break;
  1589. case 5:
  1590. volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out,
  1591. frameCount, in, aux, vol, volinc, vola, volainc);
  1592. break;
  1593. case 6:
  1594. volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out,
  1595. frameCount, in, aux, vol, volinc, vola, volainc);
  1596. break;
  1597. case 7:
  1598. volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out,
  1599. frameCount, in, aux, vol, volinc, vola, volainc);
  1600. break;
  1601. case 8:
  1602. volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out,
  1603. frameCount, in, aux, vol, volinc, vola, volainc);
  1604. break;
  1605. }
  1606. }
  1607. /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
  1608. * TO: int32_t (Q4.27) or float
  1609. * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
  1610. * TA: int32_t (Q4.27)
  1611. */
  1612. template <int MIXTYPE,
  1613. typename TO, typename TI, typename TV, typename TA, typename TAV>
  1614. static void volumeMulti(uint32_t channels, TO* out, size_t frameCount,
  1615. const TI* in, TA* aux, const TV *vol, TAV vola)
  1616. {
  1617. switch (channels) {
  1618. case 1:
  1619. volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola);
  1620. break;
  1621. case 2:
  1622. volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola);
  1623. break;
  1624. case 3:
  1625. volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola);
  1626. break;
  1627. case 4:
  1628. volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola);
  1629. break;
  1630. case 5:
  1631. volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola);
  1632. break;
  1633. case 6:
  1634. volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola);
  1635. break;
  1636. case 7:
  1637. volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola);
  1638. break;
  1639. case 8:
  1640. volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola);
  1641. break;
  1642. }
  1643. }
  1644. /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
  1645. * USEFLOATVOL (set to true if float volume is used)
  1646. * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards)
  1647. * TO: int32_t (Q4.27) or float
  1648. * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
  1649. * TA: int32_t (Q4.27)
  1650. */
  1651. template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL,
  1652. typename TO, typename TI, typename TA>
  1653. void AudioMixer::volumeMix(TO *out, size_t outFrames,
  1654. const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t)
  1655. {
  1656. if (USEFLOATVOL) {
  1657. if (ramp) {
  1658. volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
  1659. t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc);
  1660. if (ADJUSTVOL) {
  1661. t->adjustVolumeRamp(aux != NULL, true);
  1662. }
  1663. } else {
  1664. volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
  1665. t->mVolume, t->auxLevel);
  1666. }
  1667. } else {
  1668. if (ramp) {
  1669. volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
  1670. t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc);
  1671. if (ADJUSTVOL) {
  1672. t->adjustVolumeRamp(aux != NULL);
  1673. }
  1674. } else {
  1675. volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux,
  1676. t->volume, t->auxLevel);
  1677. }
  1678. }
  1679. }
  1680. /* This process hook is called when there is a single track without
  1681. * aux buffer, volume ramp, or resampling.
  1682. * TODO: Update the hook selection: this can properly handle aux and ramp.
  1683. *
  1684. * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
  1685. * TO: int32_t (Q4.27) or float
  1686. * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
  1687. * TA: int32_t (Q4.27)
  1688. */
  1689. template <int MIXTYPE, typename TO, typename TI, typename TA>
  1690. void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts)
  1691. {
  1692. ALOGVV("process_NoResampleOneTrack\n");
  1693. // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz.
  1694. const int i = 31 - __builtin_clz(state->enabledTracks);
  1695. ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled");
  1696. track_t *t = &state->tracks[i];
  1697. const uint32_t channels = t->mMixerChannelCount;
  1698. TO* out = reinterpret_cast<TO*>(t->mainBuffer);
  1699. TA* aux = reinterpret_cast<TA*>(t->auxBuffer);
  1700. const bool ramp = t->needsRamp();
  1701. for (size_t numFrames = state->frameCount; numFrames; ) {
  1702. AudioBufferProvider::Buffer& b(t->buffer);
  1703. // get input buffer
  1704. b.frameCount = numFrames;
  1705. const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames);
  1706. t->bufferProvider->getNextBuffer(&b, outputPTS);
  1707. const TI *in = reinterpret_cast<TI*>(b.raw);
  1708. // in == NULL can happen if the track was flushed just after having
  1709. // been enabled for mixing.
  1710. if (in == NULL || (((uintptr_t)in) & 3)) {
  1711. memset(out, 0, numFrames
  1712. * channels * audio_bytes_per_sample(t->mMixerFormat));
  1713. ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: "
  1714. "buffer %p track %p, channels %d, needs %#x",
  1715. in, t, t->channelCount, t->needs);
  1716. return;
  1717. }
  1718. const size_t outFrames = b.frameCount;
  1719. volumeMix<MIXTYPE, is_same<TI, float>::value, false> (
  1720. out, outFrames, in, aux, ramp, t);
  1721. out += outFrames * channels;
  1722. if (aux != NULL) {
  1723. aux += channels;
  1724. }
  1725. numFrames -= b.frameCount;
  1726. // release buffer
  1727. t->bufferProvider->releaseBuffer(&b);
  1728. }
  1729. if (ramp) {
  1730. t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value);
  1731. }
  1732. }
  1733. /* This track hook is called to do resampling then mixing,
  1734. * pulling from the track's upstream AudioBufferProvider.
  1735. *
  1736. * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
  1737. * TO: int32_t (Q4.27) or float
  1738. * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
  1739. * TA: int32_t (Q4.27)
  1740. */
  1741. template <int MIXTYPE, typename TO, typename TI, typename TA>
  1742. void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux)
  1743. {
  1744. ALOGVV("track__Resample\n");
  1745. t->resampler->setSampleRate(t->sampleRate);
  1746. const bool ramp = t->needsRamp();
  1747. if (ramp || aux != NULL) {
  1748. // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step.
  1749. // if aux != NULL: resample with unity gain to temp buffer then apply send level.
  1750. t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT);
  1751. memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(TO));
  1752. t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider);
  1753. volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
  1754. out, outFrameCount, temp, aux, ramp, t);
  1755. } else { // constant volume gain
  1756. t->resampler->setVolume(t->mVolume[0], t->mVolume[1]);
  1757. t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider);
  1758. }
  1759. }
  1760. /* This track hook is called to mix a track, when no resampling is required.
  1761. * The input buffer should be present in t->in.
  1762. *
  1763. * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration)
  1764. * TO: int32_t (Q4.27) or float
  1765. * TI: int32_t (Q4.27) or int16_t (Q0.15) or float
  1766. * TA: int32_t (Q4.27)
  1767. */
  1768. template <int MIXTYPE, typename TO, typename TI, typename TA>
  1769. void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount,
  1770. TO* temp __unused, TA* aux)
  1771. {
  1772. ALOGVV("track__NoResample\n");
  1773. const TI *in = static_cast<const TI *>(t->in);
  1774. volumeMix<MIXTYPE, is_same<TI, float>::value, true>(
  1775. out, frameCount, in, aux, t->needsRamp(), t);
  1776. // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels.
  1777. // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels.
  1778. in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * t->mMixerChannelCount;
  1779. t->in = in;
  1780. }
  1781. /* The Mixer engine generates either int32_t (Q4_27) or float data.
  1782. * We use this function to convert the engine buffers
  1783. * to the desired mixer output format, either int16_t (Q.15) or float.
  1784. */
  1785. void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat,
  1786. void *in, audio_format_t mixerInFormat, size_t sampleCount)
  1787. {
  1788. switch (mixerInFormat) {
  1789. case AUDIO_FORMAT_PCM_FLOAT:
  1790. switch (mixerOutFormat) {
  1791. case AUDIO_FORMAT_PCM_FLOAT:
  1792. memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out
  1793. break;
  1794. case AUDIO_FORMAT_PCM_16_BIT:
  1795. memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount);
  1796. break;
  1797. default:
  1798. LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
  1799. break;
  1800. }
  1801. break;
  1802. case AUDIO_FORMAT_PCM_16_BIT:
  1803. switch (mixerOutFormat) {
  1804. case AUDIO_FORMAT_PCM_FLOAT:
  1805. memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount);
  1806. break;
  1807. case AUDIO_FORMAT_PCM_16_BIT:
  1808. // two int16_t are produced per iteration
  1809. ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1);
  1810. break;
  1811. default:
  1812. LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
  1813. break;
  1814. }
  1815. break;
  1816. default:
  1817. LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
  1818. break;
  1819. }
  1820. }
  1821. /* Returns the proper track hook to use for mixing the track into the output buffer.
  1822. */
  1823. AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, uint32_t channelCount,
  1824. audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused)
  1825. {
  1826. if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
  1827. switch (trackType) {
  1828. case TRACKTYPE_NOP:
  1829. return track__nop;
  1830. case TRACKTYPE_RESAMPLE:
  1831. return track__genericResample;
  1832. case TRACKTYPE_NORESAMPLEMONO:
  1833. return track__16BitsMono;
  1834. case TRACKTYPE_NORESAMPLE:
  1835. return track__16BitsStereo;
  1836. default:
  1837. LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
  1838. break;
  1839. }
  1840. }
  1841. LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
  1842. switch (trackType) {
  1843. case TRACKTYPE_NOP:
  1844. return track__nop;
  1845. case TRACKTYPE_RESAMPLE:
  1846. switch (mixerInFormat) {
  1847. case AUDIO_FORMAT_PCM_FLOAT:
  1848. return (AudioMixer::hook_t)
  1849. track__Resample<MIXTYPE_MULTI, float /*TO*/, float /*TI*/, int32_t /*TA*/>;
  1850. case AUDIO_FORMAT_PCM_16_BIT:
  1851. return (AudioMixer::hook_t)\
  1852. track__Resample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
  1853. default:
  1854. LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
  1855. break;
  1856. }
  1857. break;
  1858. case TRACKTYPE_NORESAMPLEMONO:
  1859. switch (mixerInFormat) {
  1860. case AUDIO_FORMAT_PCM_FLOAT:
  1861. return (AudioMixer::hook_t)
  1862. track__NoResample<MIXTYPE_MONOEXPAND, float, float, int32_t>;
  1863. case AUDIO_FORMAT_PCM_16_BIT:
  1864. return (AudioMixer::hook_t)
  1865. track__NoResample<MIXTYPE_MONOEXPAND, int32_t, int16_t, int32_t>;
  1866. default:
  1867. LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
  1868. break;
  1869. }
  1870. break;
  1871. case TRACKTYPE_NORESAMPLE:
  1872. switch (mixerInFormat) {
  1873. case AUDIO_FORMAT_PCM_FLOAT:
  1874. return (AudioMixer::hook_t)
  1875. track__NoResample<MIXTYPE_MULTI, float, float, int32_t>;
  1876. case AUDIO_FORMAT_PCM_16_BIT:
  1877. return (AudioMixer::hook_t)
  1878. track__NoResample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>;
  1879. default:
  1880. LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
  1881. break;
  1882. }
  1883. break;
  1884. default:
  1885. LOG_ALWAYS_FATAL("bad trackType: %d", trackType);
  1886. break;
  1887. }
  1888. return NULL;
  1889. }
  1890. /* Returns the proper process hook for mixing tracks. Currently works only for
  1891. * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling.
  1892. *
  1893. * TODO: Due to the special mixing considerations of duplicating to
  1894. * a stereo output track, the input track cannot be MONO. This should be
  1895. * prevented by the caller.
  1896. */
  1897. AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, uint32_t channelCount,
  1898. audio_format_t mixerInFormat, audio_format_t mixerOutFormat)
  1899. {
  1900. if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK
  1901. LOG_ALWAYS_FATAL("bad processType: %d", processType);
  1902. return NULL;
  1903. }
  1904. if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) {
  1905. return process__OneTrack16BitsStereoNoResampling;
  1906. }
  1907. LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS);
  1908. switch (mixerInFormat) {
  1909. case AUDIO_FORMAT_PCM_FLOAT:
  1910. switch (mixerOutFormat) {
  1911. case AUDIO_FORMAT_PCM_FLOAT:
  1912. return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
  1913. float /*TO*/, float /*TI*/, int32_t /*TA*/>;
  1914. case AUDIO_FORMAT_PCM_16_BIT:
  1915. return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
  1916. int16_t, float, int32_t>;
  1917. default:
  1918. LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
  1919. break;
  1920. }
  1921. break;
  1922. case AUDIO_FORMAT_PCM_16_BIT:
  1923. switch (mixerOutFormat) {
  1924. case AUDIO_FORMAT_PCM_FLOAT:
  1925. return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
  1926. float, int16_t, int32_t>;
  1927. case AUDIO_FORMAT_PCM_16_BIT:
  1928. return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY,
  1929. int16_t, int16_t, int32_t>;
  1930. default:
  1931. LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat);
  1932. break;
  1933. }
  1934. break;
  1935. default:
  1936. LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat);
  1937. break;
  1938. }
  1939. return NULL;
  1940. }
  1941. // ----------------------------------------------------------------------------
  1942. }} // namespace cocos2d { namespace experimental {