os/mm/mmlibs/mmfw/MIDI/src/midiclientutilitybody.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "midiclientutilitybody.h"
sl@0
    17
sl@0
    18
const TInt KMimeLength         = 256;
sl@0
    19
const TInt KMinChannel         = 0;
sl@0
    20
const TInt KMaxChannel         = 15;
sl@0
    21
const TInt KMinNote            = 0;
sl@0
    22
const TInt KMaxNote            = 127;
sl@0
    23
const TInt KMinNoteOnVelocity  = 0;
sl@0
    24
const TInt KMaxNoteOnVelocity  = 127;
sl@0
    25
const TInt KMinNoteOffVelocity = 0;
sl@0
    26
const TInt KMaxNoteOffVelocity = 127;
sl@0
    27
const TInt KMinInstrumentId    = 0;
sl@0
    28
const TInt KMaxInstrumentId    = 127;
sl@0
    29
sl@0
    30
sl@0
    31
CMidiClientUtility::CBody* CMidiClientUtility::CBody::NewL(CMidiClientUtility* aParent,
sl@0
    32
														   MMidiClientUtilityObserver& aObserver, 
sl@0
    33
														   TInt aPriority, 
sl@0
    34
														   TInt aPref,
sl@0
    35
														   TBool aUseSharedHeap)
sl@0
    36
	{
sl@0
    37
	CBody* self = new(ELeave) CBody(aParent, aObserver, aPriority, aPref);
sl@0
    38
	CleanupStack::PushL(self);
sl@0
    39
	self->ConstructL(aUseSharedHeap);
sl@0
    40
	CleanupStack::Pop(self);
sl@0
    41
	return self;
sl@0
    42
	}
sl@0
    43
sl@0
    44
CMidiClientUtility::CBody::CBody(CMidiClientUtility* aParent, 
sl@0
    45
								 MMidiClientUtilityObserver& aObserver, 
sl@0
    46
								 TInt aPriority, 
sl@0
    47
								 TInt aPref) :
sl@0
    48
	iObserver(aObserver),
sl@0
    49
	iMidiControllerCommands(iController),
sl@0
    50
	iDRMCustomCommands(iController)
sl@0
    51
sl@0
    52
	{
sl@0
    53
	iParent = aParent;
sl@0
    54
	iState = EMidiStateClosedDisengaged;
sl@0
    55
	iPrioritySettings.iPriority = aPriority;
sl@0
    56
	iPrioritySettings.iPref = aPref;
sl@0
    57
	iIntervalSec = ETrue;
sl@0
    58
	iStopPosition = TTimeIntervalMicroSeconds(0);
sl@0
    59
	}
sl@0
    60
sl@0
    61
CMidiClientUtility::CBody::~CBody()
sl@0
    62
	{
sl@0
    63
	delete iAddDataSourceSinkAsync;
sl@0
    64
	if (iMidiControllerEventMonitor)
sl@0
    65
		iMidiControllerEventMonitor->Cancel();
sl@0
    66
		
sl@0
    67
	iController.Close();
sl@0
    68
	delete iMidiControllerEventMonitor;
sl@0
    69
	delete iMimeType;
sl@0
    70
	delete iRepeatTrailingSilenceTimer;
sl@0
    71
	delete iSource;
sl@0
    72
	}
sl@0
    73
sl@0
    74
void CMidiClientUtility::CBody::ConstructL(TBool aUseSharedHeap)
sl@0
    75
	{
sl@0
    76
	iMidiControllerEventMonitor = CMidiControllerEventMonitor::NewL(*this, iMidiControllerCommands, *iParent);
sl@0
    77
	iMimeType = HBufC8::NewL(KMimeLength);
sl@0
    78
		
sl@0
    79
	RMMFControllerImplInfoArray controllers;
sl@0
    80
	CleanupResetAndDestroyPushL(controllers);
sl@0
    81
	CMMFControllerPluginSelectionParameters* cSelect = CMMFControllerPluginSelectionParameters::NewLC();
sl@0
    82
sl@0
    83
	// Select the media IDs to allow
sl@0
    84
	RArray<TUid> mediaIds;
sl@0
    85
	CleanupClosePushL(mediaIds);
sl@0
    86
	User::LeaveIfError(mediaIds.Append(KUidMediaTypeMidi));
sl@0
    87
	cSelect->SetMediaIdsL(mediaIds,CMMFPluginSelectionParameters::EAllowOnlySuppliedMediaIds);
sl@0
    88
	CleanupStack::PopAndDestroy();//mediaIds
sl@0
    89
	cSelect->ListImplementationsL(controllers);
sl@0
    90
	// Open and configure a controller
sl@0
    91
	User::LeaveIfError(DoOpen(controllers, KUidMmfAudioOutput, KNullDesC8, aUseSharedHeap));
sl@0
    92
	CleanupStack::PopAndDestroy(2);//controllers, cSelect
sl@0
    93
	iRepeatTrailingSilenceTimer = CRepeatTrailingSilenceTimer::NewL(*this);
sl@0
    94
	iAddDataSourceSinkAsync = CMMFAddDataSourceSinkAsync::NewL(*this);
sl@0
    95
	}
sl@0
    96
sl@0
    97
void CMidiClientUtility::CBody::MadssaoAddDataSourceSinkAsyncComplete(TInt aError, const TMMFMessageDestination& aHandle)
sl@0
    98
	{
sl@0
    99
	if (aError == KErrNone)
sl@0
   100
		{
sl@0
   101
		iSourceHandle = aHandle;
sl@0
   102
		}
sl@0
   103
	else
sl@0
   104
		{
sl@0
   105
 		iMidiControllerEventMonitor->SelfComplete(aError);
sl@0
   106
		}
sl@0
   107
	}
sl@0
   108
void CMidiClientUtility::CBody::OpenFile(const TDesC& aFileName)
sl@0
   109
	{
sl@0
   110
	TMMFFileConfig sourceCfg;
sl@0
   111
	sourceCfg().iPath = aFileName;
sl@0
   112
	// Add the data source to the controller. MmcuoStateChanged will be call on completition.
sl@0
   113
	iAddDataSourceSinkAsync->AddDataSource(iController, KUidMmfFileSource, sourceCfg);
sl@0
   114
	}
sl@0
   115
sl@0
   116
void CMidiClientUtility::CBody::OpenFile(RFile& aFile)
sl@0
   117
	{
sl@0
   118
		// Add the data source to the controller. MmcuoStateChanged will be call on completition.
sl@0
   119
	iAddDataSourceSinkAsync->AddFileHandleDataSource(iController, aFile);
sl@0
   120
	}
sl@0
   121
sl@0
   122
void CMidiClientUtility::CBody::OpenFile(const TMMSource& aSource)
sl@0
   123
	{
sl@0
   124
	TRAPD(err, DoOpenFileL(aSource));
sl@0
   125
	if (err != KErrNone)
sl@0
   126
		{
sl@0
   127
 		iMidiControllerEventMonitor->SelfComplete(err);
sl@0
   128
		}
sl@0
   129
	}
sl@0
   130
	
sl@0
   131
	
sl@0
   132
void CMidiClientUtility::CBody::DoOpenFileL(const TMMSource& aSource)
sl@0
   133
	{
sl@0
   134
	delete iSource;
sl@0
   135
	iSource = NULL;
sl@0
   136
	iSource = CMMFileSourceSink::NewL(KUidMmfFileSource, aSource);
sl@0
   137
	static_cast<CMMFileSourceSink*>(iSource)->EvaluateIntentL( aSource.Intent() );
sl@0
   138
	iAddDataSourceSinkAsync->AddDataSource(iController, 
sl@0
   139
								iSource->SourceSinkUid(),
sl@0
   140
								iSource->SourceSinkData());
sl@0
   141
	}
sl@0
   142
sl@0
   143
