📄 audiouniteffect.cpp
字号:
leftBuffer = rightBuffer = NULL; mLeftBufferForCallback = mRightBufferForCallback = NULL; if (left) { leftBuffer = new float[waveTrackBlockSize]; mLeftBufferForCallback = new float[unitBlockSize]; } if (right) { rightBuffer = new float[waveTrackBlockSize]; mRightBufferForCallback = new float[unitBlockSize]; } sampleCount originalLen = len; longSampleCount ls = lstart; longSampleCount rs = rstart; while (len) { int block = waveTrackBlockSize; if (block > len) block = len; if (left) left->Get((samplePtr)leftBuffer, floatSample, ls, block); if (right) right->Get((samplePtr)rightBuffer, floatSample, rs, block); if (!DoRender(trackUnit, numChannels, leftBuffer, rightBuffer, block, unitBlockSize, &timeStamp)) { if (leftBuffer) { delete[] leftBuffer; delete[] mLeftBufferForCallback; } if (rightBuffer) { delete[] rightBuffer; delete[] mRightBufferForCallback; } AudioUnitUninitialize(trackUnit); CloseComponent(trackUnit); return false; } if (left) left->Set((samplePtr)leftBuffer, floatSample, ls, block); if (right) right->Set((samplePtr)rightBuffer, floatSample, rs, block); len -= block; ls += block; rs += block; if (left && right) { if (TrackGroupProgress(count, (ls-lstart)/(double)originalLen)) return false; } else { if (TrackProgress(count, (ls-lstart)/(double)originalLen)) return false; } } if (leftBuffer) { delete[] leftBuffer; delete[] mLeftBufferForCallback; } if (rightBuffer) { delete[] rightBuffer; delete[] mRightBufferForCallback; } AudioUnitUninitialize(trackUnit); CloseComponent(trackUnit); return true;}bool AudioUnitEffect::DoRender(AudioUnit unit, int numChannels, float *leftBuffer, float *rightBuffer, int len, int unitBlockSize, AudioTimeStamp *timeStamp){ AudioBufferList *bufferList; AudioUnitRenderActionFlags flags; ComponentResult auResult; int i, j, block; bufferList = (AudioBufferList *)malloc(sizeof(UInt32) + numChannels * sizeof(AudioBuffer)); bufferList->mNumberBuffers = numChannels; i = 0; while(i < len) { block = unitBlockSize; if (i + block > len) block = len - i; if (leftBuffer) memcpy(mLeftBufferForCallback, &leftBuffer[i], block*sizeof(float)); if (rightBuffer) memcpy(mRightBufferForCallback, &rightBuffer[i], block*sizeof(float)); flags = 0; for(j=0; j<numChannels; j++) { bufferList->mBuffers[j].mNumberChannels = 1; bufferList->mBuffers[j].mData = NULL; bufferList->mBuffers[j].mDataByteSize = sizeof(float) * block; } auResult = AudioUnitRender(unit, &flags, timeStamp, 0, block, bufferList); if (auResult != 0) { printf("Render failed: %d %4.4s\n", (int)auResult, (char *)&auResult); free(bufferList); return false; } if (leftBuffer) memcpy(&leftBuffer[i], bufferList->mBuffers[0].mData, block*sizeof(float)); if (rightBuffer) memcpy(&rightBuffer[i], bufferList->mBuffers[1].mData, block*sizeof(float)); timeStamp->mSampleTime += block; i += block; } free(bufferList); return true;}// staticOSStatus AudioUnitEffect::SimpleAudioRenderCallback (void *inRefCon, AudioUnitRenderActionFlags *inActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumFrames, AudioBufferList *ioData){ AudioUnitEffect *This = (AudioUnitEffect *)inRefCon; if (This->mLeftBufferForCallback) ioData->mBuffers[0].mData = This->mLeftBufferForCallback; if (This->mRightBufferForCallback) ioData->mBuffers[1].mData = This->mRightBufferForCallback; return 0;}Component AudioUnitEffect::GetCarbonViewComponent(OSType subtype){ ComponentDescription desc; Component component; desc.componentType = kAudioUnitCarbonViewComponentType; // 'auvw' desc.componentSubType = subtype; desc.componentManufacturer = 0; desc.componentFlags = 0; desc.componentFlagsMask = 0; // First see if we can find a carbon view designed specifically for this // plug-in: component = FindNextComponent(NULL, &desc); if (component) return component; // If not, grab the generic carbon view, which will create a GUI for // any Audio Unit. desc.componentSubType = kAUCarbonViewSubType_Generic; component = FindNextComponent(NULL, &desc); return component;}//// AudioUnitGUIControl methods//BEGIN_EVENT_TABLE(AudioUnitGUIControl, wxControl) EVT_MOUSE_EVENTS(AudioUnitGUIControl::OnMouse)END_EVENT_TABLE()AudioUnitGUIControl::~AudioUnitGUIControl(){ // Don't want to dispose of it... m_peer = NULL;}bool AudioUnitGUIControl::Create(wxWindow *parent, wxWindowID id, wxPoint pos, wxSize size, ControlRef controlRef){ m_macIsUserPane = FALSE ; if ( !wxControl::Create(parent, id, pos, size, 0, wxDefaultValidator, "AudioUnitControl") ) return false; m_peer = new wxMacControl(this, controlRef); Rect outBounds; GetControlBounds(controlRef, &outBounds); pos.x = outBounds.left; pos.y = outBounds.top; size.x = outBounds.right - outBounds.left; size.y = outBounds.bottom - outBounds.top; MacPostControlCreate(pos, size); return true;}short AudioUnitGUIControl::GetModifiers(wxMouseEvent &event){ short modifiers = 0; if ( !event.m_leftDown && !event.m_rightDown ) modifiers |= btnState ; if ( event.m_shiftDown ) modifiers |= shiftKey ; if ( event.m_controlDown ) modifiers |= controlKey ; if ( event.m_altDown ) modifiers |= optionKey ; if ( event.m_metaDown ) modifiers |= cmdKey ; return modifiers;}void AudioUnitGUIControl::OnMouse(wxMouseEvent &event){ int x = event.m_x ; int y = event.m_y ; MacClientToRootWindow( &x , &y ) ; Point localwhere ; ControlHandle control; localwhere.h = x ; localwhere.v = y ; short modifiers = GetModifiers(event); if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK ) { #if ((wxMAJOR_VERSION == 2) && (wxMINOR_VERSION <= 4)) WindowRef rootWindow = (WindowRef)GetParent()->MacGetRootWindow(); #else WindowRef rootWindow = (WindowRef)GetParent()->MacGetTopLevelWindowRef(); #endif ControlPartCode code; code = FindControl(localwhere, rootWindow, &control); if (code) { Rect outBounds; GetControlBounds((ControlRef)control, &outBounds); code = ::HandleControlClick(control, localwhere, modifiers, (ControlActionUPP)-1) ; } }}//// AudioUnitDialog methods//void EventListener(void *inUserData, AudioUnitCarbonView inView, const AudioUnitParameter *inParameter, AudioUnitCarbonViewEventID inEvent, const void *inEventParam){ // We're not actually using this yet...}enum { PreviewID = 1};BEGIN_EVENT_TABLE(AudioUnitDialog, wxDialog) EVT_BUTTON(wxID_OK, AudioUnitDialog::OnOK) EVT_BUTTON(wxID_CANCEL, AudioUnitDialog::OnCancel) EVT_BUTTON(PreviewID, AudioUnitDialog::OnPreview)END_EVENT_TABLE()AudioUnitDialog::AudioUnitDialog(wxWindow *parent, wxWindowID id, wxString title, AudioUnit unit, AudioUnitCarbonView carbonView): mUnit(unit){ long style = wxDEFAULT_DIALOG_STYLE; #if ((wxMAJOR_VERSION == 2) && (wxMINOR_VERSION <= 4)) wxDialog::Create(parent, id, title, wxDefaultPosition, wxSize(500, 400), style, title); #else // wxMac 2.5 version, all of this is just to attempt to turn off // compositing... // From wxDialog::Create SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); style |= wxTAB_TRAVERSAL; // From wxTopLevelWindow::Create wxTopLevelWindow::Init(); m_windowStyle = style; // We added this line because many plug-ins are not happy with // compositing...Requires a patch to wx such that the line // "attr |= kWindowCompositingAttribute" only happens // if m_macUsesCompositing is true... m_macUsesCompositing = false; // Rest of wxTopLevelWindow::Create SetName(title); m_windowId = id == -1 ? NewControlId() : id; MacCreateRealWindow(title, wxDefaultPosition, wxSize(500, 400), MacRemoveBordersFromStyle(style) , title) ; SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); wxTopLevelWindows.Append(this); if ( parent ) parent->AddChild(this); #endif // // On to our own code // WindowRef windowRef = (WindowRef)MacGetWindowRef(); ComponentResult auResult; ControlRef rootControl = NULL; GetRootControl(windowRef, &rootControl); int width = 500; int height = 400; Float32Point location = {0, 0}; Float32Point size = {width, height}; ControlRef audioUnitControl = NULL; auResult = AudioUnitCarbonViewCreate(carbonView, unit, windowRef, rootControl, &location, &size, &audioUnitControl); AudioUnitCarbonViewSetEventListener(carbonView, EventListener, this); mMainSizer = new wxBoxSizer(wxVERTICAL); if (auResult == 0) { mGUIControl = new AudioUnitGUIControl(this, -1, wxPoint(0, 0), wxSize(width, height), audioUnitControl); // Eventually, to handle resizing controls, call: // AudioUnitCarbonViewSetEventListener with the event: // kEventControlBoundsChanged mMainSizer->Add(mGUIControl, 1, wxEXPAND); mGUIControl->SetFocus(); } else mGUIControl = NULL; wxBoxSizer *hSizer = new wxBoxSizer(wxHORIZONTAL); wxButton *preview = new wxButton(this, PreviewID, "Preview"); hSizer->Add(preview, 0, wxALL, 10); hSizer->Add(10, 10); wxButton *ok = new wxButton(this, wxID_OK, "OK"); ok->SetDefault(); ok->SetFocus(); hSizer->Add(ok, 0, wxALL, 10); wxButton *cancel = new wxButton(this, wxID_CANCEL, "Cancel"); hSizer->Add(cancel, 0, wxALL, 10); mMainSizer->Add(hSizer, 0, wxALIGN_CENTER); this->SetAutoLayout(true); this->SetSizer(mMainSizer); mMainSizer->Fit(this); mMainSizer->SetSizeHints(this); #if ((wxMAJOR_VERSION == 2) && (wxMINOR_VERSION <= 4)) // Nothing more to to... #else // // Remove the wx event handler (because it interferes with the // event handlers that other GUIs install) // ::RemoveEventHandler((EventHandlerRef)MacGetEventHandler()); #endif}void AudioUnitDialog::OnOK(wxCommandEvent &event){ EndModal(wxID_OK);}void AudioUnitDialog::OnCancel(wxCommandEvent &event){ EndModal(wxID_CANCEL);}void AudioUnitDialog::OnPreview(wxCommandEvent &event){ // TODO}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -