void CTrack_DSP::Stop()
{
	tint32 iInsert;
	for (iInsert = 0; iInsert < giNumber_Of_Inserts; iInsert++) {
		if (mppInsert[iInsert]) {
			mppInsert[iInsert]->Stop();
		}
	}

	if (mpFileRecording) {
		mpFileRecording->Destroy();
		mpFileRecording = NULL;

		tbool bSuccess = true;
		tbool bAbortOperation = false;
		tchar pszErrMsg[1024];
		*pszErrMsg = '\0';
		tint64 iSamples = 0;
		ac::EQuality eQuality = ac::geQualityUnknown;
		tint64 iAccumOverflows = 0;
		tfloat32 fNormalizationFactor = 1.0f / mfPeak;
		tfloat32 fNormalizationFactorMono = 1.0f / mfPeakMono;

		{
			CAutoDelete<IFile> pFileSrc(IFile::Create());
			CAutoDelete<IFile> pFileDest(IFile::Create());

			// read from file
			bSuccess &= pFileSrc->Open((msRecordingName + std::string(".tmp")).c_str(), IFile::FileRead);
			
			// write to file
			bSuccess &= pFileDest->Open(msRecordingName.c_str(), IFile::FileCreate);

			if (!bSuccess) {
				sprintf(pszErrMsg, "File open operation(s) failed");
			}
			else {
				tint64 iDataBytes = pFileSrc->GetSizeWhenOpened();
				iSamples = (iDataBytes / sizeof(tfloat32)) / miRecordingChannels;
				if (iSamples <= 0)
					bSuccess = false;
			}

			if (bSuccess) {
				CAutoDelete<ac::IEncoder> pEncoder(ac::IEncoder::Create(ac::geAudioCodecWave, pszErrMsg, 1024));
				if (!pEncoder) {
					bSuccess = false;
				}
				else {
					if (bSuccess) {
						/*
						std::string sMsg =
							std::string("Do you want to import the recorded part as 24 bit Wave?\n")
							+
							//sName
							//+
							//"\n"
							"\n"
							"- Yes:  \t24 bits = no quality loss\n"
							"- No:   \t16 bits = maybe quality loss\n"
							"- Cancel:\tAbort";

						ge::IWindow::EMsgBoxReturn eRc = ge::IWindow::ShowMessageBox(
							sMsg.c_str(), "Select bit depth", ge::IWindow::MsgBoxYesNoCancel);
						//bAskedAboutBits = true;
						switch (eRc) {
							case ge::IWindow::MsgBoxRetYes: eQuality = ac::geQualityLossless24; break;
							case ge::IWindow::MsgBoxRetNo: eQuality = ac::geQualityLossless16; break;
							default: bAbortOperation = true; break;
						}
						 */
						eQuality = ac::geQualityLossless16;
					}

					tbool bDownMix = false;
					if (bSuccess && !bAbortOperation) {
						/*
						tchar pszMsg[] =
							"Do you want to import as stereo?\n"
							"\n"
							"- Yes:  \tStereo\n"
							"- No:   \tDownmix to mono\n"
							"- Cancel:\tAbort";
						ge::IWindow::EMsgBoxReturn eRc = ge::IWindow::ShowMessageBox(
							pszMsg, "Import as stereo", ge::IWindow::MsgBoxYesNoCancel);
						switch (eRc) {
							case ge::IWindow::MsgBoxRetYes: break;
							case ge::IWindow::MsgBoxRetNo: bDownMix = true; break;
							default: bAbortOperation = true; break;
						}
						 */
					}

					if (bSuccess && !bAbortOperation) {
						tint32 iDestChannels = (bDownMix) ? 1 : miRecordingChannels;
						if (
							(!pEncoder->Init(pFileDest, eQuality))
							||
							(!pEncoder->SetRawMode(true, iDestChannels, false, 32, gpDSPEngine->GetSampleRate()))
						) {
							bSuccess = false;
						}
					}

					if (bSuccess && !bAbortOperation) {
						tint64 iSkipExtraLatencySamples = gpApplication->AudioInput_IntermediateBuffer_CalcExtraLatency();
						pEncoder->SetNormalizationFactor((bDownMix) ? fNormalizationFactorMono : fNormalizationFactor);

						if (miRecordingChannels == 1) {
							// Mono recording
							tint32 iSamplesLeft = iSamples;
							while ((bSuccess) && (iSamplesLeft > 0)) {
								tfloat32 pSrc[32];
								tint32 iBytesRead = (tint32)pFileSrc->Read((tchar*)pSrc, 32 * sizeof(tfloat32));
								tint32 iSamplesRead = iBytesRead / sizeof(tfloat32);
								if (iSkipExtraLatencySamples > 0) {
									if (iSamplesRead <= iSkipExtraLatencySamples) {
										// Latency - eat them all
										iSkipExtraLatencySamples -= iSamplesRead;
										iSamplesRead = 0;
									}
									else {
										// Partial latency - eat some
										iSamplesRead -= iSkipExtraLatencySamples;
										iBytesRead = iSamplesRead * sizeof(tfloat32);
										tfloat32* pfAfterLatency = pSrc + iSkipExtraLatencySamples;
										iSkipExtraLatencySamples = 0;
										if (!pEncoder->ProcessRaw((tchar*)pfAfterLatency, NULL, iBytesRead, &iAccumOverflows)) {
											bSuccess = false;
										}
									}
								}
								else {
									if (!pEncoder->ProcessRaw((tchar*)pSrc, NULL, iBytesRead, &iAccumOverflows)) {
										bSuccess = false;
									}
								}

								iSamplesLeft -= 32;
							}
						}
						else {
							// Stereo recording (maybe it gets down-mixed to mono)
							tint32 iSamplesLeft = iSamples;
							while ((bSuccess) && (iSamplesLeft > 0)) {
								tfloat32 pSrc[2 * 32];
								tint32 iBytesRead = (tint32)pFileSrc->Read((tchar*)pSrc, 2 * 32 * sizeof(tfloat32));
								iBytesRead /= 2;
								tint32 iSamplesRead = iBytesRead / sizeof(tfloat32);
								tfloat32* pSrcR = pSrc + iSamplesRead;

								if (iSkipExtraLatencySamples > 0) {
									if (iSamplesRead <= iSkipExtraLatencySamples) {
										// Latency - eat them all
										iSkipExtraLatencySamples -= iSamplesRead;
										iSamplesRead = 0;
									}
									else {
										// Partial latency - eat some
										iSamplesRead -= iSkipExtraLatencySamples;
										iBytesRead = iSamplesRead * sizeof(tfloat32);
										tfloat32* pfAfterLatencyL = pSrc + iSkipExtraLatencySamples;
										tfloat32* pfAfterLatencyR = pSrcR + iSkipExtraLatencySamples;
										iSkipExtraLatencySamples = 0;
										if (!pEncoder->ProcessRaw((tchar*)pfAfterLatencyL, (tchar*)pfAfterLatencyR, iBytesRead, &iAccumOverflows)) {
											bSuccess = false;
										}
									}
								}
								else {
									if (!pEncoder->ProcessRaw((tchar*)pSrc, (tchar*)pSrcR, iBytesRead, &iAccumOverflows)) {
										bSuccess = false;
									}
								}

								iSamplesLeft -= 32;
							}
						}
					}

					if (!bSuccess) {
						pEncoder->GetErrMsg(pszErrMsg, 1024);
					}
				}
			}
		}

		IFile::DeleteFile((msRecordingName + std::string(".tmp")).c_str());

		if (!bSuccess) {
			
			ge::IWindow::ShowMessageBox( std::string("Error").c_str(), 
										"recording", 
										ge::IWindow::MsgBoxOK);
			/*
			gpApplication->ShowMessageBox_NonModal(pszErrMsg, "Error recording");
			 */
		}
		else if (!bAbortOperation) {
			gpApplication->QueueAudioFileImport(msRecordingName.c_str(), true, miTrack, miRecordingSongPos);
		}

		/* must wait for queued operation to complete before doing these operations

		IFile::DeleteFile(msRecordingName.c_str());

		std::string sName = msRecordingName.substr(0, msRecordingName.size() - 4);
		tint iPos = sName.rfind(':');
		sName = sName.substr(iPos + 1);
		mpDSP->CreateRegion(sName, miTrack, miRecordingSongPos, 0);
		*/
	}
} // Stop
Example #2
0
BOOL BEEncoder::encode(LPBEOBJECT pObject, ISequentialStream *pStream)
{
    BEEncoderPtr pEncoder(new BEEncoder(pStream));
    return pEncoder->EncodeObject(pObject);
}