void CMidiClientUtility::CBody::OpenDes(const TDesC8& aDescriptor)
sl@0
   144
	{
sl@0
   145
	TMMFDescriptorConfig sourceCfg;
sl@0
   146
	sourceCfg().iDes = (TAny*)&aDescriptor;
sl@0
   147
	sourceCfg().iDesThreadId = RThread().Id();
sl@0
   148
	// Add the data source to the controller. MmcuoStateChanged will be call on completition.
sl@0
   149
	iAddDataSourceSinkAsync->AddDataSource(iController, KUidMmfDescriptorSource, sourceCfg);
sl@0
   150
	}
sl@0
   151
sl@0
   152
void CMidiClientUtility::CBody::OpenUrl(const TDesC& aUrl,TInt aIapId,const TDesC8& /*aMimeType*/)
sl@0
   153
	{
sl@0
   154
	TRAPD(err, DoOpenUrlL(aUrl, aIapId));
sl@0
   155
sl@0
   156
	if (err != KErrNone)
sl@0
   157
		{
sl@0
   158
 		iMidiControllerEventMonitor->SelfComplete(err);
sl@0
   159
		}
sl@0
   160
sl@0
   161
	}
sl@0
   162
sl@0
   163
void CMidiClientUtility::CBody::DoOpenUrlL(const TDesC& aUrl,TInt aIapId)
sl@0
   164
	{
sl@0
   165
	CMMFUrlParams* sourceCfg = CMMFUrlParams::NewLC(aUrl, aIapId);
sl@0
   166
	CBufFlat* sourceCfgBuffer = sourceCfg->ExternalizeToCBufFlatLC();
sl@0
   167
	// Add the data source to the controller. MmcuoStateChanged will be call on completition.
sl@0
   168
	iAddDataSourceSinkAsync->AddDataSource(iController, KUidMmfUrlSource, sourceCfgBuffer->Ptr(0));
sl@0
   169
	CleanupStack::PopAndDestroy(2, sourceCfg);//sourceCfgBuffer, sourceCfg
sl@0
   170
	}
sl@0
   171
sl@0
   172
void CMidiClientUtility::CBody::Close()
sl@0
   173
	{
sl@0
   174
	iMidiControllerCommands.Close();
sl@0
   175
	}
sl@0
   176
sl@0
   177
void CMidiClientUtility::CBody::Play()
sl@0
   178
	{
sl@0
   179
	TInt err = iController.Prime();
sl@0
   180
	if (err==KErrNone)
sl@0
   181
		{
sl@0
   182
		err=iController.Play();
sl@0
   183
		}
sl@0
   184
	if (err!=KErrNone)
sl@0
   185
		{
sl@0
   186
		iMidiControllerEventMonitor->SelfComplete(err);
sl@0
   187
		}
sl@0
   188
	}
sl@0
   189
sl@0
   190
void CMidiClientUtility::CBody::Stop(const TTimeIntervalMicroSeconds& aFadeOutDuration)
sl@0
   191
	{
sl@0
   192
	iMidiControllerCommands.Stop(aFadeOutDuration);
sl@0
   193
	}
sl@0
   194
	
sl@0
   195
/**
sl@0
   196
 *
sl@0
   197
 * Returns the current state of the MIDI client utility
sl@0
   198
 * with regard to MIDI resources.
sl@0
   199
 *
sl@0
   200
 * @return "TMidiState" The current state of the utility
sl@0
   201
 *
sl@0
   202
 * @since	7.0s
sl@0
   203
 */
sl@0
   204
sl@0
   205
TMidiState CMidiClientUtility::CBody::State() const
sl@0
   206
	{
sl@0
   207
	return iState;
sl@0
   208
	}
sl@0
   209
sl@0
   210
void CMidiClientUtility::CBody::PlayNoteL(TInt aChannel,TInt aNote,const TTimeIntervalMicroSeconds& aDuration,TInt aNoteOnVelocity,TInt aNoteOffVelocity)
sl@0
   211
	{
sl@0
   212
	if((aChannel >= KMinChannel && aChannel <= KMaxChannel) 
sl@0
   213
	&& (aNote >= KMinNote && aNote <= KMaxNote) 
sl@0
   214
	&& (aNoteOnVelocity >= KMinNoteOnVelocity && aNoteOnVelocity <= KMaxNoteOnVelocity) 
sl@0
   215
	&& (aNoteOffVelocity >= KMinNoteOffVelocity && aNoteOffVelocity <= KMaxNoteOffVelocity))
sl@0
   216
		{	
sl@0
   217
		User::LeaveIfError(iMidiControllerCommands.PlayNote(aChannel, aNote, aDuration, aNoteOnVelocity, aNoteOffVelocity));
sl@0
   218
		}
sl@0
   219
	else
sl@0
   220
		{
sl@0
   221
		User::Leave(KErrArgument);
sl@0
   222
		}
sl@0
   223
	}
sl@0
   224
sl@0
   225
void CMidiClientUtility::CBody::PlayNoteL(TInt aChannel,TInt aNote, const TTimeIntervalMicroSeconds& aStartTime, const TTimeIntervalMicroSeconds& aDuration, TInt aNoteOnVelocity, TInt aNoteOffVelocity)
sl@0
   226
	{
sl@0
   227
	if((aChannel >= KMinChannel && aChannel <= KMaxChannel) 
sl@0
   228
	&& (aNote >= KMinNote && aNote <= KMaxNote) 
sl@0
   229
	&& (aNoteOnVelocity >= KMinNoteOnVelocity && aNoteOnVelocity <= KMaxNoteOnVelocity) 
sl@0
   230
	&& (aNoteOffVelocity >= KMinNoteOffVelocity && aNoteOffVelocity <= KMaxNoteOffVelocity))
sl@0
   231
		{
sl@0
   232
		User::LeaveIfError(iMidiControllerCommands.PlayNote(aChannel, aNote, aStartTime, aDuration, aNoteOnVelocity, aNoteOffVelocity));
sl@0
   233
		}
sl@0
   234
	else
sl@0
   235
		{
sl@0
   236
		User::Leave(KErrArgument);
sl@0
   237
		}
sl@0
   238
	}
sl@0
   239
sl@0
   240
void CMidiClientUtility::CBody::StopNotes(TInt aChannel)
sl@0
   241
	{
sl@0
   242
	if(aChannel >= KMinChannel && aChannel <= KMaxChannel) 
sl@0
   243
		{
sl@0
   244
		iMidiControllerCommands.StopNotes(aChannel);
sl@0
   245
		}
sl@0
   246
	}
sl@0
   247
sl@0
   248
void CMidiClientUtility::CBody::NoteOnL(TInt aChannel,TInt aNote,TInt aVelocity)
sl@0
   249
	{
sl@0
   250
	if((aChannel >= KMinChannel && aChannel <= KMaxChannel) 
sl@0
   251
	&& (aNote >= KMinNote && aNote <= KMaxNote) 
sl@0
   252
	&& (aVelocity >= KMinNoteOnVelocity && aVelocity <= KMaxNoteOnVelocity))
sl@0
   253
		{
sl@0
   254
		User::LeaveIfError(iMidiControllerCommands.NoteOn(aChannel, aNote, aVelocity));
sl@0
   255
		}
sl@0
   256
	else
sl@0
   257
		{
sl@0
   258
		User::Leave(KErrArgument);
sl@0
   259
		}
sl@0
   260
	}
sl@0
   261
sl@0
   262
void CMidiClientUtility::CBody::NoteOffL(TInt aChannel,TInt aNote,TInt aVelocity)
sl@0
   263
	{
sl@0
   264
	if((aChannel >= KMinChannel && aChannel <= KMaxChannel) 
sl@0
   265
	&& (aNote >= KMinNote && aNote <= KMaxNote) 
sl@0
   266
	&& (aVelocity >= KMinNoteOffVelocity && aVelocity <= KMaxNoteOffVelocity))
sl@0
   267
		{
sl@0
   268
		User::LeaveIfError(iMidiControllerCommands.NoteOff(aChannel, aNote, aVelocity));
sl@0
   269
		}
sl@0
   270
	else
sl@0
   271
		{
sl@0
   272
		User::Leave(KErrArgument);
sl@0
   273
		}
sl@0
   274
	}
