157e2f323SJérôme Duval /* 257e2f323SJérôme Duval * Copyright 2005, Jérôme Duval. All rights reserved. 357e2f323SJérôme Duval * Distributed under the terms of the MIT License. 457e2f323SJérôme Duval * 5ad0c65eaSJérôme Duval * Inspired by SoundCapture from Be newsletter (Media Kit Basics: 6ad0c65eaSJérôme Duval * Consumers and Producers) 757e2f323SJérôme Duval */ 857e2f323SJérôme Duval 957e2f323SJérôme Duval #include <Application.h> 1057e2f323SJérôme Duval #include <Alert.h> 1157e2f323SJérôme Duval #include <Debug.h> 1257e2f323SJérôme Duval #include <Screen.h> 1357e2f323SJérôme Duval #include <Button.h> 1457e2f323SJérôme Duval #include <CheckBox.h> 1557e2f323SJérôme Duval #include <TextControl.h> 1657e2f323SJérôme Duval #include <MenuField.h> 1757e2f323SJérôme Duval #include <PopUpMenu.h> 1857e2f323SJérôme Duval #include <MenuItem.h> 1957e2f323SJérôme Duval #include <Box.h> 2057e2f323SJérôme Duval #include <ScrollView.h> 2157e2f323SJérôme Duval #include <Beep.h> 2257e2f323SJérôme Duval #include <StringView.h> 2357e2f323SJérôme Duval #include <String.h> 2457e2f323SJérôme Duval #include <Slider.h> 2557e2f323SJérôme Duval #include <Message.h> 2657e2f323SJérôme Duval 2757e2f323SJérôme Duval #include <Path.h> 2857e2f323SJérôme Duval #include <FindDirectory.h> 2957e2f323SJérôme Duval #include <MediaAddOn.h> 3057e2f323SJérôme Duval 3157e2f323SJérôme Duval #include <SoundPlayer.h> 3257e2f323SJérôme Duval 3357e2f323SJérôme Duval #include <assert.h> 3457e2f323SJérôme Duval #include <stdio.h> 3557e2f323SJérôme Duval #include <string.h> 3657e2f323SJérôme Duval #include <stdlib.h> 3757e2f323SJérôme Duval #include <ctype.h> 3857e2f323SJérôme Duval #include <unistd.h> 39338b8dc3SIngo Weinhold #include <fcntl.h> 4057e2f323SJérôme Duval 4157e2f323SJérôme Duval #include <MediaRoster.h> 4257e2f323SJérôme Duval #include <TimeSource.h> 43019ed09bSJérôme Duval #include <NodeInfo.h> 4457e2f323SJérôme Duval 4557e2f323SJérôme Duval #include "RecorderWindow.h" 4657e2f323SJérôme Duval #include "SoundConsumer.h" 4757e2f323SJérôme Duval #include "FileUtils.h" 4857e2f323SJérôme Duval 4957e2f323SJérôme Duval #if ! NDEBUG 5057e2f323SJérôme Duval #define FPRINTF(args) fprintf args 5157e2f323SJérôme Duval #else 5257e2f323SJérôme Duval #define FPRINTF(args) 5357e2f323SJérôme Duval #endif 5457e2f323SJérôme Duval 5557e2f323SJérôme Duval #define DEATH FPRINTF 5657e2f323SJérôme Duval #define CONNECT FPRINTF 5757e2f323SJérôme Duval #define WINDOW FPRINTF 5857e2f323SJérôme Duval 593ee96407SJérôme Duval #undef B_TRANSLATE_CONTEXT 603ee96407SJérôme Duval #define B_TRANSLATE_CONTEXT "RecorderWindow" 613ee96407SJérôme Duval 623ee96407SJérôme Duval 6357e2f323SJérôme Duval // default window positioning 6457e2f323SJérôme Duval static const float MIN_WIDTH = 400.0f; 6557e2f323SJérôme Duval static const float MIN_HEIGHT = 336.0f; 6657e2f323SJérôme Duval static const float XPOS = 100.0f; 6757e2f323SJérôme Duval static const float YPOS = 200.0f; 6857e2f323SJérôme Duval 69ad0c65eaSJérôme Duval #define FOURCC(a,b,c,d) ((((uint32)(d)) << 24) | (((uint32)(c)) << 16) \ 70ad0c65eaSJérôme Duval | (((uint32)(b)) << 8) | ((uint32)(a))) 7157e2f323SJérôme Duval 7257e2f323SJérôme Duval struct riff_struct 7357e2f323SJérôme Duval { 7457e2f323SJérôme Duval uint32 riff_id; // 'RIFF' 7557e2f323SJérôme Duval uint32 len; 7657e2f323SJérôme Duval uint32 wave_id; // 'WAVE' 7757e2f323SJérôme Duval }; 7857e2f323SJérôme Duval 7957e2f323SJérôme Duval struct chunk_struct 8057e2f323SJérôme Duval { 8157e2f323SJérôme Duval uint32 fourcc; 8257e2f323SJérôme Duval uint32 len; 8357e2f323SJérôme Duval }; 8457e2f323SJérôme Duval 8557e2f323SJérôme Duval struct format_struct 8657e2f323SJérôme Duval { 8757e2f323SJérôme Duval uint16 format_tag; 8857e2f323SJérôme Duval uint16 channels; 8957e2f323SJérôme Duval uint32 samples_per_sec; 9057e2f323SJérôme Duval uint32 avg_bytes_per_sec; 9157e2f323SJérôme Duval uint16 block_align; 9257e2f323SJérôme Duval uint16 bits_per_sample; 9357e2f323SJérôme Duval }; 9457e2f323SJérôme Duval 9557e2f323SJérôme Duval 9657e2f323SJérôme Duval struct wave_struct 9757e2f323SJérôme Duval { 9857e2f323SJérôme Duval struct riff_struct riff; 9957e2f323SJérôme Duval struct chunk_struct format_chunk; 10057e2f323SJérôme Duval struct format_struct format; 10157e2f323SJérôme Duval struct chunk_struct data_chunk; 10257e2f323SJérôme Duval }; 10357e2f323SJérôme Duval 10457e2f323SJérôme Duval 10557e2f323SJérôme Duval RecorderWindow::RecorderWindow() : 106ad0c65eaSJérôme Duval BWindow(BRect(XPOS,YPOS,XPOS+MIN_WIDTH,YPOS+MIN_HEIGHT), "SoundRecorder", 107ad0c65eaSJérôme Duval B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_NOT_V_RESIZABLE | B_NOT_ZOOMABLE), 10857e2f323SJérôme Duval fPlayer(NULL), 1095fa77532SJérôme Duval fSoundList(NULL), 11057e2f323SJérôme Duval fPlayFile(NULL), 11157e2f323SJérôme Duval fPlayTrack(NULL), 1127942339dSJérôme Duval fPlayFrames(0), 113948356deSJérôme Duval fLooping(false), 1145fa77532SJérôme Duval fSavePanel(NULL), 115*cff7eda5SWim van der Meer fInitCheck(B_OK) 11657e2f323SJérôme Duval { 11757e2f323SJérôme Duval fRoster = NULL; 11857e2f323SJérôme Duval fRecordButton = NULL; 11957e2f323SJérôme Duval fPlayButton = NULL; 12057e2f323SJérôme Duval fStopButton = NULL; 12157e2f323SJérôme Duval fSaveButton = NULL; 122948356deSJérôme Duval fLoopButton = NULL; 12357e2f323SJérôme Duval fInputField = NULL; 12457e2f323SJérôme Duval fRecordNode = 0; 12557e2f323SJérôme Duval fRecording = false; 12657e2f323SJérôme Duval fTempCount = -1; 12757e2f323SJérôme Duval fButtonState = btnPaused; 12857e2f323SJérôme Duval 12957e2f323SJérôme Duval CalcSizes(MIN_WIDTH, MIN_HEIGHT); 13057e2f323SJérôme Duval 1313ee96407SJérôme Duval be_locale->GetAppCatalog(&fAppCatalog); 1323ee96407SJérôme Duval 1333ee96407SJérôme Duval SetTitle(B_TRANSLATE("SoundRecorder")); 1343ee96407SJérôme Duval 1355fa77532SJérôme Duval fInitCheck = InitWindow(); 1365fa77532SJérôme Duval if (fInitCheck != B_OK) { 137bdb1d3acSJérôme Duval if (fInitCheck == B_NAME_NOT_FOUND) 1383ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot find default audio hardware"), 1393ee96407SJérôme Duval fInitCheck); 140bdb1d3acSJérôme Duval else 1413ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot connect to media server"), 1423ee96407SJérôme Duval fInitCheck); 1435fa77532SJérôme Duval PostMessage(B_QUIT_REQUESTED); 1445fa77532SJérôme Duval } else 1455fa77532SJérôme Duval Show(); 14657e2f323SJérôme Duval } 14757e2f323SJérôme Duval 1488e32a27eSPhilippe Saint-Pierre 14957e2f323SJérôme Duval RecorderWindow::~RecorderWindow() 15057e2f323SJérôme Duval { 151ad0c65eaSJérôme Duval // The sound consumer and producer are Nodes; it has to be released and the Roster 15257e2f323SJérôme Duval // will reap it when it's done. 15357e2f323SJérôme Duval if (fRecordNode) { 15457e2f323SJérôme Duval fRecordNode->Release(); 15557e2f323SJérôme Duval } 15657e2f323SJérôme Duval if (fPlayer) { 15757e2f323SJérôme Duval delete fPlayer; 15857e2f323SJérôme Duval } 1597942339dSJérôme Duval 1607942339dSJérôme Duval if (fPlayTrack && fPlayFile) 1617942339dSJérôme Duval fPlayFile->ReleaseTrack(fPlayTrack); 1627942339dSJérôme Duval if (fPlayFile) 1637942339dSJérôme Duval delete fPlayFile; 1647942339dSJérôme Duval fPlayTrack = NULL; 1657942339dSJérôme Duval fPlayFile = NULL; 1667942339dSJérôme Duval 16757e2f323SJérôme Duval // Clean up items in list view. 16857e2f323SJérôme Duval if (fSoundList) { 16957e2f323SJérôme Duval fSoundList->DeselectAll(); 170ad0c65eaSJérôme Duval for (int i = 0; i < fSoundList->CountItems(); i++) { 171ad0c65eaSJérôme Duval WINDOW((stderr, "clean up item %d\n", i+1)); 172ad0c65eaSJérôme Duval SoundListItem* item = dynamic_cast<SoundListItem *>(fSoundList->ItemAt(i)); 17357e2f323SJérôme Duval if (item) { 17457e2f323SJérôme Duval if (item->IsTemp()) { 17557e2f323SJérôme Duval item->Entry().Remove(); // delete temp file 17657e2f323SJérôme Duval } 17757e2f323SJérôme Duval delete item; 17857e2f323SJérôme Duval } 17957e2f323SJérôme Duval } 18057e2f323SJérôme Duval fSoundList->MakeEmpty(); 18157e2f323SJérôme Duval } 18257e2f323SJérôme Duval // Clean up currently recording file, if any. 18357e2f323SJérôme Duval fRecEntry.Remove(); 18457e2f323SJérôme Duval fRecEntry.Unset(); 18505ea8535SKarsten Heimrich 18605ea8535SKarsten Heimrich delete fSavePanel; 18757e2f323SJérôme Duval } 18857e2f323SJérôme Duval 18957e2f323SJérôme Duval 1905fa77532SJérôme Duval status_t 1915fa77532SJérôme Duval RecorderWindow::InitCheck() 1925fa77532SJérôme Duval { 1935fa77532SJérôme Duval return fInitCheck; 1945fa77532SJérôme Duval } 1955fa77532SJérôme Duval 1965fa77532SJérôme Duval 19757e2f323SJérôme Duval void 19857e2f323SJérôme Duval RecorderWindow::CalcSizes(float min_wid, float min_hei) 19957e2f323SJérôme Duval { 20057e2f323SJérôme Duval // Set up size limits based on new screen size 2015e99b7dfSJérôme Duval BScreen screen(this); 20257e2f323SJérôme Duval BRect r = screen.Frame(); 20357e2f323SJérôme Duval float wid = r.Width()-12; 20457e2f323SJérôme Duval SetSizeLimits(min_wid, wid, min_hei, r.Height()-24); 20557e2f323SJérôme Duval 20657e2f323SJérôme Duval // Don't zoom to cover all of screen; user can resize last bit if necessary. 20757e2f323SJérôme Duval // This leaves other windows visible. 20857e2f323SJérôme Duval if (wid > 640) { 20957e2f323SJérôme Duval wid = 640 + (wid-640)/2; 21057e2f323SJérôme Duval } 21157e2f323SJérôme Duval SetZoomLimits(wid, r.Height()-24); 21257e2f323SJérôme Duval } 21357e2f323SJérôme Duval 21457e2f323SJérôme Duval 21557e2f323SJérôme Duval status_t 21657e2f323SJérôme Duval RecorderWindow::InitWindow() 21757e2f323SJérôme Duval { 21857e2f323SJérôme Duval BPopUpMenu * popup = 0; 21957e2f323SJérôme Duval status_t error; 22057e2f323SJérôme Duval 22157e2f323SJérôme Duval try { 22257e2f323SJérôme Duval // Find temp directory for recorded sounds. 22357e2f323SJérôme Duval BPath path; 22457e2f323SJérôme Duval if (!(error = find_directory(B_COMMON_TEMP_DIRECTORY, &path))) { 22557e2f323SJérôme Duval error = fTempDir.SetTo(path.Path()); 22657e2f323SJérôme Duval } 22757e2f323SJérôme Duval if (error < 0) { 22857e2f323SJérôme Duval goto bad_mojo; 22957e2f323SJérôme Duval } 23057e2f323SJérôme Duval 23157e2f323SJérôme Duval // Make sure the media roster is there (which means the server is there). 23257e2f323SJérôme Duval fRoster = BMediaRoster::Roster(&error); 23357e2f323SJérôme Duval if (!fRoster) { 23457e2f323SJérôme Duval goto bad_mojo; 23557e2f323SJérôme Duval } 23657e2f323SJérôme Duval 23757e2f323SJérôme Duval error = fRoster->GetAudioInput(&fAudioInputNode); 23857e2f323SJérôme Duval if (error < B_OK) { // there's no input? 23957e2f323SJérôme Duval goto bad_mojo; 24057e2f323SJérôme Duval } 24157e2f323SJérôme Duval 24257e2f323SJérôme Duval error = fRoster->GetAudioMixer(&fAudioMixerNode); 24357e2f323SJérôme Duval if (error < B_OK) { // there's no mixer? 24457e2f323SJérôme Duval goto bad_mojo; 24557e2f323SJérôme Duval } 24657e2f323SJérôme Duval 24757e2f323SJérôme Duval // Create our internal Node which records sound, and register it. 24857e2f323SJérôme Duval fRecordNode = new SoundConsumer("Sound Recorder"); 24957e2f323SJérôme Duval error = fRoster->RegisterNode(fRecordNode); 25057e2f323SJérôme Duval if (error < B_OK) { 25157e2f323SJérôme Duval goto bad_mojo; 25257e2f323SJérôme Duval } 25357e2f323SJérôme Duval 25457e2f323SJérôme Duval // Create the window header with controls 25557e2f323SJérôme Duval BRect r(Bounds()); 25657e2f323SJérôme Duval r.bottom = r.top + 175; 257ad0c65eaSJérôme Duval BBox *background = new BBox(r, "_background", B_FOLLOW_LEFT_RIGHT 258ad0c65eaSJérôme Duval | B_FOLLOW_TOP, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, B_NO_BORDER); 25957e2f323SJérôme Duval AddChild(background); 26057e2f323SJérôme Duval 26157e2f323SJérôme Duval 26257e2f323SJérôme Duval 26357e2f323SJérôme Duval r = background->Bounds(); 264a4797804SJérôme Duval r.left = 0; 2658881ad65SJérôme Duval r.right = r.left + 38; 26657e2f323SJérôme Duval r.bottom = r.top + 104; 26757e2f323SJérôme Duval fVUView = new VUView(r, B_FOLLOW_LEFT|B_FOLLOW_TOP); 26857e2f323SJérôme Duval background->AddChild(fVUView); 26957e2f323SJérôme Duval 27057e2f323SJérôme Duval r = background->Bounds(); 2718881ad65SJérôme Duval r.left = r.left + 40; 27257e2f323SJérôme Duval r.bottom = r.top + 104; 27357e2f323SJérôme Duval fScopeView = new ScopeView(r, B_FOLLOW_LEFT_RIGHT|B_FOLLOW_TOP); 27457e2f323SJérôme Duval background->AddChild(fScopeView); 27557e2f323SJérôme Duval 27657e2f323SJérôme Duval r = background->Bounds(); 27757e2f323SJérôme Duval r.left = 2; 27857e2f323SJérôme Duval r.right -= 26; 27957e2f323SJérôme Duval r.top = 115; 28057e2f323SJérôme Duval r.bottom = r.top + 30; 281ad0c65eaSJérôme Duval fTrackSlider = new TrackSlider(r, "trackSlider", new BMessage(POSITION_CHANGED), 282ad0c65eaSJérôme Duval B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); 28357e2f323SJérôme Duval background->AddChild(fTrackSlider); 28457e2f323SJérôme Duval 28557e2f323SJérôme Duval BRect buttonRect; 28657e2f323SJérôme Duval 28757e2f323SJérôme Duval // Button for rewinding 28857e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), kSkipButtonSize); 28957e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().LeftBottom() - BPoint(-7, 25)); 2903ee96407SJérôme Duval fRewindButton = new TransportButton(buttonRect, B_TRANSLATE("Rewind"), 291ad0c65eaSJérôme Duval kSkipBackBitmapBits, kPressedSkipBackBitmapBits, 292ad0c65eaSJérôme Duval kDisabledSkipBackBitmapBits, new BMessage(REWIND)); 29357e2f323SJérôme Duval background->AddChild(fRewindButton); 29457e2f323SJérôme Duval 29557e2f323SJérôme Duval // Button for stopping recording or playback 29657e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), kStopButtonSize); 29757e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().LeftBottom() - BPoint(-48, 25)); 2983ee96407SJérôme Duval fStopButton = new TransportButton(buttonRect, B_TRANSLATE("Stop"), 299ad0c65eaSJérôme Duval kStopButtonBitmapBits, kPressedStopButtonBitmapBits, 300ad0c65eaSJérôme Duval kDisabledStopButtonBitmapBits, new BMessage(STOP)); 30157e2f323SJérôme Duval background->AddChild(fStopButton); 30257e2f323SJérôme Duval 30357e2f323SJérôme Duval // Button for starting playback of selected sound 30457e2f323SJérôme Duval BRect playRect(BPoint(0,0), kPlayButtonSize); 30557e2f323SJérôme Duval playRect.OffsetTo(background->Bounds().LeftBottom() - BPoint(-82, 25)); 3063ee96407SJérôme Duval fPlayButton = new PlayPauseButton(playRect, B_TRANSLATE("Play"), 30757e2f323SJérôme Duval new BMessage(PLAY), new BMessage(PLAY_PERIOD), ' ', 0); 30857e2f323SJérôme Duval background->AddChild(fPlayButton); 30957e2f323SJérôme Duval 31057e2f323SJérôme Duval // Button for forwarding 31157e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), kSkipButtonSize); 31257e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().LeftBottom() - BPoint(-133, 25)); 3133ee96407SJérôme Duval fForwardButton = new TransportButton(buttonRect, B_TRANSLATE("Forward"), 314ad0c65eaSJérôme Duval kSkipForwardBitmapBits, kPressedSkipForwardBitmapBits, 315ad0c65eaSJérôme Duval kDisabledSkipForwardBitmapBits, new BMessage(FORWARD)); 31657e2f323SJérôme Duval background->AddChild(fForwardButton); 31757e2f323SJérôme Duval 31857e2f323SJérôme Duval // Button to start recording (or waiting for sound) 31957e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), kRecordButtonSize); 32057e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().LeftBottom() - BPoint(-174, 25)); 3213ee96407SJérôme Duval fRecordButton = new RecordButton(buttonRect, B_TRANSLATE("Record"), 32257e2f323SJérôme Duval new BMessage(RECORD), new BMessage(RECORD_PERIOD)); 32357e2f323SJérôme Duval background->AddChild(fRecordButton); 32457e2f323SJérôme Duval 32557e2f323SJérôme Duval // Button for saving selected sound 32657e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), kDiskButtonSize); 32757e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().LeftBottom() - BPoint(-250, 21)); 3283ee96407SJérôme Duval fSaveButton = new TransportButton(buttonRect, B_TRANSLATE("Save"), 329ad0c65eaSJérôme Duval kDiskButtonBitmapsBits, kPressedDiskButtonBitmapsBits, 330ad0c65eaSJérôme Duval kDisabledDiskButtonBitmapsBits, new BMessage(SAVE)); 33157e2f323SJérôme Duval fSaveButton->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_TOP); 33257e2f323SJérôme Duval background->AddChild(fSaveButton); 33357e2f323SJérôme Duval 334948356deSJérôme Duval // Button Loop 335948356deSJérôme Duval buttonRect = BRect(BPoint(0,0), kArrowSize); 336948356deSJérôme Duval buttonRect.OffsetTo(background->Bounds().RightBottom() - BPoint(23, 48)); 3373ee96407SJérôme Duval fLoopButton = new DrawButton(buttonRect, B_TRANSLATE("Loop"), 338948356deSJérôme Duval kLoopArrowBits, kArrowBits, new BMessage(LOOP)); 339948356deSJérôme Duval fLoopButton->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_TOP); 340948356deSJérôme Duval fLoopButton->SetTarget(this); 341948356deSJérôme Duval background->AddChild(fLoopButton); 342948356deSJérôme Duval 34357e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), kSpeakerIconBitmapSize); 34457e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().RightBottom() - BPoint(121, 17)); 345ad0c65eaSJérôme Duval SpeakerView *speakerView = new SpeakerView(buttonRect, 346ad0c65eaSJérôme Duval B_FOLLOW_LEFT | B_FOLLOW_TOP); 34757e2f323SJérôme Duval speakerView->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_TOP); 34857e2f323SJérôme Duval background->AddChild(speakerView); 34957e2f323SJérôme Duval 35057e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), BPoint(84, 19)); 35157e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().RightBottom() - BPoint(107, 20)); 352ad0c65eaSJérôme Duval fVolumeSlider = new VolumeSlider(buttonRect, "volumeSlider", 353ad0c65eaSJérôme Duval B_FOLLOW_LEFT | B_FOLLOW_TOP); 35457e2f323SJérôme Duval fVolumeSlider->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_TOP); 35557e2f323SJérôme Duval background->AddChild(fVolumeSlider); 35657e2f323SJérôme Duval 35757e2f323SJérôme Duval // Button to mask/see sounds list 35857e2f323SJérôme Duval buttonRect = BRect(BPoint(0,0), kUpDownButtonSize); 35957e2f323SJérôme Duval buttonRect.OffsetTo(background->Bounds().RightBottom() - BPoint(21, 25)); 36057e2f323SJérôme Duval fUpDownButton = new UpDownButton(buttonRect, new BMessage(VIEW_LIST)); 36157e2f323SJérôme Duval fUpDownButton->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_TOP); 36257e2f323SJérôme Duval background->AddChild(fUpDownButton); 36357e2f323SJérôme Duval 36457e2f323SJérôme Duval r = Bounds(); 36557e2f323SJérôme Duval r.top = background->Bounds().bottom + 1; 36657e2f323SJérôme Duval fBottomBox = new BBox(r, "bottomBox", B_FOLLOW_ALL); 36757e2f323SJérôme Duval fBottomBox->SetBorder(B_NO_BORDER); 36857e2f323SJérôme Duval AddChild(fBottomBox); 36957e2f323SJérôme Duval 37057e2f323SJérôme Duval // The actual list of recorded sounds (initially empty) sits 37157e2f323SJérôme Duval // below the header with the controls. 37257e2f323SJérôme Duval r = fBottomBox->Bounds(); 37357e2f323SJérôme Duval r.left += 190; 37457e2f323SJérôme Duval r.InsetBy(10, 10); 37557e2f323SJérôme Duval r.left -= 10; 37657e2f323SJérôme Duval r.top += 4; 37757e2f323SJérôme Duval r.right -= B_V_SCROLL_BAR_WIDTH; 37857e2f323SJérôme Duval r.bottom -= 25; 3793ee96407SJérôme Duval fSoundList = new SoundListView(r, B_TRANSLATE("Sound List"), 3803ee96407SJérôme Duval B_FOLLOW_ALL); 38157e2f323SJérôme Duval fSoundList->SetSelectionMessage(new BMessage(SOUND_SELECTED)); 38257e2f323SJérôme Duval fSoundList->SetViewColor(216, 216, 216); 383ad0c65eaSJérôme Duval BScrollView *scroller = new BScrollView("scroller", fSoundList, 384ad0c65eaSJérôme Duval B_FOLLOW_ALL, 0, false, true, B_FANCY_BORDER); 38557e2f323SJérôme Duval fBottomBox->AddChild(scroller); 38657e2f323SJérôme Duval 38757e2f323SJérôme Duval r = fBottomBox->Bounds(); 38857e2f323SJérôme Duval r.right = r.left + 190; 38957e2f323SJérôme Duval r.bottom -= 25; 39057e2f323SJérôme Duval r.InsetBy(10, 8); 39157e2f323SJérôme Duval r.top -= 1; 39257e2f323SJérôme Duval fFileInfoBox = new BBox(r, "fileinfo", B_FOLLOW_LEFT | B_FOLLOW_BOTTOM); 3933ee96407SJérôme Duval fFileInfoBox->SetLabel(B_TRANSLATE("File info")); 39457e2f323SJérôme Duval 39557e2f323SJérôme Duval r = fFileInfoBox->Bounds(); 39657e2f323SJérôme Duval r.left = 8; 39757e2f323SJérôme Duval r.top = 13; 39857e2f323SJérôme Duval r.bottom = r.top + 15; 39957e2f323SJérôme Duval r.right -= 10; 4003ee96407SJérôme Duval fFilename = new BStringView(r, "filename", B_TRANSLATE("File name:")); 40157e2f323SJérôme Duval fFileInfoBox->AddChild(fFilename); 40257e2f323SJérôme Duval r.top += 13; 40357e2f323SJérôme Duval r.bottom = r.top + 15; 4043ee96407SJérôme Duval fFormat = new BStringView(r, "format", B_TRANSLATE("Format:")); 40557e2f323SJérôme Duval fFileInfoBox->AddChild(fFormat); 40657e2f323SJérôme Duval r.top += 13; 40757e2f323SJérôme Duval r.bottom = r.top + 15; 4083ee96407SJérôme Duval fCompression = new BStringView(r, "compression", 4093ee96407SJérôme Duval B_TRANSLATE("Compression:")); 41057e2f323SJérôme Duval fFileInfoBox->AddChild(fCompression); 41157e2f323SJérôme Duval r.top += 13; 41257e2f323SJérôme Duval r.bottom = r.top + 15; 4133ee96407SJérôme Duval fChannels = new BStringView(r, "channels", B_TRANSLATE("Channels:")); 41457e2f323SJérôme Duval fFileInfoBox->AddChild(fChannels); 41557e2f323SJérôme Duval r.top += 13; 41657e2f323SJérôme Duval r.bottom = r.top + 15; 4173ee96407SJérôme Duval fSampleSize = new BStringView(r, "samplesize", 4183ee96407SJérôme Duval B_TRANSLATE("Sample size:")); 41957e2f323SJérôme Duval fFileInfoBox->AddChild(fSampleSize); 42057e2f323SJérôme Duval r.top += 13; 42157e2f323SJérôme Duval r.bottom = r.top + 15; 4223ee96407SJérôme Duval fSampleRate = new BStringView(r, "samplerate", 4233ee96407SJérôme Duval B_TRANSLATE("Sample rate:")); 42457e2f323SJérôme Duval fFileInfoBox->AddChild(fSampleRate); 42557e2f323SJérôme Duval r.top += 13; 42657e2f323SJérôme Duval r.bottom = r.top + 15; 4273ee96407SJérôme Duval fDuration = new BStringView(r, "duration", B_TRANSLATE("Duration:")); 42857e2f323SJérôme Duval fFileInfoBox->AddChild(fDuration); 42957e2f323SJérôme Duval 43057e2f323SJérôme Duval // Input selection lists all available physical inputs that produce 43157e2f323SJérôme Duval // buffers with B_MEDIA_RAW_AUDIO format data. 4323ee96407SJérôme Duval popup = new BPopUpMenu(B_TRANSLATE("Input")); 433a8ee8b8fSStephan Aßmus const int maxInputCount = 64; 434a8ee8b8fSStephan Aßmus dormant_node_info dni[maxInputCount]; 43557e2f323SJérôme Duval 436a8ee8b8fSStephan Aßmus int32 real_count = maxInputCount; 43757e2f323SJérôme Duval media_format output_format; 43857e2f323SJérôme Duval output_format.type = B_MEDIA_RAW_AUDIO; 43957e2f323SJérôme Duval output_format.u.raw_audio = media_raw_audio_format::wildcard; 44057e2f323SJérôme Duval error = fRoster->GetDormantNodes(dni, &real_count, 0, &output_format, 44157e2f323SJérôme Duval 0, B_BUFFER_PRODUCER | B_PHYSICAL_INPUT); 442a8ee8b8fSStephan Aßmus if (real_count > maxInputCount) { 443a8ee8b8fSStephan Aßmus WINDOW((stderr, "dropped %ld inputs\n", real_count - maxInputCount)); 444a8ee8b8fSStephan Aßmus real_count = maxInputCount; 44557e2f323SJérôme Duval } 4467974d3dcSStephan Aßmus char selected_name[B_MEDIA_NAME_LENGTH] = "Default input"; 44757e2f323SJérôme Duval BMessage * msg; 44857e2f323SJérôme Duval BMenuItem * item; 449ad0c65eaSJérôme Duval for (int i = 0; i < real_count; i++) { 45057e2f323SJérôme Duval msg = new BMessage(INPUT_SELECTED); 451ad0c65eaSJérôme Duval msg->AddData("node", B_RAW_TYPE, &dni[i], sizeof(dni[i])); 452ad0c65eaSJérôme Duval item = new BMenuItem(dni[i].name, msg); 45357e2f323SJérôme Duval popup->AddItem(item); 45457e2f323SJérôme Duval media_node_id ni[12]; 45557e2f323SJérôme Duval int32 ni_count = 12; 456ad0c65eaSJérôme Duval error = fRoster->GetInstancesFor(dni[i].addon, dni[i].flavor_id, 457ad0c65eaSJérôme Duval ni, &ni_count); 45857e2f323SJérôme Duval if (error == B_OK) 459ad0c65eaSJérôme Duval for (int j = 0; j < ni_count; j++) 460ad0c65eaSJérôme Duval if (ni[j] == fAudioInputNode.node) { 461ad0c65eaSJérôme Duval strcpy(selected_name, dni[i].name); 46257e2f323SJérôme Duval break; 46357e2f323SJérôme Duval } 46457e2f323SJérôme Duval } 46557e2f323SJérôme Duval 46657e2f323SJérôme Duval // Create the actual widget 46757e2f323SJérôme Duval BRect frame(fBottomBox->Bounds()); 46857e2f323SJérôme Duval r = frame; 46957e2f323SJérôme Duval r.left = 42; 47057e2f323SJérôme Duval r.right = (r.left + r.right) / 2; 47157e2f323SJérôme Duval r.InsetBy(10,10); 47257e2f323SJérôme Duval r.top = r.bottom - 18; 4733ee96407SJérôme Duval fInputField = new BMenuField(r, "Input", B_TRANSLATE("Input:"), popup); 4743ee96407SJérôme Duval fInputField->SetDivider(fInputField->StringWidth(B_TRANSLATE("Input:")) + 4.0f); 47557e2f323SJérôme Duval fBottomBox->AddChild(fInputField); 47657e2f323SJérôme Duval 47757e2f323SJérôme Duval fBottomBox->AddChild(fFileInfoBox); 47857e2f323SJérôme Duval 47957e2f323SJérôme Duval fBottomBox->Hide(); 48057e2f323SJérôme Duval CalcSizes(Frame().Width(), MIN_HEIGHT-161); 48157e2f323SJérôme Duval ResizeTo(Frame().Width(), MIN_HEIGHT-161); 48257e2f323SJérôme Duval 48357e2f323SJérôme Duval 48457e2f323SJérôme Duval popup->Superitem()->SetLabel(selected_name); 48557e2f323SJérôme Duval 48657e2f323SJérôme Duval // Make sure the save panel is happy. 4875fa77532SJérôme Duval fSavePanel = new BFilePanel(B_SAVE_PANEL); 4885fa77532SJérôme Duval fSavePanel->SetTarget(this); 48957e2f323SJérôme Duval } 49057e2f323SJérôme Duval catch (...) { 49157e2f323SJérôme Duval goto bad_mojo; 49257e2f323SJérôme Duval } 49357e2f323SJérôme Duval UpdateButtons(); 49457e2f323SJérôme Duval return B_OK; 49557e2f323SJérôme Duval 49657e2f323SJérôme Duval // Error handling. 49757e2f323SJérôme Duval bad_mojo: 49857e2f323SJérôme Duval if (error >= 0) { 49957e2f323SJérôme Duval error = B_ERROR; 50057e2f323SJérôme Duval } 50157e2f323SJérôme Duval if (fRecordNode) { 50257e2f323SJérôme Duval fRecordNode->Release(); 50357e2f323SJérôme Duval } 50457e2f323SJérôme Duval 50557e2f323SJérôme Duval delete fPlayer; 50657e2f323SJérôme Duval if (!fInputField) { 50757e2f323SJérôme Duval delete popup; 50857e2f323SJérôme Duval } 50957e2f323SJérôme Duval return error; 51057e2f323SJérôme Duval } 51157e2f323SJérôme Duval 51257e2f323SJérôme Duval 51357e2f323SJérôme Duval bool 51457e2f323SJérôme Duval RecorderWindow::QuitRequested() // this means Close pressed 51557e2f323SJérôme Duval { 51657e2f323SJérôme Duval StopRecording(); 51757e2f323SJérôme Duval StopPlaying(); 51857e2f323SJérôme Duval be_app->PostMessage(B_QUIT_REQUESTED); 51957e2f323SJérôme Duval return true; 52057e2f323SJérôme Duval } 52157e2f323SJérôme Duval 52257e2f323SJérôme Duval 52357e2f323SJérôme Duval void 52457e2f323SJérôme Duval RecorderWindow::MessageReceived(BMessage * message) 52557e2f323SJérôme Duval { 52657e2f323SJérôme Duval // Your average generic message dispatching switch() statement. 52757e2f323SJérôme Duval switch (message->what) { 52857e2f323SJérôme Duval case INPUT_SELECTED: 52957e2f323SJérôme Duval Input(message); 53057e2f323SJérôme Duval break; 53157e2f323SJérôme Duval case SOUND_SELECTED: 53257e2f323SJérôme Duval Selected(message); 53357e2f323SJérôme Duval break; 53457e2f323SJérôme Duval case STOP_PLAYING: 53557e2f323SJérôme Duval StopPlaying(); 53657e2f323SJérôme Duval break; 53757e2f323SJérôme Duval case STOP_RECORDING: 53857e2f323SJérôme Duval StopRecording(); 53957e2f323SJérôme Duval break; 54057e2f323SJérôme Duval case PLAY_PERIOD: 54157e2f323SJérôme Duval if (fPlayer) { 54257e2f323SJérôme Duval if (fPlayer->HasData()) 54357e2f323SJérôme Duval fPlayButton->SetPlaying(); 54457e2f323SJérôme Duval else 54557e2f323SJérôme Duval fPlayButton->SetPaused(); 54657e2f323SJérôme Duval } 54757e2f323SJérôme Duval break; 54857e2f323SJérôme Duval case RECORD_PERIOD: 54957e2f323SJérôme Duval fRecordButton->SetRecording(); 55057e2f323SJérôme Duval break; 55157e2f323SJérôme Duval case RECORD: 55257e2f323SJérôme Duval Record(message); 55357e2f323SJérôme Duval break; 55457e2f323SJérôme Duval case STOP: 55557e2f323SJérôme Duval Stop(message); 55657e2f323SJérôme Duval break; 55757e2f323SJérôme Duval case PLAY: 55857e2f323SJérôme Duval Play(message); 55957e2f323SJérôme Duval break; 56057e2f323SJérôme Duval case SAVE: 56157e2f323SJérôme Duval Save(message); 56257e2f323SJérôme Duval break; 56357e2f323SJérôme Duval case B_SAVE_REQUESTED: 56457e2f323SJérôme Duval DoSave(message); 56557e2f323SJérôme Duval break; 56657e2f323SJérôme Duval case VIEW_LIST: 56757e2f323SJérôme Duval if (fUpDownButton->Value() == B_CONTROL_ON) { 56857e2f323SJérôme Duval fBottomBox->Show(); 56957e2f323SJérôme Duval CalcSizes(Frame().Width(), MIN_HEIGHT); 57057e2f323SJérôme Duval ResizeTo(Frame().Width(), MIN_HEIGHT); 57157e2f323SJérôme Duval } else { 57257e2f323SJérôme Duval fBottomBox->Hide(); 57357e2f323SJérôme Duval CalcSizes(Frame().Width(), MIN_HEIGHT-161); 57457e2f323SJérôme Duval ResizeTo(Frame().Width(), MIN_HEIGHT-161); 57557e2f323SJérôme Duval 57657e2f323SJérôme Duval } 57757e2f323SJérôme Duval break; 57857e2f323SJérôme Duval case UPDATE_TRACKSLIDER: 57957e2f323SJérôme Duval { 58057e2f323SJérôme Duval bigtime_t timestamp = fPlayTrack->CurrentTime(); 58157e2f323SJérôme Duval fTrackSlider->SetMainTime(timestamp, false); 58257e2f323SJérôme Duval fScopeView->SetMainTime(timestamp); 58357e2f323SJérôme Duval } 58457e2f323SJérôme Duval break; 58557e2f323SJérôme Duval case POSITION_CHANGED: 58657e2f323SJérôme Duval { 58757e2f323SJérôme Duval bigtime_t right, left, main; 58857e2f323SJérôme Duval if (message->FindInt64("main", &main) == B_OK) { 58957e2f323SJérôme Duval if (fPlayTrack) { 59057e2f323SJérôme Duval fPlayTrack->SeekToTime(fTrackSlider->MainTime()); 59157e2f323SJérôme Duval fPlayFrame = fPlayTrack->CurrentFrame(); 59257e2f323SJérôme Duval } 59357e2f323SJérôme Duval fScopeView->SetMainTime(main); 59457e2f323SJérôme Duval } 59557e2f323SJérôme Duval if (message->FindInt64("right", &right) == B_OK) { 59657e2f323SJérôme Duval if (fPlayTrack) 597ad0c65eaSJérôme Duval fPlayLimit = MIN(fPlayFrames, 598ad0c65eaSJérôme Duval (off_t)(right * fPlayFormat.u.raw_audio.frame_rate/1000000LL)); 59957e2f323SJérôme Duval fScopeView->SetRightTime(right); 60057e2f323SJérôme Duval } 60157e2f323SJérôme Duval if (message->FindInt64("left", &left) == B_OK) { 60257e2f323SJérôme Duval fScopeView->SetLeftTime(left); 60357e2f323SJérôme Duval } 60457e2f323SJérôme Duval } 60557e2f323SJérôme Duval break; 606948356deSJérôme Duval case LOOP: 607948356deSJérôme Duval fLooping = fLoopButton->ButtonState(); 608948356deSJérôme Duval break; 60957e2f323SJérôme Duval case B_SIMPLE_DATA: 61057e2f323SJérôme Duval case B_REFS_RECEIVED: 61157e2f323SJérôme Duval { 61257e2f323SJérôme Duval RefsReceived(message); 61357e2f323SJérôme Duval break; 61457e2f323SJérôme Duval } 615019ed09bSJérôme Duval case B_COPY_TARGET: 616019ed09bSJérôme Duval CopyTarget(message); 617019ed09bSJérôme Duval break; 61857e2f323SJérôme Duval default: 61957e2f323SJérôme Duval BWindow::MessageReceived(message); 62057e2f323SJérôme Duval break; 62157e2f323SJérôme Duval } 62257e2f323SJérôme Duval } 62357e2f323SJérôme Duval 62457e2f323SJérôme Duval 62557e2f323SJérôme Duval void 62657e2f323SJérôme Duval RecorderWindow::Record(BMessage * message) 62757e2f323SJérôme Duval { 62857e2f323SJérôme Duval // User pressed Record button 62957e2f323SJérôme Duval fRecording = true; 63057e2f323SJérôme Duval if (fButtonState != btnPaused) { 63157e2f323SJérôme Duval StopRecording(); 63257e2f323SJérôme Duval return; // user is too fast on the mouse 63357e2f323SJérôme Duval } 63457e2f323SJérôme Duval SetButtonState(btnRecording); 63557e2f323SJérôme Duval fRecordButton->SetRecording(); 63657e2f323SJérôme Duval 63757e2f323SJérôme Duval char name[256]; 63857e2f323SJérôme Duval // Create a file with a temporary name 63957e2f323SJérôme Duval status_t err = NewTempName(name); 64057e2f323SJérôme Duval if (err < B_OK) { 6413ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot find an unused name to use for the " 6423ee96407SJérôme Duval "new recording"), err); 64357e2f323SJérôme Duval return; 64457e2f323SJérôme Duval } 64557e2f323SJérôme Duval // Find the file so we can refer to it later 64657e2f323SJérôme Duval err = fTempDir.FindEntry(name, &fRecEntry); 64757e2f323SJérôme Duval if (err < B_OK) { 6483ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot find the temporary file created to " 6493ee96407SJérôme Duval "hold the new recording"), err); 65057e2f323SJérôme Duval return; 65157e2f323SJérôme Duval } 65257e2f323SJérôme Duval err = fRecFile.SetTo(&fTempDir, name, O_RDWR); 65357e2f323SJérôme Duval if (err < B_OK) { 6543ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot open the temporary file created to " 6553ee96407SJérôme Duval "hold the new recording"), err); 65657e2f323SJérôme Duval fRecEntry.Unset(); 65757e2f323SJérôme Duval return; 65857e2f323SJérôme Duval } 65957e2f323SJérôme Duval // Reserve space on disk (creates fewer fragments) 66007db0c6fSJérôme Duval err = fRecFile.SetSize(4 * fRecordFormat.u.raw_audio.channel_count 6613ee96407SJérôme Duval * fRecordFormat.u.raw_audio.frame_rate 6623ee96407SJérôme Duval * (fRecordFormat.u.raw_audio.format 663ad0c65eaSJérôme Duval & media_raw_audio_format::B_AUDIO_SIZE_MASK)); 66457e2f323SJérôme Duval if (err < B_OK) { 6653ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot record a sound that long"), err); 66657e2f323SJérôme Duval fRecEntry.Remove(); 66757e2f323SJérôme Duval fRecEntry.Unset(); 66857e2f323SJérôme Duval return; 66957e2f323SJérôme Duval } 67057e2f323SJérôme Duval fRecSize = 0; 67157e2f323SJérôme Duval 67257e2f323SJérôme Duval fRecFile.Seek(sizeof(struct wave_struct), SEEK_SET); 67357e2f323SJérôme Duval 67457e2f323SJérôme Duval // Hook up input 67557e2f323SJérôme Duval err = MakeRecordConnection(fAudioInputNode); 67657e2f323SJérôme Duval if (err < B_OK) { 6773ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot connect to the selected sound input"), 6783ee96407SJérôme Duval err); 67957e2f323SJérôme Duval fRecEntry.Remove(); 68057e2f323SJérôme Duval fRecEntry.Unset(); 68157e2f323SJérôme Duval return; 68257e2f323SJérôme Duval } 68357e2f323SJérôme Duval 68457e2f323SJérôme Duval // And get it going... 68557e2f323SJérôme Duval bigtime_t then = fRecordNode->TimeSource()->Now() + 50000LL; 68657e2f323SJérôme Duval fRoster->StartNode(fRecordNode->Node(), then); 68757e2f323SJérôme Duval if (fAudioInputNode.kind & B_TIME_SOURCE) { 688ad0c65eaSJérôme Duval fRoster->StartNode(fAudioInputNode, 689ad0c65eaSJérôme Duval fRecordNode->TimeSource()->RealTimeFor(then, 0)); 69057e2f323SJérôme Duval } 69157e2f323SJérôme Duval else { 69257e2f323SJérôme Duval fRoster->StartNode(fAudioInputNode, then); 69357e2f323SJérôme Duval } 69457e2f323SJérôme Duval } 69557e2f323SJérôme Duval 6968e32a27eSPhilippe Saint-Pierre 69757e2f323SJérôme Duval void 69857e2f323SJérôme Duval RecorderWindow::Play(BMessage * message) 69957e2f323SJérôme Duval { 70057e2f323SJérôme Duval if (fPlayer) { 70157e2f323SJérôme Duval // User pressed Play button and playing 70257e2f323SJérôme Duval if (fPlayer->HasData()) 70357e2f323SJérôme Duval fPlayButton->SetPaused(); 70457e2f323SJérôme Duval else 70557e2f323SJérôme Duval fPlayButton->SetPlaying(); 70657e2f323SJérôme Duval fPlayer->SetHasData(!fPlayer->HasData()); 70757e2f323SJérôme Duval return; 70857e2f323SJérôme Duval } 70957e2f323SJérôme Duval 71057e2f323SJérôme Duval SetButtonState(btnPlaying); 71157e2f323SJérôme Duval fPlayButton->SetPlaying(); 71257e2f323SJérôme Duval 71357e2f323SJérôme Duval if (!fPlayTrack) { 7143ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot get the file to play"), B_ERROR); 71557e2f323SJérôme Duval return; 71657e2f323SJérôme Duval } 71757e2f323SJérôme Duval 718ad0c65eaSJérôme Duval fPlayLimit = MIN(fPlayFrames, (off_t)(fTrackSlider->RightTime() 719ad0c65eaSJérôme Duval * fPlayFormat.u.raw_audio.frame_rate / 1000000LL)); 72057e2f323SJérôme Duval fPlayTrack->SeekToTime(fTrackSlider->MainTime()); 72157e2f323SJérôme Duval fPlayFrame = fPlayTrack->CurrentFrame(); 72257e2f323SJérôme Duval 72357e2f323SJérôme Duval // Create our internal Node which plays sound, and register it. 7243ee96407SJérôme Duval fPlayer = new BSoundPlayer(fAudioMixerNode, &fPlayFormat.u.raw_audio, 7253ee96407SJérôme Duval "Sound Player"); 72657e2f323SJérôme Duval status_t err = fPlayer->InitCheck(); 72757e2f323SJérôme Duval if (err < B_OK) { 72857e2f323SJérôme Duval return; 72957e2f323SJérôme Duval } 73057e2f323SJérôme Duval 73157e2f323SJérôme Duval fVolumeSlider->SetSoundPlayer(fPlayer); 73257e2f323SJérôme Duval fPlayer->SetCallbacks(PlayFile, NotifyPlayFile, this); 73357e2f323SJérôme Duval 73457e2f323SJérôme Duval // And get it going... 73557e2f323SJérôme Duval fPlayer->Start(); 73657e2f323SJérôme Duval fPlayer->SetHasData(true); 73757e2f323SJérôme Duval } 73857e2f323SJérôme Duval 7398e32a27eSPhilippe Saint-Pierre 74057e2f323SJérôme Duval void 74157e2f323SJérôme Duval RecorderWindow::Stop(BMessage * message) 74257e2f323SJérôme Duval { 74357e2f323SJérôme Duval // User pressed Stop button. 74457e2f323SJérôme Duval // Stop recorder. 74557e2f323SJérôme Duval StopRecording(); 74657e2f323SJérôme Duval // Stop player. 74757e2f323SJérôme Duval StopPlaying(); 74857e2f323SJérôme Duval } 74957e2f323SJérôme Duval 7508e32a27eSPhilippe Saint-Pierre 75157e2f323SJérôme Duval void 75257e2f323SJérôme Duval RecorderWindow::Save(BMessage * message) 75357e2f323SJérôme Duval { 75457e2f323SJérôme Duval // User pressed Save button. 75557e2f323SJérôme Duval // Find the item to save. 75657e2f323SJérôme Duval int32 index = fSoundList->CurrentSelection(); 75757e2f323SJérôme Duval SoundListItem* pItem = dynamic_cast<SoundListItem*>(fSoundList->ItemAt(index)); 75857e2f323SJérôme Duval if ((! pItem) || (pItem->Entry().InitCheck() != B_OK)) { 75957e2f323SJérôme Duval return; 76057e2f323SJérôme Duval } 76157e2f323SJérôme Duval 76257e2f323SJérôme Duval // Update the save panel and show it. 76357e2f323SJérôme Duval char filename[B_FILE_NAME_LENGTH]; 76457e2f323SJérôme Duval pItem->Entry().GetName(filename); 76557e2f323SJérôme Duval BMessage saveMsg(B_SAVE_REQUESTED); 76657e2f323SJérôme Duval entry_ref ref; 76757e2f323SJérôme Duval pItem->Entry().GetRef(&ref); 76857e2f323SJérôme Duval 769a4797804SJérôme Duval if (saveMsg.AddPointer("sound list item", pItem) != B_OK) 770a4797804SJérôme Duval fprintf(stderr, "failed to add pItem\n"); 7715fa77532SJérôme Duval fSavePanel->SetSaveText(filename); 7725fa77532SJérôme Duval fSavePanel->SetMessage(&saveMsg); 7735fa77532SJérôme Duval fSavePanel->Show(); 77457e2f323SJérôme Duval } 77557e2f323SJérôme Duval 7768e32a27eSPhilippe Saint-Pierre 77757e2f323SJérôme Duval void 77857e2f323SJérôme Duval RecorderWindow::DoSave(BMessage * message) 77957e2f323SJérôme Duval { 78057e2f323SJérôme Duval // User picked a place to put the file. 78157e2f323SJérôme Duval // Find the location of the old (e.g. 78257e2f323SJérôme Duval // temporary file), and the name of the 78357e2f323SJérôme Duval // new file to save. 78457e2f323SJérôme Duval entry_ref old_ref, new_dir_ref; 78557e2f323SJérôme Duval const char* new_name; 78657e2f323SJérôme Duval SoundListItem* pItem; 78757e2f323SJérôme Duval 78857e2f323SJérôme Duval if ((message->FindPointer("sound list item", (void**) &pItem) == B_OK) 78957e2f323SJérôme Duval && (message->FindRef("directory", &new_dir_ref) == B_OK) 79057e2f323SJérôme Duval && (message->FindString("name", &new_name) == B_OK)) 79157e2f323SJérôme Duval { 79257e2f323SJérôme Duval BEntry& oldEntry = pItem->Entry(); 79357e2f323SJérôme Duval BFile oldFile(&oldEntry, B_READ_WRITE); 79457e2f323SJérôme Duval if (oldFile.InitCheck() != B_OK) 79557e2f323SJérôme Duval return; 79657e2f323SJérôme Duval 79757e2f323SJérôme Duval BDirectory newDir(&new_dir_ref); 79857e2f323SJérôme Duval if (newDir.InitCheck() != B_OK) 79957e2f323SJérôme Duval return; 80057e2f323SJérôme Duval 80157e2f323SJérôme Duval BFile newFile; 80257e2f323SJérôme Duval newDir.CreateFile(new_name, &newFile); 80357e2f323SJérôme Duval 80457e2f323SJérôme Duval if (newFile.InitCheck() != B_OK) 80557e2f323SJérôme Duval return; 80657e2f323SJérôme Duval 80757e2f323SJérôme Duval status_t err = CopyFile(newFile, oldFile); 80857e2f323SJérôme Duval 80957e2f323SJérôme Duval if (err == B_OK) { 81057e2f323SJérôme Duval // clean up the sound list and item 81157e2f323SJérôme Duval if (pItem->IsTemp()) 81257e2f323SJérôme Duval oldEntry.Remove(); // blows away temp file! 81357e2f323SJérôme Duval oldEntry.SetTo(&newDir, new_name); 81457e2f323SJérôme Duval pItem->SetTemp(false); // don't blow the new entry away when we exit! 81557e2f323SJérôme Duval fSoundList->Invalidate(); 81657e2f323SJérôme Duval } 81757e2f323SJérôme Duval } else { 81857e2f323SJérôme Duval WINDOW((stderr, "Couldn't save file.\n")); 81957e2f323SJérôme Duval } 82057e2f323SJérôme Duval } 82157e2f323SJérôme Duval 82257e2f323SJérôme Duval 82357e2f323SJérôme Duval void 82457e2f323SJérôme Duval RecorderWindow::Input(BMessage * message) 82557e2f323SJérôme Duval { 82657e2f323SJérôme Duval // User selected input from pop-up 82757e2f323SJérôme Duval const dormant_node_info * dni = 0; 82857e2f323SJérôme Duval ssize_t size = 0; 82957e2f323SJérôme Duval if (message->FindData("node", B_RAW_TYPE, (const void **)&dni, &size)) { 83057e2f323SJérôme Duval return; // bad input selection message 83157e2f323SJérôme Duval } 83257e2f323SJérôme Duval 83357e2f323SJérôme Duval media_node_id node_id; 83457e2f323SJérôme Duval status_t error = fRoster->GetInstancesFor(dni->addon, dni->flavor_id, &node_id); 83557e2f323SJérôme Duval if (error != B_OK) { 83657e2f323SJérôme Duval fRoster->InstantiateDormantNode(*dni, &fAudioInputNode); 83757e2f323SJérôme Duval } else { 83857e2f323SJérôme Duval fRoster->GetNodeFor(node_id, &fAudioInputNode); 83957e2f323SJérôme Duval } 84057e2f323SJérôme Duval } 84157e2f323SJérôme Duval 8428e32a27eSPhilippe Saint-Pierre 84357e2f323SJérôme Duval void 84457e2f323SJérôme Duval RecorderWindow::Selected(BMessage * message) 84557e2f323SJérôme Duval { 84657e2f323SJérôme Duval // User selected a sound in list view 847c6f8aa29SJérôme Duval int32 selIdx = fSoundList->CurrentSelection(); 848c6f8aa29SJérôme Duval SoundListItem* pItem = dynamic_cast<SoundListItem*>(fSoundList->ItemAt(selIdx)); 849c6f8aa29SJérôme Duval if (!pItem) 850c6f8aa29SJérôme Duval return; 851c6f8aa29SJérôme Duval status_t err = UpdatePlayFile(pItem, true); 852c6f8aa29SJérôme Duval if (err != B_OK) { 8533ee96407SJérôme Duval ErrorAlert(B_TRANSLATE("Cannot recognize this file as a media file"), 854ad0c65eaSJérôme Duval err == B_MEDIA_NO_HANDLER ? B_OK : err); 855c6f8aa29SJérôme Duval RemoveCurrentSoundItem(); 856c6f8aa29SJérôme Duval } 85757e2f323SJérôme Duval UpdateButtons(); 85857e2f323SJérôme Duval } 85957e2f323SJérôme Duval 8608e32a27eSPhilippe Saint-Pierre 86157e2f323SJérôme Duval status_t 86257e2f323SJérôme Duval RecorderWindow::MakeRecordConnection(const media_node & input) 86357e2f323SJérôme Duval { 86457e2f323SJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection()\n")); 86557e2f323SJérôme Duval 86657e2f323SJérôme Duval // Find an available output for the given input node. 86757e2f323SJérôme Duval int32 count = 0; 86857e2f323SJérôme Duval status_t err = fRoster->GetFreeOutputsFor(input, &fAudioOutput, 1, &count, B_MEDIA_RAW_AUDIO); 86957e2f323SJérôme Duval if (err < B_OK) { 870ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 871ad0c65eaSJérôme Duval " couldn't get free outputs from audio input node\n")); 87257e2f323SJérôme Duval return err; 87357e2f323SJérôme Duval } 87457e2f323SJérôme Duval if (count < 1) { 875ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 876ad0c65eaSJérôme Duval " no free outputs from audio input node\n")); 87757e2f323SJérôme Duval return B_BUSY; 87857e2f323SJérôme Duval } 87957e2f323SJérôme Duval 88057e2f323SJérôme Duval // Find an available input for our own Node. Note that we go through the 88157e2f323SJérôme Duval // MediaRoster; calling Media Kit methods directly on Nodes in our app is 88257e2f323SJérôme Duval // not OK (because synchronization happens in the service thread, not in 88357e2f323SJérôme Duval // the calling thread). 88457e2f323SJérôme Duval // TODO: explain this 88557e2f323SJérôme Duval err = fRoster->GetFreeInputsFor(fRecordNode->Node(), &fRecInput, 1, &count, B_MEDIA_RAW_AUDIO); 88657e2f323SJérôme Duval if (err < B_OK) { 887ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 888ad0c65eaSJérôme Duval " couldn't get free inputs for sound recorder\n")); 88957e2f323SJérôme Duval return err; 89057e2f323SJérôme Duval } 89157e2f323SJérôme Duval if (count < 1) { 892ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 893ad0c65eaSJérôme Duval " no free inputs for sound recorder\n")); 89457e2f323SJérôme Duval return B_BUSY; 89557e2f323SJérôme Duval } 89657e2f323SJérôme Duval 89757e2f323SJérôme Duval // Find out what the time source of the input is. 89857e2f323SJérôme Duval // For most nodes, we just use the preferred time source (the DAC) for synchronization. 89957e2f323SJérôme Duval // However, nodes that record from an input need to synchronize to the audio input node 90057e2f323SJérôme Duval // instead for best results. 90157e2f323SJérôme Duval // MakeTimeSourceFor gives us a "clone" of the time source node that we can manipulate 90257e2f323SJérôme Duval // to our heart's content. When we're done with it, though, we need to call Release() 90357e2f323SJérôme Duval // on the time source node, so that it keeps an accurate reference count and can delete 90457e2f323SJérôme Duval // itself when it's no longer needed. 90557e2f323SJérôme Duval // TODO: what about filters connected to audio input? 90657e2f323SJérôme Duval media_node use_time_source; 90757e2f323SJérôme Duval BTimeSource * tsobj = fRoster->MakeTimeSourceFor(input); 90857e2f323SJérôme Duval if (! tsobj) { 909ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 910ad0c65eaSJérôme Duval " couldn't clone time source from audio input node\n")); 91157e2f323SJérôme Duval return B_MEDIA_BAD_NODE; 91257e2f323SJérôme Duval } 91357e2f323SJérôme Duval 91457e2f323SJérôme Duval // Apply the time source in effect to our own Node. 91557e2f323SJérôme Duval err = fRoster->SetTimeSourceFor(fRecordNode->Node().node, tsobj->Node().node); 91657e2f323SJérôme Duval if (err < B_OK) { 917ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 918ad0c65eaSJérôme Duval " couldn't set the sound recorder's time source\n")); 91957e2f323SJérôme Duval tsobj->Release(); 92057e2f323SJérôme Duval return err; 92157e2f323SJérôme Duval } 92257e2f323SJérôme Duval 92357e2f323SJérôme Duval // Get a format, any format. 92488fef303SJérôme Duval fRecordFormat.u.raw_audio = fAudioOutput.format.u.raw_audio; 92588fef303SJérôme Duval fRecordFormat.type = B_MEDIA_RAW_AUDIO; 92657e2f323SJérôme Duval 92757e2f323SJérôme Duval // Tell the consumer where we want data to go. 92857e2f323SJérôme Duval err = fRecordNode->SetHooks(RecordFile, NotifyRecordFile, this); 92957e2f323SJérôme Duval if (err < B_OK) { 930ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 931ad0c65eaSJérôme Duval " couldn't set the sound recorder's hook functions\n")); 93257e2f323SJérôme Duval tsobj->Release(); 93357e2f323SJérôme Duval return err; 93457e2f323SJérôme Duval } 93557e2f323SJérôme Duval 9363ee96407SJérôme Duval // Using the same structs for input and output is OK in 9373ee96407SJérôme Duval // BMediaRoster::Connect(). 9383ee96407SJérôme Duval err = fRoster->Connect(fAudioOutput.source, fRecInput.destination, 9393ee96407SJérôme Duval &fRecordFormat, &fAudioOutput, &fRecInput); 94057e2f323SJérôme Duval if (err < B_OK) { 941ad0c65eaSJérôme Duval CONNECT((stderr, "RecorderWindow::MakeRecordConnection():" 942ad0c65eaSJérôme Duval " failed to connect sound recorder to audio input node.\n")); 94357e2f323SJérôme Duval tsobj->Release(); 94457e2f323SJérôme Duval fRecordNode->SetHooks(0, 0, 0); 94557e2f323SJérôme Duval return err; 94657e2f323SJérôme Duval } 94757e2f323SJérôme Duval 94857e2f323SJérôme Duval // Start the time source if it's not running. 94957e2f323SJérôme Duval if ((tsobj->Node() != input) && !tsobj->IsRunning()) { 95057e2f323SJérôme Duval fRoster->StartNode(tsobj->Node(), BTimeSource::RealTime()); 95157e2f323SJérôme Duval } 95257e2f323SJérôme Duval tsobj->Release(); // we're done with this time source instance! 95357e2f323SJérôme Duval return B_OK; 95457e2f323SJérôme Duval } 95557e2f323SJérôme Duval 95657e2f323SJérôme Duval 95757e2f323SJérôme Duval status_t 95857e2f323SJérôme Duval RecorderWindow::BreakRecordConnection() 95957e2f323SJérôme Duval { 96057e2f323SJérôme Duval status_t err; 96157e2f323SJérôme Duval 96257e2f323SJérôme Duval // If we are the last connection, the Node will stop automatically since it 96357e2f323SJérôme Duval // has nowhere to send data to. 96457e2f323SJérôme Duval err = fRoster->StopNode(fRecInput.node, 0); 965ad0c65eaSJérôme Duval err = fRoster->Disconnect(fAudioOutput.node.node, fAudioOutput.source, 966ad0c65eaSJérôme Duval fRecInput.node.node, fRecInput.destination); 96757e2f323SJérôme Duval fAudioOutput.source = media_source::null; 96857e2f323SJérôme Duval fRecInput.destination = media_destination::null; 96957e2f323SJérôme Duval return err; 97057e2f323SJérôme Duval } 97157e2f323SJérôme Duval 9728e32a27eSPhilippe Saint-Pierre 97357e2f323SJérôme Duval status_t 97457e2f323SJérôme Duval RecorderWindow::StopRecording() 97557e2f323SJérôme Duval { 97657e2f323SJérôme Duval if (!fRecording) 97757e2f323SJérôme Duval return B_OK; 97857e2f323SJérôme Duval fRecording = false; 97957e2f323SJérôme Duval BreakRecordConnection(); 98057e2f323SJérôme Duval fRecordNode->SetHooks(NULL,NULL,NULL); 98157e2f323SJérôme Duval if (fRecSize > 0) { 98257e2f323SJérôme Duval 98357e2f323SJérôme Duval wave_struct header; 98457e2f323SJérôme Duval header.riff.riff_id = FOURCC('R','I','F','F'); 985a4797804SJérôme Duval header.riff.len = fRecSize + sizeof(header) - 8; 98657e2f323SJérôme Duval header.riff.wave_id = FOURCC('W','A','V','E'); 98757e2f323SJérôme Duval header.format_chunk.fourcc = FOURCC('f','m','t',' '); 98857e2f323SJérôme Duval header.format_chunk.len = sizeof(header.format); 98957e2f323SJérôme Duval header.format.format_tag = 1; 99088fef303SJérôme Duval header.format.channels = fRecordFormat.u.raw_audio.channel_count; 99188fef303SJérôme Duval header.format.samples_per_sec = (uint32)fRecordFormat.u.raw_audio.frame_rate; 99288fef303SJérôme Duval header.format.avg_bytes_per_sec = (uint32)(fRecordFormat.u.raw_audio.frame_rate 99388fef303SJérôme Duval * fRecordFormat.u.raw_audio.channel_count 99488fef303SJérôme Duval * (fRecordFormat.u.raw_audio.format & 0xf)); 99588fef303SJérôme Duval header.format.bits_per_sample = (fRecordFormat.u.raw_audio.format & 0xf) * 8; 99688fef303SJérôme Duval header.format.block_align = (fRecordFormat.u.raw_audio.format & 0xf) 99788fef303SJérôme Duval * fRecordFormat.u.raw_audio.channel_count; 99857e2f323SJérôme Duval header.data_chunk.fourcc = FOURCC('d','a','t','a'); 99957e2f323SJérôme Duval header.data_chunk.len = fRecSize; 100057e2f323SJérôme Duval fRecFile.Seek(0, SEEK_SET); 100157e2f323SJérôme Duval fRecFile.Write(&header, sizeof(header)); 100257e2f323SJérôme Duval 10033ee96407SJérôme Duval fRecFile.SetSize(fRecSize + sizeof(header)); 10043ee96407SJérôme Duval // We reserve space; make sure we cut off any excess at the end. 100557e2f323SJérôme Duval AddSoundItem(fRecEntry, true); 100657e2f323SJérôme Duval } 100757e2f323SJérôme Duval else { 100857e2f323SJérôme Duval fRecEntry.Remove(); 100957e2f323SJérôme Duval } 101057e2f323SJérôme Duval // We're done for this time. 101157e2f323SJérôme Duval fRecEntry.Unset(); 101257e2f323SJérôme Duval // Close the file. 101357e2f323SJérôme Duval fRecFile.Unset(); 101457e2f323SJérôme Duval // No more recording going on. 101557e2f323SJérôme Duval fRecSize = 0; 101657e2f323SJérôme Duval SetButtonState(btnPaused); 101757e2f323SJérôme Duval fRecordButton->SetStopped(); 101857e2f323SJérôme Duval 101957e2f323SJérôme Duval return B_OK; 102057e2f323SJérôme Duval } 102157e2f323SJérôme Duval 102257e2f323SJérôme Duval 102357e2f323SJérôme Duval status_t 102457e2f323SJérôme Duval RecorderWindow::StopPlaying() 102557e2f323SJérôme Duval { 102657e2f323SJérôme Duval if (fPlayer) { 102757e2f323SJérôme Duval fPlayer->Stop(); 102857e2f323SJérôme Duval fPlayer->SetCallbacks(0, 0, 0); 102957e2f323SJérôme Duval fVolumeSlider->SetSoundPlayer(NULL); 103057e2f323SJérôme Duval delete fPlayer; 103157e2f323SJérôme Duval fPlayer = NULL; 103257e2f323SJérôme Duval } 103357e2f323SJérôme Duval SetButtonState(btnPaused); 103457e2f323SJérôme Duval fPlayButton->SetStopped(); 103557e2f323SJérôme Duval fTrackSlider->ResetMainTime(); 103657e2f323SJérôme Duval fScopeView->SetMainTime(*fTrackSlider->MainTime()); 103757e2f323SJérôme Duval return B_OK; 103857e2f323SJérôme Duval } 103957e2f323SJérôme Duval 104057e2f323SJérôme Duval 104157e2f323SJérôme Duval void 104257e2f323SJérôme Duval RecorderWindow::SetButtonState(BtnState state) 104357e2f323SJérôme Duval { 104457e2f323SJérôme Duval fButtonState = state; 104557e2f323SJérôme Duval UpdateButtons(); 104657e2f323SJérôme Duval } 104757e2f323SJérôme Duval 104857e2f323SJérôme Duval 104957e2f323SJérôme Duval void 105057e2f323SJérôme Duval RecorderWindow::UpdateButtons() 105157e2f323SJérôme Duval { 105257e2f323SJérôme Duval bool hasSelection = (fSoundList->CurrentSelection() >= 0); 105357e2f323SJérôme Duval fRecordButton->SetEnabled(fButtonState != btnPlaying); 105457e2f323SJérôme Duval fPlayButton->SetEnabled((fButtonState != btnRecording) && hasSelection); 105557e2f323SJérôme Duval fRewindButton->SetEnabled((fButtonState != btnRecording) && hasSelection); 105657e2f323SJérôme Duval fForwardButton->SetEnabled((fButtonState != btnRecording) && hasSelection); 105757e2f323SJérôme Duval fStopButton->SetEnabled(fButtonState != btnPaused); 105857e2f323SJérôme Duval fSaveButton->SetEnabled(hasSelection && (fButtonState != btnRecording)); 105957e2f323SJérôme Duval fInputField->SetEnabled(fButtonState != btnRecording); 106057e2f323SJérôme Duval } 106157e2f323SJérôme Duval 10625fa77532SJérôme Duval #ifndef __HAIKU__ 10633ee96407SJérôme Duval extern "C" status_t DecodedFormat__11BMediaTrackP12media_format( 10643ee96407SJérôme Duval BMediaTrack *self, media_format *inout_format); 10655fa77532SJérôme Duval #endif 106657e2f323SJérôme Duval 10678e32a27eSPhilippe Saint-Pierre 1068c6f8aa29SJérôme Duval status_t 1069c6f8aa29SJérôme Duval RecorderWindow::UpdatePlayFile(SoundListItem* item, bool updateDisplay) 107057e2f323SJérôme Duval { 1071019ed09bSJérôme Duval fScopeView->CancelRendering(); 1072019ed09bSJérôme Duval StopPlaying(); 1073019ed09bSJérôme Duval StopRecording(); 1074019ed09bSJérôme Duval 1075dbbc34b6SJérôme Duval if (fPlayTrack && fPlayFile) { 10767942339dSJérôme Duval fPlayFile->ReleaseTrack(fPlayTrack); 10777942339dSJérôme Duval fPlayTrack = NULL; 1078dbbc34b6SJérôme Duval } 1079dbbc34b6SJérôme Duval if (fPlayFile) { 1080dbbc34b6SJérôme Duval delete fPlayFile; 10817942339dSJérôme Duval fPlayFile = NULL; 1082dbbc34b6SJérôme Duval } 10837942339dSJérôme Duval 108457e2f323SJérôme Duval status_t err; 1085c6f8aa29SJérôme Duval BEntry& entry = item->Entry(); 108657e2f323SJérôme Duval entry_ref ref; 108757e2f323SJérôme Duval entry.GetRef(&ref); 108857e2f323SJérôme Duval fPlayFile = new BMediaFile(&ref); //, B_MEDIA_FILE_UNBUFFERED); 108957e2f323SJérôme Duval if ((err = fPlayFile->InitCheck()) < B_OK) { 109057e2f323SJérôme Duval delete fPlayFile; 1091dbbc34b6SJérôme Duval fPlayFile = NULL; 1092c6f8aa29SJérôme Duval return err; 109357e2f323SJérôme Duval } 109457e2f323SJérôme Duval 109557e2f323SJérôme Duval for (int ix=0; ix < fPlayFile->CountTracks(); ix++) { 109657e2f323SJérôme Duval BMediaTrack * track = fPlayFile->TrackAt(ix); 109757e2f323SJérôme Duval fPlayFormat.type = B_MEDIA_RAW_AUDIO; 10985fa77532SJérôme Duval #ifdef __HAIKU__ 10995fa77532SJérôme Duval if ((track->DecodedFormat(&fPlayFormat) == B_OK) 11005fa77532SJérôme Duval #else 11015fa77532SJérôme Duval if ((DecodedFormat__11BMediaTrackP12media_format(track, &fPlayFormat) == B_OK) 11025fa77532SJérôme Duval #endif 11035fa77532SJérôme Duval && (fPlayFormat.type == B_MEDIA_RAW_AUDIO)) { 110457e2f323SJérôme Duval fPlayTrack = track; 110557e2f323SJérôme Duval break; 110657e2f323SJérôme Duval } 110757e2f323SJérôme Duval if (track) 110857e2f323SJérôme Duval fPlayFile->ReleaseTrack(track); 110957e2f323SJérôme Duval } 111057e2f323SJérôme Duval 111157e2f323SJérôme Duval if (!fPlayTrack) { 111257e2f323SJérôme Duval delete fPlayFile; 1113019ed09bSJérôme Duval fPlayFile = NULL; 1114c6f8aa29SJérôme Duval return B_STREAM_NOT_FOUND; 111557e2f323SJérôme Duval } 111657e2f323SJérôme Duval 1117c6f8aa29SJérôme Duval if (!updateDisplay) 1118c6f8aa29SJérôme Duval return B_OK; 1119c6f8aa29SJérôme Duval 11203ee96407SJérôme Duval BString filename = B_TRANSLATE("File name: "); 112157e2f323SJérôme Duval filename << ref.name; 112257e2f323SJérôme Duval fFilename->SetText(filename.String()); 112357e2f323SJérôme Duval 11243ee96407SJérôme Duval BString format = B_TRANSLATE("Format: "); 112557e2f323SJérôme Duval media_file_format file_format; 112657e2f323SJérôme Duval if (fPlayFile->GetFileFormatInfo(&file_format) == B_OK) 112757e2f323SJérôme Duval format << file_format.short_name; 11283ee96407SJérôme Duval BString compression = B_TRANSLATE("Compression: "); 112957e2f323SJérôme Duval media_codec_info codec_info; 113057e2f323SJérôme Duval if (fPlayTrack->GetCodecInfo(&codec_info) == B_OK) { 113157e2f323SJérôme Duval if (strcmp(codec_info.short_name, "raw")==0) 11323ee96407SJérôme Duval compression << B_TRANSLATE("None"); 113357e2f323SJérôme Duval else 113457e2f323SJérôme Duval compression << codec_info.short_name; 113557e2f323SJérôme Duval } 11363ee96407SJérôme Duval BString channels = B_TRANSLATE("Channels: "); 113757e2f323SJérôme Duval channels << fPlayFormat.u.raw_audio.channel_count; 11383ee96407SJérôme Duval BString samplesize = B_TRANSLATE("Sample size: "); 11393ee96407SJérôme Duval samplesize << 8 * (fPlayFormat.u.raw_audio.format & 0xf) 11403ee96407SJérôme Duval << B_TRANSLATE(" bits"); 11413ee96407SJérôme Duval BString samplerate = B_TRANSLATE("Sample rate: "); 114257e2f323SJérôme Duval samplerate << (int)fPlayFormat.u.raw_audio.frame_rate; 11433ee96407SJérôme Duval BString durationString = B_TRANSLATE("Duration: "); 114457e2f323SJérôme Duval bigtime_t duration = fPlayTrack->Duration(); 11453ee96407SJérôme Duval durationString << (float)(duration / 1000000.0) << B_TRANSLATE(" seconds"); 114657e2f323SJérôme Duval 114757e2f323SJérôme Duval fFormat->SetText(format.String()); 114857e2f323SJérôme Duval fCompression->SetText(compression.String()); 114957e2f323SJérôme Duval fChannels->SetText(channels.String()); 115057e2f323SJérôme Duval fSampleSize->SetText(samplesize.String()); 115157e2f323SJérôme Duval fSampleRate->SetText(samplerate.String()); 115257e2f323SJérôme Duval fDuration->SetText(durationString.String()); 115357e2f323SJérôme Duval 115457e2f323SJérôme Duval fTrackSlider->SetTotalTime(duration, true); 1155019ed09bSJérôme Duval fScopeView->SetTotalTime(duration, true); 115657e2f323SJérôme Duval fScopeView->RenderTrack(fPlayTrack, fPlayFormat); 11577942339dSJérôme Duval 11587942339dSJérôme Duval fPlayFrames = fPlayTrack->CountFrames(); 1159c6f8aa29SJérôme Duval return B_OK; 116057e2f323SJérôme Duval } 116157e2f323SJérôme Duval 116257e2f323SJérôme Duval 116357e2f323SJérôme Duval void 116457e2f323SJérôme Duval RecorderWindow::ErrorAlert(const char * action, status_t err) 116557e2f323SJérôme Duval { 116657e2f323SJérôme Duval char msg[300]; 1167c6f8aa29SJérôme Duval if (err != B_OK) 11683ee96407SJérôme Duval sprintf(msg, "%s: %s. [%lx]", action, strerror(err), (int32) err); 1169c6f8aa29SJérôme Duval else 11703ee96407SJérôme Duval sprintf(msg, "%s.", action); 11713ee96407SJérôme Duval (new BAlert("", msg, B_TRANSLATE("Stop")))->Go(); 117257e2f323SJérôme Duval } 117357e2f323SJérôme Duval 117457e2f323SJérôme Duval 117557e2f323SJérôme Duval status_t 117657e2f323SJérôme Duval RecorderWindow::NewTempName(char * name) 117757e2f323SJérôme Duval { 117857e2f323SJérôme Duval int init_count = fTempCount; 117957e2f323SJérôme Duval again: 118057e2f323SJérôme Duval if (fTempCount-init_count > 25) { 118157e2f323SJérôme Duval return B_ERROR; 118257e2f323SJérôme Duval } 118357e2f323SJérôme Duval else { 118457e2f323SJérôme Duval fTempCount++; 118557e2f323SJérôme Duval if (fTempCount==0) 118657e2f323SJérôme Duval sprintf(name, "Audio Clip"); 118757e2f323SJérôme Duval else 118857e2f323SJérôme Duval sprintf(name, "Audio Clip %d", fTempCount); 118957e2f323SJérôme Duval BPath path; 119057e2f323SJérôme Duval status_t err; 119157e2f323SJérôme Duval BEntry tempEnt; 119257e2f323SJérôme Duval if ((err = fTempDir.GetEntry(&tempEnt)) < B_OK) { 119357e2f323SJérôme Duval return err; 119457e2f323SJérôme Duval } 119557e2f323SJérôme Duval if ((err = tempEnt.GetPath(&path)) < B_OK) { 119657e2f323SJérôme Duval return err; 119757e2f323SJérôme Duval } 119857e2f323SJérôme Duval path.Append(name); 119957e2f323SJérôme Duval int fd; 120057e2f323SJérôme Duval // Use O_EXCL so we know we created the file (sync with other instances) 120157e2f323SJérôme Duval if ((fd = open(path.Path(), O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) { 120257e2f323SJérôme Duval goto again; 120357e2f323SJérôme Duval } 120457e2f323SJérôme Duval close(fd); 120557e2f323SJérôme Duval } 120657e2f323SJérôme Duval return B_OK; 120757e2f323SJérôme Duval } 120857e2f323SJérôme Duval 120957e2f323SJérôme Duval 121057e2f323SJérôme Duval void 121157e2f323SJérôme Duval RecorderWindow::AddSoundItem(const BEntry& entry, bool temp) 121257e2f323SJérôme Duval { 121357e2f323SJérôme Duval // Create list item to display. 121457e2f323SJérôme Duval SoundListItem * listItem = new SoundListItem(entry, temp); 121557e2f323SJérôme Duval fSoundList->AddItem(listItem); 121657e2f323SJérôme Duval fSoundList->Invalidate(); 121757e2f323SJérôme Duval fSoundList->Select(fSoundList->IndexOf(listItem)); 121857e2f323SJérôme Duval } 121957e2f323SJérôme Duval 1220019ed09bSJérôme Duval 1221019ed09bSJérôme Duval void 1222019ed09bSJérôme Duval RecorderWindow::RemoveCurrentSoundItem() { 1223c6f8aa29SJérôme Duval int32 index = fSoundList->CurrentSelection(); 1224c6f8aa29SJérôme Duval BListItem *item = fSoundList->RemoveItem(index); 1225019ed09bSJérôme Duval delete item; 1226c6f8aa29SJérôme Duval if (index >= fSoundList->CountItems()) 1227c6f8aa29SJérôme Duval index = fSoundList->CountItems() - 1; 1228c6f8aa29SJérôme Duval fSoundList->Select(index); 1229019ed09bSJérôme Duval } 1230019ed09bSJérôme Duval 1231019ed09bSJérôme Duval 123257e2f323SJérôme Duval void 1233ef367a8aSStephan Aßmus RecorderWindow::RecordFile(void* cookie, bigtime_t timestamp, 1234ef367a8aSStephan Aßmus void* data, size_t size, const media_raw_audio_format &format) 123557e2f323SJérôme Duval { 123657e2f323SJérôme Duval // Callback called from the SoundConsumer when receiving buffers. 123757e2f323SJérôme Duval RecorderWindow * window = (RecorderWindow *)cookie; 123857e2f323SJérôme Duval 123957e2f323SJérôme Duval if (window->fRecording) { 124057e2f323SJérôme Duval // Write the data to file (we don't buffer or guard file access 124157e2f323SJérôme Duval // or anything) 124257e2f323SJérôme Duval window->fRecFile.WriteAt(window->fRecSize, data, size); 1243ad0c65eaSJérôme Duval window->fVUView->ComputeLevels(data, size, format.format); 124457e2f323SJérôme Duval window->fRecSize += size; 124557e2f323SJérôme Duval } 124657e2f323SJérôme Duval } 124757e2f323SJérôme Duval 124857e2f323SJérôme Duval 124957e2f323SJérôme Duval void 125057e2f323SJérôme Duval RecorderWindow::NotifyRecordFile(void * cookie, int32 code, ...) 125157e2f323SJérôme Duval { 125257e2f323SJérôme Duval if ((code == B_WILL_STOP) || (code == B_NODE_DIES)) { 125357e2f323SJérôme Duval RecorderWindow * window = (RecorderWindow *)cookie; 125457e2f323SJérôme Duval // Tell the window we've stopped, if it doesn't 125557e2f323SJérôme Duval // already know. 125657e2f323SJérôme Duval window->PostMessage(STOP_RECORDING); 125757e2f323SJérôme Duval } 125857e2f323SJérôme Duval } 125957e2f323SJérôme Duval 126057e2f323SJérôme Duval 126157e2f323SJérôme Duval void 1262ad0c65eaSJérôme Duval RecorderWindow::PlayFile(void * cookie, void * data, size_t size, 1263ad0c65eaSJérôme Duval const media_raw_audio_format & format) 126457e2f323SJérôme Duval { 126557e2f323SJérôme Duval // Callback called from the SoundProducer when producing buffers. 126657e2f323SJérôme Duval RecorderWindow * window = (RecorderWindow *)cookie; 126757e2f323SJérôme Duval int32 frame_size = (window->fPlayFormat.u.raw_audio.format & 0xf) * 126857e2f323SJérôme Duval window->fPlayFormat.u.raw_audio.channel_count; 126957e2f323SJérôme Duval 1270948356deSJérôme Duval if ((window->fPlayFrame < window->fPlayLimit) || window->fLooping) { 1271948356deSJérôme Duval if (window->fPlayFrame >= window->fPlayLimit) { 1272948356deSJérôme Duval bigtime_t left = window->fTrackSlider->LeftTime(); 1273948356deSJérôme Duval window->fPlayTrack->SeekToTime(&left); 1274948356deSJérôme Duval window->fPlayFrame = window->fPlayTrack->CurrentFrame(); 1275948356deSJérôme Duval } 127657e2f323SJérôme Duval int64 frames = 0; 127757e2f323SJérôme Duval window->fPlayTrack->ReadFrames(data, &frames); 1278ad0c65eaSJérôme Duval window->fVUView->ComputeLevels(data, size / frame_size, format.format); 127957e2f323SJérôme Duval window->fPlayFrame += size/frame_size; 128057e2f323SJérôme Duval window->PostMessage(UPDATE_TRACKSLIDER); 128157e2f323SJérôme Duval } else { 128257e2f323SJérôme Duval // we're done! 128357e2f323SJérôme Duval window->PostMessage(STOP_PLAYING); 128457e2f323SJérôme Duval } 128557e2f323SJérôme Duval } 128657e2f323SJérôme Duval 12878e32a27eSPhilippe Saint-Pierre 128857e2f323SJérôme Duval void 1289ad0c65eaSJérôme Duval RecorderWindow::NotifyPlayFile(void * cookie, 1290ad0c65eaSJérôme Duval BSoundPlayer::sound_player_notification code, ...) 129157e2f323SJérôme Duval { 129257e2f323SJérôme Duval if ((code == BSoundPlayer::B_STOPPED) || (code == BSoundPlayer::B_SOUND_DONE)) { 129357e2f323SJérôme Duval RecorderWindow * window = (RecorderWindow *)cookie; 129457e2f323SJérôme Duval // tell the window we've stopped, if it doesn't 129557e2f323SJérôme Duval // already know. 129657e2f323SJérôme Duval window->PostMessage(STOP_PLAYING); 129757e2f323SJérôme Duval } 129857e2f323SJérôme Duval } 129957e2f323SJérôme Duval 130057e2f323SJérôme Duval 130157e2f323SJérôme Duval void 130257e2f323SJérôme Duval RecorderWindow::RefsReceived(BMessage *msg) 130357e2f323SJérôme Duval { 130457e2f323SJérôme Duval entry_ref ref; 130557e2f323SJérôme Duval int32 i = 0; 1306c6f8aa29SJérôme Duval int32 countGood = 0; 1307c6f8aa29SJérôme Duval int32 countBad = 0; 130857e2f323SJérôme Duval 130957e2f323SJérôme Duval while (msg->FindRef("refs", i++, &ref) == B_OK) { 131057e2f323SJérôme Duval 131157e2f323SJérôme Duval BEntry entry(&ref, true); 131257e2f323SJérôme Duval BPath path(&entry); 131357e2f323SJérôme Duval BNode node(&entry); 131457e2f323SJérôme Duval 131557e2f323SJérôme Duval if (node.IsFile()) { 1316c6f8aa29SJérôme Duval SoundListItem * listItem = new SoundListItem(entry, false); 1317c6f8aa29SJérôme Duval if (UpdatePlayFile(listItem) == B_OK) { 1318c6f8aa29SJérôme Duval fSoundList->AddItem(listItem); 1319c6f8aa29SJérôme Duval countGood++; 1320c6f8aa29SJérôme Duval continue; 1321c6f8aa29SJérôme Duval } 1322c6f8aa29SJérôme Duval delete listItem; 132357e2f323SJérôme Duval } else if(node.IsDirectory()) { 132457e2f323SJérôme Duval 132557e2f323SJérôme Duval } 1326c6f8aa29SJérôme Duval countBad++; 1327c6f8aa29SJérôme Duval } 1328c6f8aa29SJérôme Duval 1329c6f8aa29SJérôme Duval if (countBad > 0 && countGood == 0) 13303ee96407SJérôme Duval (new BAlert(B_TRANSLATE("Nothing to play"), B_TRANSLATE("None of the " 13313ee96407SJérôme Duval "files appear to be audio files"), B_TRANSLATE("OK"), NULL, NULL, 13323ee96407SJérôme Duval B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go(); 1333c6f8aa29SJérôme Duval else if (countGood > 0) { 1334c6f8aa29SJérôme Duval if (countBad > 0) 13353ee96407SJérôme Duval (new BAlert(B_TRANSLATE("Invalid audio files"), B_TRANSLATE("Some " 13363ee96407SJérôme Duval "of the files don't appear to be audio files"), 13373ee96407SJérôme Duval B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, 13383ee96407SJérôme Duval B_WARNING_ALERT))->Go(); 1339c6f8aa29SJérôme Duval fSoundList->Select(fSoundList->CountItems() - 1); 134057e2f323SJérôme Duval } 134157e2f323SJérôme Duval } 1342019ed09bSJérôme Duval 1343019ed09bSJérôme Duval 1344019ed09bSJérôme Duval void 1345019ed09bSJérôme Duval RecorderWindow::CopyTarget(BMessage *msg) 1346019ed09bSJérôme Duval { 1347019ed09bSJérôme Duval const char *type = NULL; 1348019ed09bSJérôme Duval if (msg->FindString("be:types", &type) == B_OK) { 1349019ed09bSJérôme Duval if (!strcasecmp(type, B_FILE_MIME_TYPE)) { 1350019ed09bSJérôme Duval const char *name; 1351019ed09bSJérôme Duval entry_ref dir; 1352019ed09bSJérôme Duval if (msg->FindString("be:filetypes") == B_OK 1353019ed09bSJérôme Duval && msg->FindString("name", &name) == B_OK 1354019ed09bSJérôme Duval && msg->FindRef("directory", &dir) == B_OK) { 1355019ed09bSJérôme Duval BDirectory directory(&dir); 1356019ed09bSJérôme Duval BFile file(&directory, name, O_RDWR | O_TRUNC); 1357019ed09bSJérôme Duval 1358019ed09bSJérôme Duval // seek time 1359019ed09bSJérôme Duval bigtime_t start = fTrackSlider->LeftTime(); 1360019ed09bSJérôme Duval 1361019ed09bSJérôme Duval // write data 13623ee96407SJérôme Duval bigtime_t diffTime = fTrackSlider->RightTime() 13633ee96407SJérôme Duval - fTrackSlider->LeftTime(); 1364ad0c65eaSJérôme Duval int64 framesToWrite = (int64) (diffTime 1365ad0c65eaSJérôme Duval * fPlayFormat.u.raw_audio.frame_rate / 1000000LL); 1366019ed09bSJérôme Duval int32 frameSize = (fPlayFormat.u.raw_audio.format & 0xf) 1367019ed09bSJérôme Duval * fPlayFormat.u.raw_audio.channel_count; 1368019ed09bSJérôme Duval 1369019ed09bSJérôme Duval wave_struct header; 1370019ed09bSJérôme Duval header.riff.riff_id = FOURCC('R','I','F','F'); 13713ee96407SJérôme Duval header.riff.len 13723ee96407SJérôme Duval = (frameSize * framesToWrite) + sizeof(header) - 8; 1373019ed09bSJérôme Duval header.riff.wave_id = FOURCC('W','A','V','E'); 1374019ed09bSJérôme Duval header.format_chunk.fourcc = FOURCC('f','m','t',' '); 1375019ed09bSJérôme Duval header.format_chunk.len = sizeof(header.format); 1376019ed09bSJérôme Duval header.format.format_tag = 1; 1377019ed09bSJérôme Duval header.format.channels = fPlayFormat.u.raw_audio.channel_count; 13783ee96407SJérôme Duval header.format.samples_per_sec 13793ee96407SJérôme Duval = (uint32)fPlayFormat.u.raw_audio.frame_rate; 13803ee96407SJérôme Duval header.format.avg_bytes_per_sec 13813ee96407SJérôme Duval = (uint32)(fPlayFormat.u.raw_audio.frame_rate 1382019ed09bSJérôme Duval * fPlayFormat.u.raw_audio.channel_count 1383019ed09bSJérôme Duval * (fPlayFormat.u.raw_audio.format & 0xf)); 13843ee96407SJérôme Duval header.format.bits_per_sample 13853ee96407SJérôme Duval = (fPlayFormat.u.raw_audio.format & 0xf) * 8; 1386019ed09bSJérôme Duval header.format.block_align = frameSize; 1387019ed09bSJérôme Duval header.data_chunk.fourcc = FOURCC('d','a','t','a'); 1388019ed09bSJérôme Duval header.data_chunk.len = frameSize * framesToWrite; 1389019ed09bSJérôme Duval file.Seek(0, SEEK_SET); 1390019ed09bSJérôme Duval file.Write(&header, sizeof(header)); 1391019ed09bSJérôme Duval 1392019ed09bSJérôme Duval char *data = (char *)malloc(fPlayFormat.u.raw_audio.buffer_size); 1393019ed09bSJérôme Duval 1394c6f8aa29SJérôme Duval fPlayTrack->SeekToTime(&start); 1395c6f8aa29SJérôme Duval fPlayFrame = fPlayTrack->CurrentFrame(); 1396019ed09bSJérôme Duval while (framesToWrite > 0) { 1397019ed09bSJérôme Duval int64 frames = 0; 1398019ed09bSJérôme Duval status_t err = fPlayTrack->ReadFrames(data, &frames); 1399c6f8aa29SJérôme Duval if (frames <= 0 || err != B_OK) { 1400c6f8aa29SJérôme Duval if (err != B_OK) 1401c6f8aa29SJérôme Duval fprintf(stderr, "CopyTarget: ReadFrames failed\n"); 1402019ed09bSJérôme Duval break; 1403c6f8aa29SJérôme Duval } 1404019ed09bSJérôme Duval file.Write(data, frames * frameSize); 1405019ed09bSJérôme Duval framesToWrite -= frames; 1406019ed09bSJérôme Duval } 1407019ed09bSJérôme Duval 1408019ed09bSJérôme Duval file.Sync(); 1409019ed09bSJérôme Duval free(data); 1410019ed09bSJérôme Duval BNodeInfo nodeInfo(&file); 1411019ed09bSJérôme Duval // set type 1412019ed09bSJérôme Duval } 1413019ed09bSJérôme Duval } else { 1414019ed09bSJérôme Duval 1415019ed09bSJérôme Duval } 1416019ed09bSJérôme Duval } 1417019ed09bSJérôme Duval } 1418