Update contrib.
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "midiclientutilitybody.h"
18 const TInt KMimeLength = 256;
19 const TInt KMinChannel = 0;
20 const TInt KMaxChannel = 15;
21 const TInt KMinNote = 0;
22 const TInt KMaxNote = 127;
23 const TInt KMinNoteOnVelocity = 0;
24 const TInt KMaxNoteOnVelocity = 127;
25 const TInt KMinNoteOffVelocity = 0;
26 const TInt KMaxNoteOffVelocity = 127;
27 const TInt KMinInstrumentId = 0;
28 const TInt KMaxInstrumentId = 127;
31 CMidiClientUtility::CBody* CMidiClientUtility::CBody::NewL(CMidiClientUtility* aParent,
32 MMidiClientUtilityObserver& aObserver,
37 CBody* self = new(ELeave) CBody(aParent, aObserver, aPriority, aPref);
38 CleanupStack::PushL(self);
39 self->ConstructL(aUseSharedHeap);
40 CleanupStack::Pop(self);
44 CMidiClientUtility::CBody::CBody(CMidiClientUtility* aParent,
45 MMidiClientUtilityObserver& aObserver,
49 iMidiControllerCommands(iController),
50 iDRMCustomCommands(iController)
54 iState = EMidiStateClosedDisengaged;
55 iPrioritySettings.iPriority = aPriority;
56 iPrioritySettings.iPref = aPref;
58 iStopPosition = TTimeIntervalMicroSeconds(0);
61 CMidiClientUtility::CBody::~CBody()
63 delete iAddDataSourceSinkAsync;
64 if (iMidiControllerEventMonitor)
65 iMidiControllerEventMonitor->Cancel();
68 delete iMidiControllerEventMonitor;
70 delete iRepeatTrailingSilenceTimer;
74 void CMidiClientUtility::CBody::ConstructL(TBool aUseSharedHeap)
76 iMidiControllerEventMonitor = CMidiControllerEventMonitor::NewL(*this, iMidiControllerCommands, *iParent);
77 iMimeType = HBufC8::NewL(KMimeLength);
79 RMMFControllerImplInfoArray controllers;
80 CleanupResetAndDestroyPushL(controllers);
81 CMMFControllerPluginSelectionParameters* cSelect = CMMFControllerPluginSelectionParameters::NewLC();
83 // Select the media IDs to allow
84 RArray<TUid> mediaIds;
85 CleanupClosePushL(mediaIds);
86 User::LeaveIfError(mediaIds.Append(KUidMediaTypeMidi));
87 cSelect->SetMediaIdsL(mediaIds,CMMFPluginSelectionParameters::EAllowOnlySuppliedMediaIds);
88 CleanupStack::PopAndDestroy();//mediaIds
89 cSelect->ListImplementationsL(controllers);
90 // Open and configure a controller
91 User::LeaveIfError(DoOpen(controllers, KUidMmfAudioOutput, KNullDesC8, aUseSharedHeap));
92 CleanupStack::PopAndDestroy(2);//controllers, cSelect
93 iRepeatTrailingSilenceTimer = CRepeatTrailingSilenceTimer::NewL(*this);
94 iAddDataSourceSinkAsync = CMMFAddDataSourceSinkAsync::NewL(*this);
97 void CMidiClientUtility::CBody::MadssaoAddDataSourceSinkAsyncComplete(TInt aError, const TMMFMessageDestination& aHandle)
99 if (aError == KErrNone)
101 iSourceHandle = aHandle;
105 iMidiControllerEventMonitor->SelfComplete(aError);
108 void CMidiClientUtility::CBody::OpenFile(const TDesC& aFileName)
110 TMMFFileConfig sourceCfg;
111 sourceCfg().iPath = aFileName;
112 // Add the data source to the controller. MmcuoStateChanged will be call on completition.
113 iAddDataSourceSinkAsync->AddDataSource(iController, KUidMmfFileSource, sourceCfg);
116 void CMidiClientUtility::CBody::OpenFile(RFile& aFile)
118 // Add the data source to the controller. MmcuoStateChanged will be call on completition.
119 iAddDataSourceSinkAsync->AddFileHandleDataSource(iController, aFile);
122 void CMidiClientUtility::CBody::OpenFile(const TMMSource& aSource)
124 TRAPD(err, DoOpenFileL(aSource));
127 iMidiControllerEventMonitor->SelfComplete(err);
132 void CMidiClientUtility::CBody::DoOpenFileL(const TMMSource& aSource)
136 iSource = CMMFileSourceSink::NewL(KUidMmfFileSource, aSource);
137 static_cast<CMMFileSourceSink*>(iSource)->EvaluateIntentL( aSource.Intent() );
138 iAddDataSourceSinkAsync->AddDataSource(iController,
139 iSource->SourceSinkUid(),
140 iSource->SourceSinkData());
143 void CMidiClientUtility::CBody::OpenDes(const TDesC8& aDescriptor)
145 TMMFDescriptorConfig sourceCfg;
146 sourceCfg().iDes = (TAny*)&aDescriptor;
147 sourceCfg().iDesThreadId = RThread().Id();
148 // Add the data source to the controller. MmcuoStateChanged will be call on completition.
149 iAddDataSourceSinkAsync->AddDataSource(iController, KUidMmfDescriptorSource, sourceCfg);
152 void CMidiClientUtility::CBody::OpenUrl(const TDesC& aUrl,TInt aIapId,const TDesC8& /*aMimeType*/)
154 TRAPD(err, DoOpenUrlL(aUrl, aIapId));
158 iMidiControllerEventMonitor->SelfComplete(err);
163 void CMidiClientUtility::CBody::DoOpenUrlL(const TDesC& aUrl,TInt aIapId)
165 CMMFUrlParams* sourceCfg = CMMFUrlParams::NewLC(aUrl, aIapId);
166 CBufFlat* sourceCfgBuffer = sourceCfg->ExternalizeToCBufFlatLC();
167 // Add the data source to the controller. MmcuoStateChanged will be call on completition.
168 iAddDataSourceSinkAsync->AddDataSource(iController, KUidMmfUrlSource, sourceCfgBuffer->Ptr(0));
169 CleanupStack::PopAndDestroy(2, sourceCfg);//sourceCfgBuffer, sourceCfg
172 void CMidiClientUtility::CBody::Close()
174 iMidiControllerCommands.Close();
177 void CMidiClientUtility::CBody::Play()
179 TInt err = iController.Prime();
182 err=iController.Play();
186 iMidiControllerEventMonitor->SelfComplete(err);
190 void CMidiClientUtility::CBody::Stop(const TTimeIntervalMicroSeconds& aFadeOutDuration)
192 iMidiControllerCommands.Stop(aFadeOutDuration);
197 * Returns the current state of the MIDI client utility
198 * with regard to MIDI resources.
200 * @return "TMidiState" The current state of the utility
205 TMidiState CMidiClientUtility::CBody::State() const
210 void CMidiClientUtility::CBody::PlayNoteL(TInt aChannel,TInt aNote,const TTimeIntervalMicroSeconds& aDuration,TInt aNoteOnVelocity,TInt aNoteOffVelocity)
212 if((aChannel >= KMinChannel && aChannel <= KMaxChannel)
213 && (aNote >= KMinNote && aNote <= KMaxNote)
214 && (aNoteOnVelocity >= KMinNoteOnVelocity && aNoteOnVelocity <= KMaxNoteOnVelocity)
215 && (aNoteOffVelocity >= KMinNoteOffVelocity && aNoteOffVelocity <= KMaxNoteOffVelocity))
217 User::LeaveIfError(iMidiControllerCommands.PlayNote(aChannel, aNote, aDuration, aNoteOnVelocity, aNoteOffVelocity));
221 User::Leave(KErrArgument);
225 void CMidiClientUtility::CBody::PlayNoteL(TInt aChannel,TInt aNote, const TTimeIntervalMicroSeconds& aStartTime, const TTimeIntervalMicroSeconds& aDuration, TInt aNoteOnVelocity, TInt aNoteOffVelocity)
227 if((aChannel >= KMinChannel && aChannel <= KMaxChannel)
228 && (aNote >= KMinNote && aNote <= KMaxNote)
229 && (aNoteOnVelocity >= KMinNoteOnVelocity && aNoteOnVelocity <= KMaxNoteOnVelocity)
230 && (aNoteOffVelocity >= KMinNoteOffVelocity && aNoteOffVelocity <= KMaxNoteOffVelocity))
232 User::LeaveIfError(iMidiControllerCommands.PlayNote(aChannel, aNote, aStartTime, aDuration, aNoteOnVelocity, aNoteOffVelocity));
236 User::Leave(KErrArgument);
240 void CMidiClientUtility::CBody::StopNotes(TInt aChannel)
242 if(aChannel >= KMinChannel && aChannel <= KMaxChannel)
244 iMidiControllerCommands.StopNotes(aChannel);
248 void CMidiClientUtility::CBody::NoteOnL(TInt aChannel,TInt aNote,TInt aVelocity)
250 if((aChannel >= KMinChannel && aChannel <= KMaxChannel)
251 && (aNote >= KMinNote && aNote <= KMaxNote)
252 && (aVelocity >= KMinNoteOnVelocity && aVelocity <= KMaxNoteOnVelocity))
254 User::LeaveIfError(iMidiControllerCommands.NoteOn(aChannel, aNote, aVelocity));
258 User::Leave(KErrArgument);
262 void CMidiClientUtility::CBody::NoteOffL(TInt aChannel,TInt aNote,TInt aVelocity)
264 if((aChannel >= KMinChannel && aChannel <= KMaxChannel)
265 && (aNote >= KMinNote && aNote <= KMaxNote)
266 && (aVelocity >= KMinNoteOffVelocity && aVelocity <= KMaxNoteOffVelocity))
268 User::LeaveIfError(iMidiControllerCommands.NoteOff(aChannel, aNote, aVelocity));
272 User::Leave(KErrArgument);
276 TInt CMidiClientUtility::CBody::PlaybackRateL() const
279 User::LeaveIfError(iMidiControllerCommands.PlaybackRate(rate));
283 void CMidiClientUtility::CBody::SetPlaybackRateL(TInt aRate)
285 User::LeaveIfError(iMidiControllerCommands.SetPlaybackRate(aRate));
288 TInt CMidiClientUtility::CBody::MaxPlaybackRateL() const
291 User::LeaveIfError(iMidiControllerCommands.MaxPlaybackRate(maxRate));
295 TInt CMidiClientUtility::CBody::MinPlaybackRateL() const
298 User::LeaveIfError(iMidiControllerCommands.MinPlaybackRate(minRate));
303 TInt CMidiClientUtility::CBody::TempoMicroBeatsPerMinuteL() const
305 TInt microBeatsPerMinute;
306 User::LeaveIfError(iMidiControllerCommands.TempoMicroBeatsPerMinute(microBeatsPerMinute));
307 return microBeatsPerMinute;
310 void CMidiClientUtility::CBody::SetTempoL(TInt aMicroBeatsPerMinute)
312 if(aMicroBeatsPerMinute > 0)
314 User::LeaveIfError(iMidiControllerCommands.SetTempo(aMicroBeatsPerMinute));
318 User::Leave(KErrArgument);
322 TInt CMidiClientUtility::CBody::PitchTranspositionCentsL() const
325 User::LeaveIfError(iMidiControllerCommands.PitchTranspositionCents(cents));
329 TInt CMidiClientUtility::CBody::SetPitchTranspositionL(TInt aCents)
331 TInt pitchApplied = 0;
332 //we do not check aCents value - it is expected the controller will report KErrArgument
333 //if the pitch level is not supported.
334 User::LeaveIfError(iMidiControllerCommands.SetPitchTransposition(aCents, pitchApplied));
339 TTimeIntervalMicroSeconds CMidiClientUtility::CBody::DurationMicroSecondsL() const
341 TTimeIntervalMicroSeconds duration;
342 User::LeaveIfError(iController.GetDuration(duration));
346 TInt64 CMidiClientUtility::CBody::DurationMicroBeatsL() const
349 User::LeaveIfError(iMidiControllerCommands.DurationMicroBeats(duration));
353 TInt CMidiClientUtility::CBody::NumTracksL() const
356 User::LeaveIfError(iMidiControllerCommands.NumTracks(tracks));
360 void CMidiClientUtility::CBody::SetTrackMuteL(TInt aTrack, TBool aMuted) const
362 TInt numTracks = NumTracksL();
363 if((aTrack >= 0) && (aTrack < numTracks))
365 User::LeaveIfError(iMidiControllerCommands.SetTrackMute(aTrack, aMuted));
369 User::Leave(KErrArgument);
373 const TDesC8& CMidiClientUtility::CBody::MimeTypeL()
375 TPtr8 des = iMimeType->Des();
376 User::LeaveIfError(iMidiControllerCommands.MimeType(des));
380 TTimeIntervalMicroSeconds CMidiClientUtility::CBody::PositionMicroSecondsL() const
382 TTimeIntervalMicroSeconds position;
383 User::LeaveIfError(iController.GetPosition(position));
387 void CMidiClientUtility::CBody::SetPositionMicroSecondsL(const TTimeIntervalMicroSeconds& aPosition)
389 TTimeIntervalMicroSeconds maxPosition = DurationMicroSecondsL();
390 TTimeIntervalMicroSeconds minPosition(0);
392 TTimeIntervalMicroSeconds position = aPosition;
393 if (aPosition > maxPosition)
395 position = maxPosition;
397 if (aPosition < minPosition)
399 position = minPosition;
401 User::LeaveIfError(iController.SetPosition(position));
404 TInt64 CMidiClientUtility::CBody::PositionMicroBeatsL() const
407 User::LeaveIfError(iMidiControllerCommands.PositionMicroBeats(position));
411 void CMidiClientUtility::CBody::SetPositionMicroBeatsL(TInt64 aMicroBeats)
413 TInt64 maxPosition = DurationMicroBeatsL();
414 TInt64 minPosition(0);
416 TInt64 position = aMicroBeats;
417 if (aMicroBeats > maxPosition)
419 position = maxPosition;
421 if (aMicroBeats < minPosition)
423 position = minPosition;
425 User::LeaveIfError(iMidiControllerCommands.SetPositionMicroBeats(position));
428 void CMidiClientUtility::CBody::SetSyncUpdateCallbackIntervalL(const TTimeIntervalMicroSeconds& aMicroSeconds, TInt64 aMicroBeats)
431 if((aMicroSeconds > TTimeIntervalMicroSeconds(0)) || (aMicroSeconds == TTimeIntervalMicroSeconds(0) && aMicroBeats == 0))
433 iIntervalSec = ETrue;
439 iIntervalSec = EFalse;
443 User::Leave(KErrArgument);
447 User::LeaveIfError(iMidiControllerCommands.SetSyncUpdateCallbackInterval(aMicroSeconds, aMicroBeats));
450 TInt CMidiClientUtility::CBody::SendMessageL(const TDesC8& aMidiMessage)
453 User::LeaveIfError(iMidiControllerCommands.SendMessage(aMidiMessage, numByteProc));
457 TInt CMidiClientUtility::CBody::SendMessageL(const TDesC8& aMidiMessage,const TTimeIntervalMicroSeconds& aTime)
460 User::LeaveIfError(iMidiControllerCommands.SendMessage(aMidiMessage, aTime, numByteProc));
464 void CMidiClientUtility::CBody::SendMipMessageL(const RArray<TMipMessageEntry>& aEntry)
466 User::LeaveIfError(iMidiControllerCommands.SendMipMessage(aEntry));
469 TInt CMidiClientUtility::CBody::NumberOfBanksL(TBool aCustom) const
472 User::LeaveIfError(iMidiControllerCommands.NumberOfBanks(aCustom, numBanks));
476 TInt CMidiClientUtility::CBody::GetBankIdL(TBool aCustom,TInt aBankIndex) const
478 TInt numBanks = NumberOfBanksL(aCustom);
480 if(aBankIndex >= 0 && aBankIndex < numBanks)
482 User::LeaveIfError(iMidiControllerCommands.GetBankId(aCustom, aBankIndex, bankId));
486 User::Leave(KErrArgument);
491 void CMidiClientUtility::CBody::LoadCustomBankL(const TDesC& aFileName,TInt& aBankCollectionIndex)
493 User::LeaveIfError(iMidiControllerCommands.LoadCustomBank(aFileName, aBankCollectionIndex));
496 void CMidiClientUtility::CBody::UnloadCustomBankL(TInt aBankCollectionIndex)
498 User::LeaveIfError(iMidiControllerCommands.UnloadCustomBank(aBankCollectionIndex));
501 TBool CMidiClientUtility::CBody::CustomBankLoadedL(TInt aBankCollectionIndex) const
504 User::LeaveIfError(iMidiControllerCommands.CustomBankLoaded(aBankCollectionIndex, bankLoaded));
508 void CMidiClientUtility::CBody::UnloadAllCustomBanksL()
510 User::LeaveIfError(iMidiControllerCommands.UnloadAllCustomBanks());
513 TInt CMidiClientUtility::CBody::NumberOfInstrumentsL(TInt aBankId,TBool aCustom) const
516 User::LeaveIfError(iMidiControllerCommands.NumberOfInstruments(aBankId, aCustom, numInstruments));
517 return numInstruments;
520 TInt CMidiClientUtility::CBody::GetInstrumentIdL(TInt aBankId,TBool aCustom,TInt aInstrumentIndex) const
522 TInt numInstruments = NumberOfInstrumentsL(aBankId, aCustom);
523 TInt instrumentId = 0;
524 if(aInstrumentIndex >=0 && aInstrumentIndex < numInstruments)
526 User::LeaveIfError(iMidiControllerCommands.GetInstrumentId(aBankId, aCustom, aInstrumentIndex, instrumentId));
530 User::Leave(KErrArgument);
535 HBufC* CMidiClientUtility::CBody::InstrumentNameL(TInt aBankId, TBool aCustom, TInt aInstrumentId) const
537 HBufC* instrumentName = NULL;
539 if(aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId)
541 instrumentName = iMidiControllerCommands.InstrumentNameL(aBankId, aCustom, aInstrumentId);
545 User::Leave(KErrArgument);
548 return instrumentName;
551 void CMidiClientUtility::CBody::SetInstrumentL(TInt aChannel,TInt aBankId,TInt aInstrumentId)
553 if((aChannel >= KMinChannel && aChannel <= KMaxChannel)
554 && (aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId))
556 User::LeaveIfError(iMidiControllerCommands.SetInstrument(aChannel, aBankId, aInstrumentId));
560 User::Leave(KErrArgument);
564 void CMidiClientUtility::CBody::LoadCustomInstrumentL(const TDesC& aFileName, TInt aFileBankId, TInt aFileInstrumentId, TInt aMemoryBankId, TInt aMemoryInstrumentId)
566 if((aFileInstrumentId >= KMinInstrumentId && aFileInstrumentId <= KMaxInstrumentId)
567 && (aMemoryInstrumentId >= KMinInstrumentId && aMemoryInstrumentId <= KMaxInstrumentId))
569 User::LeaveIfError(iMidiControllerCommands.LoadCustomInstrument(aFileName, aFileBankId, aFileInstrumentId, aMemoryBankId, aMemoryInstrumentId));
573 User::Leave(KErrArgument);
577 void CMidiClientUtility::CBody::UnloadCustomInstrumentL(TInt aCustomBankId,TInt aInstrumentId)
579 if(aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId)
581 User::LeaveIfError(iMidiControllerCommands.UnloadCustomInstrument(aCustomBankId, aInstrumentId));
585 User::Leave(KErrArgument);
589 HBufC* CMidiClientUtility::CBody::PercussionKeyNameL(TInt aNote, TInt aBankId, TBool aCustom, TInt aInstrumentId) const
591 HBufC* pKeyName = NULL;
593 if((aNote >= KMinNote && aNote <= KMaxNote)
594 && (aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId))
596 pKeyName = iMidiControllerCommands.PercussionKeyNameL(aNote, aBankId, aCustom, aInstrumentId);
600 User::Leave(KErrArgument);
606 void CMidiClientUtility::CBody::StopTimeL(TTimeIntervalMicroSeconds& aStopTime) const
608 User::LeaveIfError(iMidiControllerCommands.StopTime(aStopTime));
611 void CMidiClientUtility::CBody::SetStopTimeL(const TTimeIntervalMicroSeconds& aStopTime)
613 TTimeIntervalMicroSeconds duration = DurationMicroSecondsL();
614 if(aStopTime >= TTimeIntervalMicroSeconds(0) && aStopTime <= duration)
616 User::LeaveIfError(iMidiControllerCommands.SetStopTime(aStopTime));
620 User::Leave(KErrArgument);
624 void CMidiClientUtility::CBody::SetRepeatsL(TInt aRepeatNumberOfTimes, const TTimeIntervalMicroSeconds& aTrailingSilence)
626 if((aRepeatNumberOfTimes >= 0) && (aTrailingSilence >= TTimeIntervalMicroSeconds(0)))
628 User::LeaveIfError(iMidiControllerCommands.SetRepeats(aRepeatNumberOfTimes, aTrailingSilence));
632 User::Leave(KErrArgument);
636 TInt CMidiClientUtility::CBody::PolyphonyL() const
639 TInt maxPoly = MaxPolyphonyL();
640 User::LeaveIfError(iMidiControllerCommands.Polyphony(numNotes));
641 if(maxPoly <= numNotes)
651 TInt CMidiClientUtility::CBody::MaxPolyphonyL() const
654 User::LeaveIfError(iMidiControllerCommands.MaxPolyphony(maxNotes));
658 TInt CMidiClientUtility::CBody::ChannelsSupportedL() const
661 User::LeaveIfError(iMidiControllerCommands.ChannelsSupported(channels));
665 TReal32 CMidiClientUtility::CBody::ChannelVolumeL(TInt aChannel) const
668 if(aChannel >= KMinChannel && aChannel <= KMaxChannel)
670 User::LeaveIfError(iMidiControllerCommands.ChannelVolume(aChannel, channelVol));
674 User::Leave(KErrArgument);
679 TReal32 CMidiClientUtility::CBody::MaxChannelVolumeL() const
682 User::LeaveIfError(iMidiControllerCommands.MaxChannelVolume(maxChanVol));
686 void CMidiClientUtility::CBody::SetChannelVolumeL(TInt aChannel,TReal32 aVolume)
688 TReal32 maxChanVol = MaxChannelVolumeL();
689 if((aChannel >= KMinChannel && aChannel <= KMaxChannel) && aVolume <= maxChanVol)
691 User::LeaveIfError(iMidiControllerCommands.SetChannelVolume(aChannel, aVolume));
695 User::Leave(KErrArgument);
699 void CMidiClientUtility::CBody::SetChannelMuteL(TInt aChannel,TBool aMuted)
701 if(aChannel >= KMinChannel && aChannel <= KMaxChannel)
703 User::LeaveIfError(iMidiControllerCommands.SetChannelMute(aChannel, aMuted));
707 User::Leave(KErrArgument);
711 TInt CMidiClientUtility::CBody::VolumeL() const
714 User::LeaveIfError(iMidiControllerCommands.Volume(vol));
718 TInt CMidiClientUtility::CBody::MaxVolumeL() const
721 User::LeaveIfError(iMidiControllerCommands.MaxVolume(maxVol));
725 void CMidiClientUtility::CBody::SetVolumeL(TInt aVolume)
727 User::LeaveIfError(iMidiControllerCommands.SetVolume(aVolume));
730 void CMidiClientUtility::CBody::SetVolumeRampL(const TTimeIntervalMicroSeconds& aRampDuration)
732 User::LeaveIfError(iMidiControllerCommands.SetVolumeRamp(aRampDuration));
736 TInt CMidiClientUtility::CBody::GetBalanceL() const
739 User::LeaveIfError(iMidiControllerCommands.GetBalance(balance));
743 void CMidiClientUtility::CBody::SetBalanceL(TInt aBalance)
745 User::LeaveIfError(iMidiControllerCommands.SetBalance(aBalance));
748 void CMidiClientUtility::CBody::SetPriorityL(TInt aPriority, TInt aPref)
750 TMMFPrioritySettings priority;
751 priority.iPriority = aPriority;
752 priority.iPref = aPref;
754 User::LeaveIfError(iController.SetPrioritySettings(priority));
757 TInt CMidiClientUtility::CBody::NumberOfMetaDataEntriesL() const
760 User::LeaveIfError(iController.GetNumberOfMetaDataEntries(numMetaData));
764 CMMFMetaDataEntry* CMidiClientUtility::CBody::GetMetaDataEntryL(TInt aMetaDataIndex) const
766 CMMFMetaDataEntry* metaDataEntry = iController.GetMetaDataEntryL(aMetaDataIndex);
767 return metaDataEntry;
770 void CMidiClientUtility::CBody::CustomCommandSyncL(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2, TDes8& aDataFrom)
772 User::LeaveIfError(iController.CustomCommandSync(aDestination, aFunction, aDataTo1, aDataTo2, aDataFrom));
775 void CMidiClientUtility::CBody::CustomCommandSyncL(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2)
777 User::LeaveIfError(iController.CustomCommandSync(aDestination, aFunction, aDataTo1, aDataTo2));
780 void CMidiClientUtility::CBody::CustomCommandAsync(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2, TDes8& aDataFrom, TRequestStatus& aStatus)
782 iController.CustomCommandAsync(aDestination, aFunction, aDataTo1, aDataTo2, aDataFrom, aStatus);
785 void CMidiClientUtility::CBody::CustomCommandAsync(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2, TRequestStatus& aStatus)
787 iController.CustomCommandAsync(aDestination, aFunction, aDataTo1, aDataTo2, aStatus);
790 MMMFDRMCustomCommand* CMidiClientUtility::CBody::GetDRMCustomCommand()
792 if (iDRMCustomCommands.IsSupported())
794 return static_cast<MMMFDRMCustomCommand*>(&iDRMCustomCommands);
799 TInt CMidiClientUtility::CBody::DoOpen(const RMMFControllerImplInfoArray& aControllers, TUid aSinkUid, const TDesC8& aSinkData, TBool aUseSharedHeap)
801 // Make sure any existing controller is closed.
802 iMidiControllerEventMonitor->Cancel();
805 // Try opening and configuring each controller in turn
806 TInt error = KErrNotSupported;
810 // Break if we're at the end of the array of controllers
811 if (index >= aControllers.Count())
814 // Open the controller
815 error = iController.Open(aControllers[index]->Uid(), iPrioritySettings, aUseSharedHeap);
817 // If the controller was opened without error, start receiving events from it.
820 iMidiControllerEventMonitor->Start();
825 error = iController.AddDataSink(aSinkUid, aSinkData, iSinkHandle);
827 // If an error occurred in any of the above, close the controller.
830 iMidiControllerEventMonitor->Cancel();
841 void CMidiClientUtility::CBody::HandleMidiEvent(const CMMFMidiEvent& aEvent)
843 if(aEvent.iEventType == KMMFEventCategoryMidiOpenDataSourceComplete ||
844 aEvent.iEventType == KMMFEventCategoryMidiClose ||
845 aEvent.iEventType == KMMFEventCategoryMidiPrime ||
846 aEvent.iEventType == KMMFEventCategoryMidiPlaying ||
847 aEvent.iEventType == KMMFEventCategoryMidiPlaybackIncompatible ||
848 aEvent.iEventType == KMMFEventCategoryMidiPlaybackSilent)
850 iState = aEvent.iNewState;
851 iObserver.MmcuoStateChanged(aEvent.iOldState, aEvent.iNewState, aEvent.iMicroSeconds, aEvent.iErrorCode);
853 if (aEvent.iEventType == KMMFEventCategoryMidiClose)
855 if (iSourceHandle.DestinationHandle())
857 iController.RemoveDataSource(iSourceHandle);
861 else if(aEvent.iEventType == KMMFEventCategoryMidiPlayingComplete)
863 iState = aEvent.iNewState;
864 iObserver.MmcuoStateChanged(aEvent.iOldState, aEvent.iNewState, aEvent.iMicroSeconds, aEvent.iErrorCode);
866 else if(aEvent.iEventType == KMMFEventCategoryMidiSyncUpdate)
868 iObserver.MmcuoSyncUpdate(aEvent.iMicroSeconds, aEvent.iMicroBeats);
870 else if(aEvent.iEventType == KMMFEventCategoryTempoChanged)
872 iObserver.MmcuoTempoChanged(aEvent.iTempoMicroBeats);
874 else if(aEvent.iEventType == KMMFEventCategoryVolumeChanged)
876 iObserver.MmcuoVolumeChanged(aEvent.iChannel, aEvent.iVolumeInDecibels);
878 else if(aEvent.iEventType == KMMFEventCategoryMuteChanged)
880 iObserver.MmcuoMuteChanged(aEvent.iChannel, aEvent.iMute);
882 else if(aEvent.iEventType == KMMFEventCategoryMetaDataEntryFound)
884 iObserver.MmcuoMetaDataEntryFound(aEvent.iMetaDataEntryId, aEvent.iMicroSeconds);
886 else if(aEvent.iEventType == KMMFEventCategoryMipMessageReceived)
888 iObserver.MmcuoMipMessageReceived(aEvent.iMipMessage);
890 else if(aEvent.iEventType == KMMFEventCategoryPolyphonyChanged)
892 iObserver.MmcuoPolyphonyChanged(aEvent.iPolyphony);
894 else if(aEvent.iEventType == KMMFEventCategoryInstrumentChanged)
896 iObserver.MmcuoInstrumentChanged(aEvent.iChannel,aEvent.iBankId,aEvent.iInstrumentId);
898 else if((iState == EMidiStateOpenPlaying) || (iState == EMidiStatePlaybackIncompatible) ||
899 (iState == EMidiStatePlaybackSilent) || (iState == EMidiStateClosedEngaged) ||
900 (iState == EMidiStateOpenEngaged))
902 iState = aEvent.iNewState;
903 iObserver.MmcuoStateChanged(aEvent.iOldState, aEvent.iNewState, aEvent.iMicroSeconds, aEvent.iErrorCode);
906 else if (aEvent.iEventType == KMMFErrorCategoryControllerGeneralError)
908 iObserver.MmcuoStateChanged(iState, iState, TTimeIntervalMicroSeconds(0), aEvent.iErrorCode);
912 // xxx - what do we do when we don't understand the error type?
918 * Used to change the value of MaxPolyphonyL()
920 void CMidiClientUtility::CBody::SetMaxPolyphonyL(TInt aMaxNotes)
922 User::LeaveIfError(iMidiControllerCommands.SetMaxPolyphony(aMaxNotes));
925 TInt CMidiClientUtility::CBody::GetRepeats()
928 iMidiControllerCommands.GetRepeats(numRepeats);
932 void CMidiClientUtility::CBody::LoadCustomBankDataL(const TDesC8& aBankData,TInt& aBankId)
934 User::LeaveIfError(iMidiControllerCommands.LoadCustomBankData(aBankData, aBankId));
937 void CMidiClientUtility::CBody::LoadCustomInstrumentDataL(const TDesC8& aInstrumentData, TInt aBankDataId, TInt aInstrumentDataId, TInt aMemoryBankId, TInt aMemoryInstrumentId)
939 if((aInstrumentDataId >= KMinInstrumentId && aInstrumentDataId <= KMaxInstrumentId)
940 && (aMemoryInstrumentId >= KMinInstrumentId && aMemoryInstrumentId <= KMaxInstrumentId))
942 User::LeaveIfError(iMidiControllerCommands.LoadCustomInstrumentData(aInstrumentData, aBankDataId, aInstrumentDataId, aMemoryBankId, aMemoryInstrumentId));
946 User::Leave(KErrArgument);
950 void CMidiClientUtility::CBody::SetBankL(TBool aCustom)
952 User::LeaveIfError(iMidiControllerCommands.SetBank(aCustom));
955 TBool CMidiClientUtility::CBody::IsTrackMuteL(TInt aTrack) const
958 User::LeaveIfError(iMidiControllerCommands.IsTrackMute(aTrack, mute));
962 TBool CMidiClientUtility::CBody::IsChannelMuteL(TInt aChannel) const
965 if (aChannel >= KMinChannel && aChannel <= KMaxChannel)
967 User::LeaveIfError(iMidiControllerCommands.IsChannelMute(aChannel, mute));
971 User::Leave(KErrArgument);
977 void CMidiClientUtility::CBody::GetInstrumentL(TInt aChannel, TInt& aInstrumentId, TInt& aBankId)
979 if (aChannel >= KMinChannel && aChannel <= KMaxChannel)
981 User::LeaveIfError(iMidiControllerCommands.GetInstrument(aChannel, aInstrumentId, aBankId));
985 User::Leave(KErrArgument);
989 void CMidiClientUtility::CBody::RepeatTrailingSilenceTimerComplete()
994 CRepeatTrailingSilenceTimer* CRepeatTrailingSilenceTimer::NewL(MRepeatTrailingSilenceTimerObs& aObs)
996 CRepeatTrailingSilenceTimer* s = new(ELeave) CRepeatTrailingSilenceTimer(aObs);
997 CleanupStack::PushL(s);
1003 void CRepeatTrailingSilenceTimer::RunL()
1005 iObs.RepeatTrailingSilenceTimerComplete();
1008 CRepeatTrailingSilenceTimer::CRepeatTrailingSilenceTimer(MRepeatTrailingSilenceTimerObs& aObs) :
1009 CTimer(EPriorityHigh),
1012 CActiveScheduler::Add(this);
1020 CMidiControllerEventMonitor* CMidiControllerEventMonitor::NewL(MMidiControllerEventMonitorObserver& aMidiObserver,
1021 RMidiControllerCustomCommands& aMidiControllerCustomCommands, const CMidiClientUtility& aParent)
1023 CMidiControllerEventMonitor* self = new(ELeave) CMidiControllerEventMonitor(aMidiObserver, aMidiControllerCustomCommands, aParent);
1024 CleanupStack::PushL(self);
1026 CleanupStack::Pop(self);
1030 void CMidiControllerEventMonitor::ConstructL()
1032 iMidiEvent = new (ELeave) CMMFMidiEvent();
1035 CMidiControllerEventMonitor::CMidiControllerEventMonitor(MMidiControllerEventMonitorObserver& aMidiObserver,
1036 RMidiControllerCustomCommands& aMidiControllerCustomCommands, const CMidiClientUtility& aParent) :
1037 CActive(EPriorityStandard),
1038 iMidiObserver(aMidiObserver),
1039 iMidiControllerCustomCommands(aMidiControllerCustomCommands),
1042 CActiveScheduler::Add(this);
1045 CMidiControllerEventMonitor::~CMidiControllerEventMonitor()
1052 Start receiving events from the controller.
1054 This can only be called once the controller is open.
1056 void CMidiControllerEventMonitor::Start()
1058 iMidiControllerCustomCommands.ReceiveEvents(iSizeOfEvent, iStatus);
1062 void CMidiControllerEventMonitor::RunL()
1064 User::LeaveIfError(iStatus.Int());
1066 // Create a buffer big enough to hold the event, then retrieve it from the server
1067 HBufC8* buf = HBufC8::NewLC(iSizeOfEvent());
1068 TPtr8 bufPtr = buf->Des();
1069 User::LeaveIfError(iMidiControllerCustomCommands.RetrieveEvent(bufPtr));
1071 // Now internalize a CMMFMidiEvent with the info in the buffer
1072 RDesReadStream stream(bufPtr);
1073 CleanupClosePushL(stream);
1075 CMMFMidiEvent* theEvent = new (ELeave) CMMFMidiEvent();
1077 CleanupStack::PushL(theEvent);
1078 theEvent->InternalizeL(stream);
1080 iMidiObserver.HandleMidiEvent(*theEvent);
1083 CleanupStack::PopAndDestroy(3);//buf, stream, theEvent
1086 void CMidiControllerEventMonitor::SelfComplete(TInt aError)
1089 TRequestStatus *status = &iStatus;
1092 User::RequestComplete(status, aError);
1095 void CMidiControllerEventMonitor::DoCancel()
1097 iMidiControllerCustomCommands.CancelReceiveEvents();
1100 TInt CMidiControllerEventMonitor::RunError(TInt aError)
1102 iMidiEvent->iEventType = KMMFErrorCategoryControllerGeneralError;
1103 iMidiEvent->iErrorCode = aError;
1104 iMidiEvent->iOldState = iParent.State();
1105 iMidiEvent->iNewState = iMidiEvent->iOldState;
1107 iMidiObserver.HandleMidiEvent(*iMidiEvent);