sl@0
   275
sl@0
   276
TInt CMidiClientUtility::CBody::PlaybackRateL() const
sl@0
   277
	{
sl@0
   278
	TInt rate;
sl@0
   279
	User::LeaveIfError(iMidiControllerCommands.PlaybackRate(rate));
sl@0
   280
	return rate;
sl@0
   281
	}
sl@0
   282
sl@0
   283
void CMidiClientUtility::CBody::SetPlaybackRateL(TInt aRate)
sl@0
   284
	{
sl@0
   285
	User::LeaveIfError(iMidiControllerCommands.SetPlaybackRate(aRate));
sl@0
   286
	}
sl@0
   287
sl@0
   288
TInt CMidiClientUtility::CBody::MaxPlaybackRateL() const
sl@0
   289
	{
sl@0
   290
	TInt maxRate;
sl@0
   291
	User::LeaveIfError(iMidiControllerCommands.MaxPlaybackRate(maxRate));
sl@0
   292
	return maxRate;
sl@0
   293
	}
sl@0
   294
sl@0
   295
TInt CMidiClientUtility::CBody::MinPlaybackRateL() const
sl@0
   296
	{
sl@0
   297
	TInt minRate;
sl@0
   298
	User::LeaveIfError(iMidiControllerCommands.MinPlaybackRate(minRate));
sl@0
   299
	return minRate;
sl@0
   300
	}
sl@0
   301
sl@0
   302
sl@0
   303
TInt CMidiClientUtility::CBody::TempoMicroBeatsPerMinuteL() const
sl@0
   304
	{
sl@0
   305
	TInt microBeatsPerMinute;
sl@0
   306
	User::LeaveIfError(iMidiControllerCommands.TempoMicroBeatsPerMinute(microBeatsPerMinute));
sl@0
   307
	return microBeatsPerMinute;
sl@0
   308
	}
sl@0
   309
sl@0
   310
void CMidiClientUtility::CBody::SetTempoL(TInt aMicroBeatsPerMinute)
sl@0
   311
	{
sl@0
   312
	if(aMicroBeatsPerMinute > 0)
sl@0
   313
		{
sl@0
   314
		User::LeaveIfError(iMidiControllerCommands.SetTempo(aMicroBeatsPerMinute));
sl@0
   315
		}
sl@0
   316
	else
sl@0
   317
		{
sl@0
   318
		User::Leave(KErrArgument);
sl@0
   319
		}
sl@0
   320
	}
sl@0
   321
sl@0
   322
TInt CMidiClientUtility::CBody::PitchTranspositionCentsL() const
sl@0
   323
	{
sl@0
   324
	TInt cents;
sl@0
   325
	User::LeaveIfError(iMidiControllerCommands.PitchTranspositionCents(cents));
sl@0
   326
	return cents;
sl@0
   327
	}
sl@0
   328
sl@0
   329
TInt CMidiClientUtility::CBody::SetPitchTranspositionL(TInt aCents)
sl@0
   330
	{
sl@0
   331
	TInt pitchApplied = 0;
sl@0
   332
	//we do not check aCents value - it is expected the controller will report KErrArgument
sl@0
   333
	//if the pitch level is not supported.
sl@0
   334
	User::LeaveIfError(iMidiControllerCommands.SetPitchTransposition(aCents, pitchApplied));
sl@0
   335
sl@0
   336
	return pitchApplied;
sl@0
   337
	}
sl@0
   338
sl@0
   339
TTimeIntervalMicroSeconds CMidiClientUtility::CBody::DurationMicroSecondsL() const
sl@0
   340
	{
sl@0
   341
	TTimeIntervalMicroSeconds duration;
sl@0
   342
	User::LeaveIfError(iController.GetDuration(duration));
sl@0
   343
	return duration;
sl@0
   344
	}
sl@0
   345
sl@0
   346
TInt64 CMidiClientUtility::CBody::DurationMicroBeatsL() const
sl@0
   347
	{
sl@0
   348
	TInt64 duration;
sl@0
   349
	User::LeaveIfError(iMidiControllerCommands.DurationMicroBeats(duration));
sl@0
   350
	return duration;
sl@0
   351
	}
sl@0
   352
sl@0
   353
TInt CMidiClientUtility::CBody::NumTracksL() const
sl@0
   354
	{
sl@0
   355
	TInt tracks;
sl@0
   356
	User::LeaveIfError(iMidiControllerCommands.NumTracks(tracks));
sl@0
   357
	return tracks;
sl@0
   358
	}
sl@0
   359
sl@0
   360
void CMidiClientUtility::CBody::SetTrackMuteL(TInt aTrack, TBool aMuted) const
sl@0
   361
	{
sl@0
   362
	TInt numTracks = NumTracksL();
sl@0
   363
	if((aTrack >= 0) && (aTrack < numTracks))
sl@0
   364
		{
sl@0
   365
		User::LeaveIfError(iMidiControllerCommands.SetTrackMute(aTrack, aMuted));
sl@0
   366
		}
sl@0
   367
	else
sl@0
   368
		{
sl@0
   369
		User::Leave(KErrArgument);
sl@0
   370
		}
sl@0
   371
	}
sl@0
   372
sl@0
   373
const TDesC8& CMidiClientUtility::CBody::MimeTypeL()
sl@0
   374
	{
sl@0
   375
	TPtr8 des = iMimeType->Des();
sl@0
   376
	User::LeaveIfError(iMidiControllerCommands.MimeType(des));
sl@0
   377
	return *iMimeType;
sl@0
   378
	}
sl@0
   379
sl@0
   380
TTimeIntervalMicroSeconds CMidiClientUtility::CBody::PositionMicroSecondsL() const
sl@0
   381
	{
sl@0
   382
	TTimeIntervalMicroSeconds position;
sl@0
   383
	User::LeaveIfError(iController.GetPosition(position));
sl@0
   384
	return position;
sl@0
   385
	}
sl@0
   386
sl@0
   387
void CMidiClientUtility::CBody::SetPositionMicroSecondsL(const TTimeIntervalMicroSeconds& aPosition)
sl@0
   388
	{
sl@0
   389
	TTimeIntervalMicroSeconds maxPosition = DurationMicroSecondsL();
sl@0
   390
	TTimeIntervalMicroSeconds minPosition(0);
sl@0
   391
sl@0
   392
	TTimeIntervalMicroSeconds position = aPosition;
sl@0
   393
	if (aPosition > maxPosition)
sl@0
   394
		{
sl@0
   395
		position = maxPosition;
sl@0
   396
		}
sl@0
   397
	if (aPosition < minPosition)
sl@0
   398
		{
sl@0
   399
		position = minPosition;
sl@0
   400
		}
sl@0
   401
	User::LeaveIfError(iController.SetPosition(position));
sl@0
   402
	}
sl@0
   403
sl@0
   404
TInt64 CMidiClientUtility::CBody::PositionMicroBeatsL() const
sl@0
   405
	{
sl@0
   406
	TInt64 position;
sl@0
   407
	User::LeaveIfError(iMidiControllerCommands.PositionMicroBeats(position));
sl@0
   408
	return position;
sl@0
   409
	}
sl@0
   410
sl@0
   411
void CMidiClientUtility::CBody::SetPositionMicroBeatsL(TInt64 aMicroBeats)
sl@0
   412
	{
sl@0
   413
	TInt64 maxPosition = DurationMicroBeatsL();
sl@0
   414
	TInt64 minPosition(0);
sl@0
   415
sl@0
   416
	TInt64 position = aMicroBeats;
sl@0
   417
	if (aMicroBeats > maxPosition)
sl@0
   418
		{
sl@0
   419
		position = maxPosition;
sl@0
   420
		}
sl@0
   421
	if (aMicroBeats < minPosition)
sl@0
   422
		{
sl@0
   423
		position = minPosition;
sl@0
   424
		}
sl@0
   425
	User::LeaveIfError(iMidiControllerCommands.SetPositionMicroBeats(position));
sl@0
   426
	}
sl@0
   427
sl@0
   428
void CMidiClientUtility::CBody::SetSyncUpdateCallbackIntervalL(const TTimeIntervalMicroSeconds& aMicroSeconds, TInt64 aMicroBeats)
sl@0
   429
	{
sl@0
   430
	
sl@0
   431
	if((aMicroSeconds > TTimeIntervalMicroSeconds(0)) || (aMicroSeconds == TTimeIntervalMicroSeconds(0) && aMicroBeats == 0))
sl@0
   432
		{
sl@0
   433
		iIntervalSec = ETrue;
sl@0
   434
		}
sl@0
   435
	else 
sl@0
   436
		{
sl@0
   437
		if (aMicroBeats > 0)
sl@0
   438
			{
sl@0
   439
			iIntervalSec = EFalse;
sl@0
   440
			}
sl@0
   441
		else
sl@0
   442
			{
sl@0
   443
			User::Leave(KErrArgument);
sl@0
   444
			}
sl@0
   445
		}
sl@0
   446
	
sl@0
   447
	User::LeaveIfError(iMidiControllerCommands.SetSyncUpdateCallbackInterval(aMicroSeconds, aMicroBeats));
sl@0
   448
	}
sl@0
   449
sl@0
   450
TInt CMidiClientUtility::CBody::SendMessageL(const TDesC8& aMidiMessage)
sl@0
   451
	{
sl@0
   452
	TInt numByteProc;
sl@0
   453
	User::LeaveIfError(iMidiControllerCommands.SendMessage(aMidiMessage, numByteProc));
sl@0
   454
	return numByteProc;
sl@0
   455
	}
sl@0
   456
sl@0
   457
TInt CMidiClientUtility::CBody::SendMessageL(const TDesC8& aMidiMessage,const TTimeIntervalMicroSeconds& aTime)
sl@0
   458
	{
sl@0
   459
	TInt numByteProc;
sl@0
   460
	User::LeaveIfError(iMidiControllerCommands.SendMessage(aMidiMessage, aTime, numByteProc));
sl@0
   461
	return numByteProc;
sl@0
   462
	}
sl@0
   463
sl@0
   464
void CMidiClientUtility::CBody::SendMipMessageL(const RArray<TMipMessageEntry>& aEntry)
sl@0
   465
	{
sl@0
   466
	User::LeaveIfError(iMidiControllerCommands.SendMipMessage(aEntry));
sl@0
   467
	}
sl@0
   468
sl@0
   469
TInt CMidiClientUtility::CBody::NumberOfBanksL(TBool aCustom) const
sl@0
   470
	{
sl@0
   471
	TInt numBanks;
sl@0
   472
	User::LeaveIfError(iMidiControllerCommands.NumberOfBanks(aCustom, numBanks));
sl@0
   473
	return numBanks;
sl@0
   474
	}
sl@0
   475
sl@0
   476
TInt CMidiClientUtility::CBody::GetBankIdL(TBool aCustom,TInt aBankIndex) const
sl@0
   477
	{
sl@0
   478
	TInt numBanks = NumberOfBanksL(aCustom);
sl@0
   479
	TInt bankId = 0;
sl@0
   480
	if(aBankIndex >= 0 && aBankIndex < numBanks)
sl@0
   481
		{
sl@0
   482
		User::LeaveIfError(iMidiControllerCommands.GetBankId(aCustom, aBankIndex, bankId));
sl@0
   483
		}
sl@0
   484
	else
sl@0
   485
		{
sl@0
   486
		User::Leave(KErrArgument);
sl@0
   487
		}
sl@0
   488
	return bankId;
sl@0
   489
	}
sl@0
   490
sl@0
   491
void CMidiClientUtility::CBody::LoadCustomBankL(const TDesC& aFileName,TInt& aBankCollectionIndex)
sl@0
   492
	{
sl@0
   493
	User::LeaveIfError(iMidiControllerCommands.LoadCustomBank(aFileName, aBankCollectionIndex));
sl@0
   494
	}
sl@0
   495
sl@0
   496
void CMidiClientUtility::CBody::UnloadCustomBankL(TInt aBankCollectionIndex)
sl@0
   497
	{
sl@0
   498
	User::LeaveIfError(iMidiControllerCommands.UnloadCustomBank(aBankCollectionIndex));
sl@0
   499
	}
sl@0
   500
sl@0
   501
TBool CMidiClientUtility::CBody::CustomBankLoadedL(TInt aBankCollectionIndex) const
sl@0
   502
	{
sl@0
   503
	TBool bankLoaded;
sl@0
   504
	User::LeaveIfError(iMidiControllerCommands.CustomBankLoaded(aBankCollectionIndex, bankLoaded));
sl@0
   505
	return bankLoaded;
sl@0
   506
	}
sl@0
   507
sl@0
   508
void CMidiClientUtility::CBody::UnloadAllCustomBanksL()
sl@0
   509
	{
sl@0
   510
	User::LeaveIfError(iMidiControllerCommands.UnloadAllCustomBanks());
sl@0
   511
	}
sl@0
   512
sl@0
   513
TInt CMidiClientUtility::CBody::NumberOfInstrumentsL(TInt aBankId,TBool aCustom) const
sl@0
   514
	{
sl@0
   515
	TInt numInstruments;
sl@0
   516
	User::LeaveIfError(iMidiControllerCommands.NumberOfInstruments(aBankId, aCustom, numInstruments));
sl@0
   517
	return numInstruments;
sl@0
   518
	}
sl@0
   519
sl@0
   520
TInt CMidiClientUtility::CBody::GetInstrumentIdL(TInt aBankId,TBool aCustom,TInt aInstrumentIndex) const
sl@0
   521
	{
sl@0
   522
	TInt numInstruments = NumberOfInstrumentsL(aBankId, aCustom);
sl@0
   523
	TInt instrumentId = 0;
sl@0
   524
	if(aInstrumentIndex >=0 && aInstrumentIndex < numInstruments)
sl@0
   525
		{
sl@0
   526
		User::LeaveIfError(iMidiControllerCommands.GetInstrumentId(aBankId, aCustom, aInstrumentIndex, instrumentId));
sl@0
   527
		}
sl@0
   528
	else
sl@0
   529
		{
sl@0
   530
		User::Leave(KErrArgument);
sl@0
   531
		}
sl@0
   532
	return instrumentId;
sl@0
   533
	}
sl@0
   534
sl@0
   535
HBufC* CMidiClientUtility::CBody::InstrumentNameL(TInt aBankId, TBool aCustom, TInt aInstrumentId) const
sl@0
   536
	{
sl@0
   537
	HBufC* instrumentName = NULL;
sl@0
   538
sl@0
   539
	if(aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId)
sl@0
   540
		{
sl@0
   541
		instrumentName = iMidiControllerCommands.InstrumentNameL(aBankId, aCustom, aInstrumentId);
sl@0
   542
		}
sl@0
   543
	else
sl@0
   544
		{
sl@0
   545
		User::Leave(KErrArgument);
sl@0
   546
		}
sl@0
   547
sl@0
   548
	return instrumentName;
sl@0
   549
	}
sl@0
   550
sl@0
   551
void CMidiClientUtility::CBody::SetInstrumentL(TInt aChannel,TInt aBankId,TInt aInstrumentId)
sl@0
   552
	{
sl@0
   553
	if((aChannel >= KMinChannel && aChannel <= KMaxChannel)  
sl@0
   554
	&& (aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId))
sl@0
   555
		{
sl@0
   556
		User::LeaveIfError(iMidiControllerCommands.SetInstrument(aChannel, aBankId, aInstrumentId));
sl@0
   557
		}
sl@0
   558
	else
sl@0
   559
		{
sl@0
   560
		User::Leave(KErrArgument);
sl@0
   561
		}
sl@0
   562
	}
sl@0
   563
sl@0
   564
void CMidiClientUtility::CBody::LoadCustomInstrumentL(const TDesC& aFileName, TInt aFileBankId, TInt aFileInstrumentId, TInt aMemoryBankId, TInt aMemoryInstrumentId)
sl@0
   565
	{
sl@0
   566
	if((aFileInstrumentId >= KMinInstrumentId && aFileInstrumentId <= KMaxInstrumentId) 
sl@0
   567
	&& (aMemoryInstrumentId >= KMinInstrumentId && aMemoryInstrumentId <= KMaxInstrumentId))
sl@0
   568
		{
sl@0
   569
		User::LeaveIfError(iMidiControllerCommands.LoadCustomInstrument(aFileName, aFileBankId, aFileInstrumentId, aMemoryBankId, aMemoryInstrumentId));
sl@0
   570
		}	
sl@0
   571
	else
sl@0
   572
		{
sl@0
   573
		User::Leave(KErrArgument);
sl@0
   574
		}
sl@0
   575
	}
sl@0
   576
sl@0
   577
void CMidiClientUtility::CBody::UnloadCustomInstrumentL(TInt aCustomBankId,TInt aInstrumentId)
sl@0
   578
	{
sl@0
   579
	if(aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId)
sl@0
   580
		{
sl@0
   581
		User::LeaveIfError(iMidiControllerCommands.UnloadCustomInstrument(aCustomBankId, aInstrumentId));
sl@0
   582
		}
sl@0
   583
	else
sl@0
   584
		{
sl@0
   585
		User::Leave(KErrArgument);
sl@0
   586
		}
sl@0
   587
	}
sl@0
   588
sl@0
   589
HBufC* CMidiClientUtility::CBody::PercussionKeyNameL(TInt aNote, TInt aBankId, TBool aCustom, TInt aInstrumentId) const
sl@0
   590
	{
sl@0
   591
	HBufC* pKeyName = NULL;
sl@0
   592
sl@0
   593
	if((aNote >= KMinNote && aNote <= KMaxNote) 
sl@0
   594
	&& (aInstrumentId >= KMinInstrumentId && aInstrumentId <= KMaxInstrumentId))
sl@0
   595
		{
sl@0
   596
		pKeyName = iMidiControllerCommands.PercussionKeyNameL(aNote, aBankId, aCustom, aInstrumentId);
sl@0
   597
		}
sl@0
   598
	else
sl@0
   599
		{
sl@0
   600
		User::Leave(KErrArgument);
sl@0
   601
		}
sl@0
   602
sl@0
   603
	return pKeyName;
sl@0
   604
	}
sl@0
   605
sl@0
   606
void CMidiClientUtility::CBody::StopTimeL(TTimeIntervalMicroSeconds& aStopTime) const
sl@0
   607
	{
sl@0
   608
	User::LeaveIfError(iMidiControllerCommands.StopTime(aStopTime));
sl@0
   609
	}
sl@0
   610
sl@0
   611
void CMidiClientUtility::CBody::SetStopTimeL(const TTimeIntervalMicroSeconds& aStopTime)
sl@0
   612
	{
sl@0
   613
	TTimeIntervalMicroSeconds duration = DurationMicroSecondsL();
sl@0
   614
	if(aStopTime >= TTimeIntervalMicroSeconds(0) && aStopTime <= duration)
sl@0
   615
		{
sl@0
   616
		User::LeaveIfError(iMidiControllerCommands.SetStopTime(aStopTime));
sl@0
   617
		}
sl@0
   618
	else
sl@0
   619
		{
sl@0
   620
		User::Leave(KErrArgument);
sl@0
   621
		}
sl@0
   622
	}
sl@0
   623
sl@0
   624
void CMidiClientUtility::CBody::SetRepeatsL(TInt aRepeatNumberOfTimes, const TTimeIntervalMicroSeconds& aTrailingSilence)
sl@0
   625
	{
sl@0
   626
	if((aRepeatNumberOfTimes >= 0) && (aTrailingSilence >= TTimeIntervalMicroSeconds(0)))
sl@0
   627
		{
sl@0
   628
		User::LeaveIfError(iMidiControllerCommands.SetRepeats(aRepeatNumberOfTimes, aTrailingSilence));
sl@0
   629
		}
sl@0
   630
	else
sl@0
   631
		{
sl@0
   632
		User::Leave(KErrArgument);
sl@0
   633
		}	
sl@0
   634
	}
sl@0
   635
sl@0
   636
TInt CMidiClientUtility::CBody::PolyphonyL() const
sl@0
   637
	{
sl@0
   638
	TInt numNotes;
sl@0
   639
	TInt maxPoly = MaxPolyphonyL();
sl@0
   640
	User::LeaveIfError(iMidiControllerCommands.Polyphony(numNotes));
sl@0
   641
	if(maxPoly <= numNotes)
sl@0
   642
		{
sl@0
   643
		return maxPoly;
sl@0
   644
		}
sl@0
   645
	else
sl@0
   646
		{
sl@0
   647
		return numNotes;	
sl@0
   648
		}
sl@0
   649
	}
sl@0
   650
sl@0
   651
TInt CMidiClientUtility::CBody::MaxPolyphonyL() const
sl@0
   652
	{
sl@0
   653
	TInt maxNotes;
sl@0
   654
	User::LeaveIfError(iMidiControllerCommands.MaxPolyphony(maxNotes));
sl@0
   655
	return maxNotes;
sl@0
   656
	}
sl@0
   657
sl@0
   658
TInt CMidiClientUtility::CBody::ChannelsSupportedL() const
sl@0
   659
	{
sl@0
   660
	TInt channels;
sl@0
   661
	User::LeaveIfError(iMidiControllerCommands.ChannelsSupported(channels));
sl@0
   662
	return channels;
sl@0
   663
	}
sl@0
   664
sl@0
   665
TReal32 CMidiClientUtility::CBody::ChannelVolumeL(TInt aChannel) const
sl@0
   666
	{
sl@0
   667
	TReal32 channelVol;
sl@0
   668
	if(aChannel >= KMinChannel && aChannel <= KMaxChannel) 
sl@0
   669
		{
sl@0
   670
		User::LeaveIfError(iMidiControllerCommands.ChannelVolume(aChannel, channelVol));
sl@0
   671
		}
sl@0
   672
	else
sl@0
   673
		{
sl@0
   674
		User::Leave(KErrArgument);	
sl@0
   675
		}
sl@0
   676
	return channelVol;
sl@0
   677
	}
sl@0
   678
sl@0
   679
TReal32 CMidiClientUtility::CBody::MaxChannelVolumeL() const
sl@0
   680
	{
sl@0
   681
	TReal32 maxChanVol;
sl@0
   682
	User::LeaveIfError(iMidiControllerCommands.MaxChannelVolume(maxChanVol));
sl@0
   683
	return maxChanVol;
sl@0
   684
	}
sl@0
   685
sl@0
   686
void CMidiClientUtility::CBody::SetChannelVolumeL(TInt aChannel,TReal32 aVolume)
sl@0
   687
	{
sl@0
   688
	TReal32 maxChanVol = MaxChannelVolumeL();
sl@0
   689
	if((aChannel >= KMinChannel && aChannel <= KMaxChannel) && aVolume <= maxChanVol)
sl@0
   690
		{
sl@0
   691
		User::LeaveIfError(iMidiControllerCommands.SetChannelVolume(aChannel, aVolume));
sl@0
   692
		}
sl@0
   693
	else
sl@0
   694
		{
sl@0
   695
		User::Leave(KErrArgument);
sl@0
   696
		}
sl@0
   697
	}
sl@0
   698
sl@0
   699
void CMidiClientUtility::CBody::SetChannelMuteL(TInt aChannel,TBool aMuted)
sl@0
   700
	{
sl@0
   701
	if(aChannel >= KMinChannel && aChannel <= KMaxChannel)
sl@0
   702
		{
sl@0
   703
		User::LeaveIfError(iMidiControllerCommands.SetChannelMute(aChannel, aMuted));
sl@0
   704
		}
sl@0
   705
	else
sl@0
   706
		{
sl@0
   707
		User::Leave(KErrArgument);
sl@0
   708
		}
sl@0
   709
	}
sl@0
   710
sl@0
   711
TInt CMidiClientUtility::CBody::VolumeL() const
sl@0
   712
	{
sl@0
   713
	TInt vol;
sl@0
   714
	User::LeaveIfError(iMidiControllerCommands.Volume(vol));
sl@0
   715
	return vol;
sl@0
   716
	}
sl@0
   717
sl@0
   718
TInt CMidiClientUtility::CBody::MaxVolumeL() const
sl@0
   719
	{
sl@0
   720
	TInt maxVol;
sl@0
   721
	User::LeaveIfError(iMidiControllerCommands.MaxVolume(maxVol));
sl@0
   722
	return maxVol;
sl@0
   723
	}
sl@0
   724
sl@0
   725
void CMidiClientUtility::CBody::SetVolumeL(TInt aVolume)
sl@0
   726
	{
sl@0
   727
	User::LeaveIfError(iMidiControllerCommands.SetVolume(aVolume));
sl@0
   728
	}
sl@0
   729
sl@0
   730
void CMidiClientUtility::CBody::SetVolumeRampL(const TTimeIntervalMicroSeconds& aRampDuration)
sl@0
   731
	{
sl@0
   732
	User::LeaveIfError(iMidiControllerCommands.SetVolumeRamp(aRampDuration));
sl@0
   733
	}
sl@0
   734
sl@0
   735
sl@0
   736
TInt CMidiClientUtility::CBody::GetBalanceL() const
sl@0
   737
	{
sl@0
   738
	TInt balance;
sl@0
   739
	User::LeaveIfError(iMidiControllerCommands.GetBalance(balance));
sl@0
   740
	return balance;
sl@0
   741
	}
sl@0
   742
sl@0
   743
void CMidiClientUtility::CBody::SetBalanceL(TInt aBalance)
sl@0
   744
	{
sl@0
   745
	User::LeaveIfError(iMidiControllerCommands.SetBalance(aBalance));
sl@0
   746
	}
sl@0
   747
sl@0
   748
void CMidiClientUtility::CBody::SetPriorityL(TInt aPriority, TInt aPref)
sl@0
   749
	{
sl@0
   750
	TMMFPrioritySettings priority;
sl@0
   751
	priority.iPriority = aPriority;
sl@0
   752
	priority.iPref = aPref;
sl@0
   753
sl@0
   754
	User::LeaveIfError(iController.SetPrioritySettings(priority));
sl@0
   755
	}
sl@0
   756
sl@0
   757
TInt CMidiClientUtility::CBody::NumberOfMetaDataEntriesL() const
sl@0
   758
	{
sl@0
   759
	TInt numMetaData;
sl@0
   760
	User::LeaveIfError(iController.GetNumberOfMetaDataEntries(numMetaData));
sl@0
   761
	return numMetaData;
sl@0
   762
	}
sl@0
   763
sl@0
   764
CMMFMetaDataEntry* CMidiClientUtility::CBody::GetMetaDataEntryL(TInt aMetaDataIndex) const
sl@0
   765
	{
sl@0
   766
	CMMFMetaDataEntry* metaDataEntry = iController.GetMetaDataEntryL(aMetaDataIndex);
sl@0
   767
	return metaDataEntry;
sl@0
   768
	}
sl@0
   769
sl@0
   770
void CMidiClientUtility::CBody::CustomCommandSyncL(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2, TDes8& aDataFrom)
sl@0
   771
	{
sl@0
   772
	User::LeaveIfError(iController.CustomCommandSync(aDestination, aFunction, aDataTo1, aDataTo2, aDataFrom));
sl@0
   773
	}
sl@0
   774
sl@0
   775
void CMidiClientUtility::CBody::CustomCommandSyncL(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2)
sl@0
   776
	{
sl@0
   777
	User::LeaveIfError(iController.CustomCommandSync(aDestination, aFunction, aDataTo1, aDataTo2));
sl@0
   778
	}
sl@0
   779
sl@0
   780
void CMidiClientUtility::CBody::CustomCommandAsync(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2, TDes8& aDataFrom, TRequestStatus& aStatus)
sl@0
   781
	{
sl@0
   782
	iController.CustomCommandAsync(aDestination, aFunction, aDataTo1, aDataTo2, aDataFrom, aStatus);
sl@0
   783
	}
sl@0
   784
sl@0
   785
void CMidiClientUtility::CBody::CustomCommandAsync(const TMMFMessageDestinationPckg& aDestination, TInt aFunction, const TDesC8& aDataTo1, const TDesC8& aDataTo2, TRequestStatus& aStatus)
sl@0
   786
	{
sl@0
   787
	iController.CustomCommandAsync(aDestination, aFunction, aDataTo1, aDataTo2, aStatus);
sl@0
   788
	}
sl@0
   789
sl@0
   790
MMMFDRMCustomCommand* CMidiClientUtility::CBody::GetDRMCustomCommand()
sl@0
   791
	{
sl@0
   792
	if (iDRMCustomCommands.IsSupported())
sl@0
   793
		{
sl@0
   794
		return static_cast<MMMFDRMCustomCommand*>(&iDRMCustomCommands);
sl@0
   795
		}
sl@0
   796
	return NULL;
sl@0
   797
	}
sl@0
   798
	
sl@0
   799
TInt CMidiClientUtility::CBody::DoOpen(const RMMFControllerImplInfoArray& aControllers, TUid aSinkUid, const TDesC8& aSinkData, TBool aUseSharedHeap)
sl@0
   800
	{
sl@0
   801
	// Make sure any existing controller is closed.
sl@0
   802
	iMidiControllerEventMonitor->Cancel();
sl@0
   803
	iController.Close();
sl@0
   804
sl@0
   805
	// Try opening and configuring each controller in turn
sl@0
   806
	TInt error = KErrNotSupported;
sl@0
   807
	TInt index = 0;
sl@0
   808
	while (error)
sl@0
   809
		{
sl@0
   810
		// Break if we're at the end of the array of controllers
sl@0
   811
		if (index >= aControllers.Count())
sl@0
   812
			break;
sl@0
   813
sl@0
   814
		// Open the controller
sl@0
   815
		error = iController.Open(aControllers[index]->Uid(), iPrioritySettings, aUseSharedHeap);
sl@0
   816
sl@0
   817
		// If the controller was opened without error, start receiving events from it.
sl@0
   818
		if (error==KErrNone)
sl@0
   819
			{
sl@0
   820
			iMidiControllerEventMonitor->Start();
sl@0
   821
			}
sl@0
   822
sl@0
   823
		// Add the data sink
sl@0
   824
		if (error==KErrNone)
sl@0
   825
			error = iController.AddDataSink(aSinkUid, aSinkData, iSinkHandle);
sl@0
   826
sl@0
   827
		// If an error occurred in any of the above, close the controller.
sl@0
   828
		if (error!=KErrNone)
sl@0
   829
			{
sl@0
   830
			iMidiControllerEventMonitor->Cancel();
sl@0
   831
			iController.Close();
sl@0
   832
			}
sl@0
   833
sl@0
   834
		// increment index
sl@0
   835
		index++;
sl@0
   836
		}
sl@0
   837
sl@0
   838
	return error;
sl@0
   839
	}
sl@0
   840
sl@0
   841
   void CMidiClientUtility::CBody::HandleMidiEvent(const CMMFMidiEvent& aEvent)
sl@0
   842
   	{
sl@0
   843
   	if(aEvent.iEventType == KMMFEventCategoryMidiOpenDataSourceComplete ||
sl@0
   844
   	   aEvent.iEventType == KMMFEventCategoryMidiClose ||
sl@0
   845
   	   aEvent.iEventType == KMMFEventCategoryMidiPrime ||
sl@0
   846
   	   aEvent.iEventType == KMMFEventCategoryMidiPlaying ||
sl@0
   847
   	   aEvent.iEventType == KMMFEventCategoryMidiPlaybackIncompatible ||
sl@0
   848
   	   aEvent.iEventType == KMMFEventCategoryMidiPlaybackSilent)
sl@0
   849
   		{
sl@0
   850
   		iState = aEvent.iNewState;
sl@0
   851
   		iObserver.MmcuoStateChanged(aEvent.iOldState, aEvent.iNewState, aEvent.iMicroSeconds, aEvent.iErrorCode);
sl@0
   852
   		
sl@0
   853
   		if (aEvent.iEventType == KMMFEventCategoryMidiClose)
sl@0
   854
   			{
sl@0
   855
			if (iSourceHandle.DestinationHandle())
sl@0
   856
				{
sl@0
   857
				iController.RemoveDataSource(iSourceHandle);
sl@0
   858
				}
sl@0
   859
			}	
sl@0
   860
 		}
sl@0
   861
   	else if(aEvent.iEventType == KMMFEventCategoryMidiPlayingComplete)
sl@0
   862
   		{
sl@0
   863
 		iState = aEvent.iNewState;
sl@0
   864
   		iObserver.MmcuoStateChanged(aEvent.iOldState, aEvent.iNewState, aEvent.iMicroSeconds, aEvent.iErrorCode);
sl@0
   865
   		}
sl@0
   866
   	else if(aEvent.iEventType == KMMFEventCategoryMidiSyncUpdate)
sl@0
   867
   		{
sl@0
   868
   		iObserver.MmcuoSyncUpdate(aEvent.iMicroSeconds, aEvent.iMicroBeats);
sl@0
   869
   		}
sl@0
   870
   	else if(aEvent.iEventType == KMMFEventCategoryTempoChanged)
sl@0
   871
   		{
sl@0
   872
   		iObserver.MmcuoTempoChanged(aEvent.iTempoMicroBeats);
sl@0
   873
   		}
sl@0
   874
   	else if(aEvent.iEventType == KMMFEventCategoryVolumeChanged)
sl@0
   875
   		{
sl@0
   876
   		iObserver.MmcuoVolumeChanged(aEvent.iChannel, aEvent.iVolumeInDecibels);
sl@0
   877
   		}
sl@0
   878
   	else if(aEvent.iEventType == KMMFEventCategoryMuteChanged)
sl@0
   879
   		{
sl@0
   880
   		iObserver.MmcuoMuteChanged(aEvent.iChannel, aEvent.iMute);
sl@0
   881
   		}
sl@0
   882
   	else if(aEvent.iEventType == KMMFEventCategoryMetaDataEntryFound)
sl@0
   883
   		{
sl@0
   884
   		iObserver.MmcuoMetaDataEntryFound(aEvent.iMetaDataEntryId, aEvent.iMicroSeconds);
sl@0
   885
   		}
sl@0
   886
   	else if(aEvent.iEventType == KMMFEventCategoryMipMessageReceived)
sl@0
   887
   		{
sl@0
   888
   		iObserver.MmcuoMipMessageReceived(aEvent.iMipMessage);
sl@0
   889
   		}
sl@0
   890
   	else if(aEvent.iEventType == KMMFEventCategoryPolyphonyChanged)
sl@0
   891
   		{
sl@0
   892
   		iObserver.MmcuoPolyphonyChanged(aEvent.iPolyphony);
sl@0
   893
   		}
sl@0
   894
   	else if(aEvent.iEventType == KMMFEventCategoryInstrumentChanged)
sl@0
   895
   		{
sl@0
   896
   		iObserver.MmcuoInstrumentChanged(aEvent.iChannel,aEvent.iBankId,aEvent.iInstrumentId);
sl@0
   897
   		}
sl@0
   898
   	else if((iState == EMidiStateOpenPlaying) || (iState == EMidiStatePlaybackIncompatible) || 
sl@0
   899
   			(iState == EMidiStatePlaybackSilent) || (iState == EMidiStateClosedEngaged) ||
sl@0
   900
   			(iState == EMidiStateOpenEngaged))
sl@0
   901
   		{
sl@0
   902
 		iState = aEvent.iNewState;
sl@0
   903
   		iObserver.MmcuoStateChanged(aEvent.iOldState, aEvent.iNewState, aEvent.iMicroSeconds, aEvent.iErrorCode);
sl@0
   904
sl@0
   905
   		}
sl@0
   906
   	else if (aEvent.iEventType == KMMFErrorCategoryControllerGeneralError)
sl@0
   907
   		{
sl@0
   908
   		iObserver.MmcuoStateChanged(iState, iState, TTimeIntervalMicroSeconds(0), aEvent.iErrorCode);
sl@0
   909
   		}
sl@0
   910
   	else
sl@0
   911
   		{
sl@0
   912
   		// xxx - what do we do when we don't understand the error type?
sl@0
   913
   		}
sl@0
   914
   	}
sl@0
   915
sl@0
   916
/**
sl@0
   917
 *
sl@0
   918
 * Used to change the value of MaxPolyphonyL()
sl@0
   919
 */ 	
sl@0
   920
void CMidiClientUtility::CBody::SetMaxPolyphonyL(TInt aMaxNotes)
sl@0
   921
	{
sl@0
   922
	User::LeaveIfError(iMidiControllerCommands.SetMaxPolyphony(aMaxNotes));
sl@0
   923
	}
sl@0
   924
sl@0
   925
TInt CMidiClientUtility::CBody::GetRepeats()
sl@0
   926
	{
sl@0
   927
	TInt numRepeats = 0;
sl@0
   928
	iMidiControllerCommands.GetRepeats(numRepeats);
sl@0
   929
	return numRepeats;
sl@0
   930
	}
sl@0
   931
sl@0
   932
void CMidiClientUtility::CBody::LoadCustomBankDataL(const TDesC8& aBankData,TInt& aBankId)
sl@0
   933
	{
sl@0
   934
	User::LeaveIfError(iMidiControllerCommands.LoadCustomBankData(aBankData, aBankId));
sl@0
   935
	}
sl@0
   936
sl@0
   937
void CMidiClientUtility::CBody::LoadCustomInstrumentDataL(const TDesC8& aInstrumentData, TInt aBankDataId, TInt aInstrumentDataId, TInt aMemoryBankId, TInt aMemoryInstrumentId)
sl@0
   938
	{
sl@0
   939
	if((aInstrumentDataId >= KMinInstrumentId && aInstrumentDataId <= KMaxInstrumentId) 
sl@0
   940
	&& (aMemoryInstrumentId >= KMinInstrumentId && aMemoryInstrumentId <= KMaxInstrumentId))
sl@0
   941
		{
sl@0
   942
		User::LeaveIfError(iMidiControllerCommands.LoadCustomInstrumentData(aInstrumentData, aBankDataId, aInstrumentDataId, aMemoryBankId, aMemoryInstrumentId));
sl@0
   943
		}
sl@0
   944
	else
sl@0
   945
		{
sl@0
   946
		User::Leave(KErrArgument);
sl@0
   947
		}
sl@0
   948
	}
sl@0
   949
sl@0
   950
void CMidiClientUtility::CBody::SetBankL(TBool aCustom)
sl@0
   951
	{
sl@0
   952
	User::LeaveIfError(iMidiControllerCommands.SetBank(aCustom));
sl@0
   953
	}
sl@0
   954
sl@0
   955
TBool CMidiClientUtility::CBody::IsTrackMuteL(TInt aTrack) const
sl@0
   956
	{
sl@0
   957
	TBool mute;
sl@0
   958
	User::LeaveIfError(iMidiControllerCommands.IsTrackMute(aTrack, mute));
sl@0
   959
	return mute;
sl@0
   960
	}
sl@0
   961
sl@0
   962
TBool CMidiClientUtility::CBody::IsChannelMuteL(TInt aChannel) const
sl@0
   963
	{
sl@0
   964
	TBool mute;
sl@0
   965
	if (aChannel >= KMinChannel && aChannel <= KMaxChannel)
sl@0
   966
		{
sl@0
   967
		User::LeaveIfError(iMidiControllerCommands.IsChannelMute(aChannel, mute));	
sl@0
   968
		}
sl@0
   969
	else
sl@0
   970
		{
sl@0
   971
		User::Leave(KErrArgument);	
sl@0
   972
		}
sl@0
   973
		
sl@0
   974
	return mute;
sl@0
   975
	}
sl@0
   976
sl@0
   977
void CMidiClientUtility::CBody::GetInstrumentL(TInt aChannel, TInt& aInstrumentId, TInt& aBankId)
sl@0
   978
	{
sl@0
   979
	if (aChannel >= KMinChannel && aChannel <= KMaxChannel)
sl@0
   980
		{
sl@0
   981
		User::LeaveIfError(iMidiControllerCommands.GetInstrument(aChannel, aInstrumentId, aBankId));	
sl@0
   982
		}
sl@0
   983
	else
sl@0
   984
		{
sl@0
   985
		User::Leave(KErrArgument);		
sl@0
   986
		}
sl@0
   987
	}
sl@0
   988
sl@0
   989
void CMidiClientUtility::CBody::RepeatTrailingSilenceTimerComplete()
sl@0
   990
	{
sl@0
   991
	Play();
sl@0
   992
	}
sl@0
   993
sl@0
   994
CRepeatTrailingSilenceTimer* CRepeatTrailingSilenceTimer::NewL(MRepeatTrailingSilenceTimerObs& aObs)
sl@0
   995
	{
sl@0
   996
	CRepeatTrailingSilenceTimer* s = new(ELeave) CRepeatTrailingSilenceTimer(aObs);
sl@0
   997
	CleanupStack::PushL(s);
sl@0
   998
	s->ConstructL();
sl@0
   999
	CleanupStack::Pop();
sl@0
  1000
	return s;
sl@0
  1001
	}
sl@0
  1002
sl@0
  1003
void CRepeatTrailingSilenceTimer::RunL()
sl@0
  1004
	{
sl@0
  1005
	iObs.RepeatTrailingSilenceTimerComplete();
sl@0
  1006
	}
sl@0
  1007
sl@0
  1008
CRepeatTrailingSilenceTimer::CRepeatTrailingSilenceTimer(MRepeatTrailingSilenceTimerObs& aObs) :
sl@0
  1009
	CTimer(EPriorityHigh),
sl@0
  1010
	iObs(aObs)
sl@0
  1011
	{
sl@0
  1012
	CActiveScheduler::Add(this);
sl@0
  1013
	}
sl@0
  1014
sl@0
  1015
//
sl@0
  1016
//
sl@0
  1017
//
sl@0
  1018
//
sl@0
  1019
sl@0
  1020
CMidiControllerEventMonitor* CMidiControllerEventMonitor::NewL(MMidiControllerEventMonitorObserver& aMidiObserver, 
sl@0
  1021
												RMidiControllerCustomCommands& aMidiControllerCustomCommands, const CMidiClientUtility& aParent)
sl@0
  1022
	{
sl@0
  1023
	CMidiControllerEventMonitor* self = new(ELeave) CMidiControllerEventMonitor(aMidiObserver, aMidiControllerCustomCommands, aParent);
sl@0
  1024
	CleanupStack::PushL(self);
sl@0
  1025
	self->ConstructL();
sl@0
  1026
	CleanupStack::Pop(self);
sl@0
  1027
	return self;
sl@0
  1028
	}
sl@0
  1029
sl@0
  1030
void CMidiControllerEventMonitor::ConstructL()
sl@0
  1031
	{
sl@0
  1032
	iMidiEvent = new (ELeave) CMMFMidiEvent();
sl@0
  1033
	}
sl@0
  1034
sl@0
  1035
CMidiControllerEventMonitor::CMidiControllerEventMonitor(MMidiControllerEventMonitorObserver& aMidiObserver, 
sl@0
  1036
													   RMidiControllerCustomCommands& aMidiControllerCustomCommands, const CMidiClientUtility& aParent) :
sl@0
  1037
	CActive(EPriorityStandard),
sl@0
  1038
	iMidiObserver(aMidiObserver), 
sl@0
  1039
	iMidiControllerCustomCommands(aMidiControllerCustomCommands),
sl@0
  1040
	iParent(aParent)
sl@0
  1041
	{
sl@0
  1042
	CActiveScheduler::Add(this);
sl@0
  1043
	}
sl@0
  1044
sl@0
  1045
CMidiControllerEventMonitor::~CMidiControllerEventMonitor()
sl@0
  1046
	{
sl@0
  1047
	Cancel();
sl@0
  1048
	delete iMidiEvent;
sl@0
  1049
	}
sl@0
  1050
sl@0
  1051
/**
sl@0
  1052
Start receiving events from the controller.
sl@0
  1053
sl@0
  1054
This can only be called once the controller is open.
sl@0
  1055
*/
sl@0
  1056
void CMidiControllerEventMonitor::Start()
sl@0
  1057
	{
sl@0
  1058
	iMidiControllerCustomCommands.ReceiveEvents(iSizeOfEvent, iStatus);
sl@0
  1059
	SetActive();
sl@0
  1060
	}
sl@0
  1061
sl@0
  1062
void CMidiControllerEventMonitor::RunL()
sl@0
  1063
	{
sl@0
  1064
	User::LeaveIfError(iStatus.Int());
sl@0
  1065
sl@0
  1066
	// Create a buffer big enough to hold the event, then retrieve it from the server
sl@0
  1067
	HBufC8* buf = HBufC8::NewLC(iSizeOfEvent());
sl@0
  1068
	TPtr8 bufPtr = buf->Des();
sl@0
  1069
	User::LeaveIfError(iMidiControllerCustomCommands.RetrieveEvent(bufPtr));
sl@0
  1070
sl@0
  1071
	// Now internalize a CMMFMidiEvent with the info in the buffer
sl@0
  1072
	RDesReadStream stream(bufPtr);
sl@0
  1073
	CleanupClosePushL(stream);
sl@0
  1074
sl@0
  1075
	CMMFMidiEvent* theEvent = new (ELeave) CMMFMidiEvent();
sl@0
  1076
	
sl@0
  1077
	CleanupStack::PushL(theEvent);
sl@0
  1078
	theEvent->InternalizeL(stream);
sl@0
  1079
	
sl@0
  1080
	iMidiObserver.HandleMidiEvent(*theEvent);
sl@0
  1081
	Start();
sl@0
  1082
sl@0
  1083
	CleanupStack::PopAndDestroy(3);//buf, stream, theEvent
sl@0
  1084
	}
sl@0
  1085
sl@0
  1086
void CMidiControllerEventMonitor::SelfComplete(TInt aError)
sl@0
  1087
	{
sl@0
  1088
	Cancel();
sl@0
  1089
	TRequestStatus *status = &iStatus;
sl@0
  1090
	if(!IsActive())
sl@0
  1091
		SetActive();
sl@0
  1092
	User::RequestComplete(status, aError);
sl@0
  1093
	}
sl@0
  1094
sl@0
  1095
void CMidiControllerEventMonitor::DoCancel()
sl@0
  1096
	{
sl@0
  1097
	iMidiControllerCustomCommands.CancelReceiveEvents();
sl@0
  1098
	}
sl@0
  1099
sl@0
  1100
TInt CMidiControllerEventMonitor::RunError(TInt aError)
sl@0
  1101
	{
sl@0
  1102
	iMidiEvent->iEventType = KMMFErrorCategoryControllerGeneralError;
sl@0
  1103
	iMidiEvent->iErrorCode = aError;
sl@0
  1104
	iMidiEvent->iOldState = iParent.State();
sl@0
  1105
	iMidiEvent->iNewState = iMidiEvent->iOldState;
sl@0
  1106
sl@0
  1107
	iMidiObserver.HandleMidiEvent(*iMidiEvent);
sl@0
  1108
	Start();
sl@0
  1109
	return KErrNone;
sl@0
  1110
	}