From 019ed09bee683a10c8d902d2c01e29f584950359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Sun, 9 Nov 2008 14:37:18 +0000 Subject: [PATCH] * cancel rendering if selection changed * update track slider borders if selection changed * added drag'n drop from ScopeView git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28574 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/soundrecorder/RecorderWindow.cpp | 92 +++++++++++++++++++++-- src/apps/soundrecorder/RecorderWindow.h | 2 + src/apps/soundrecorder/ScopeView.cpp | 41 +++++++++- src/apps/soundrecorder/ScopeView.h | 4 +- src/apps/soundrecorder/TrackSlider.cpp | 2 + 5 files changed, 131 insertions(+), 10 deletions(-) diff --git a/src/apps/soundrecorder/RecorderWindow.cpp b/src/apps/soundrecorder/RecorderWindow.cpp index 1ff29105b6..e64d6172cc 100644 --- a/src/apps/soundrecorder/RecorderWindow.cpp +++ b/src/apps/soundrecorder/RecorderWindow.cpp @@ -39,6 +39,7 @@ #include #include +#include #include "RecorderWindow.h" #include "SoundConsumer.h" @@ -589,6 +590,9 @@ RecorderWindow::MessageReceived(BMessage * message) RefsReceived(message); break; } + case B_COPY_TARGET: + CopyTarget(message); + break; default: BWindow::MessageReceived(message); break; @@ -681,7 +685,7 @@ RecorderWindow::Play(BMessage * message) return; } - fPlayLimit = MIN(fPlayFrames, (off_t)(fTrackSlider->RightTime()*fPlayFormat.u.raw_audio.frame_rate/1000000LL)); + fPlayLimit = MIN(fPlayFrames, (off_t)(fTrackSlider->RightTime() * fPlayFormat.u.raw_audio.frame_rate / 1000000LL)); fPlayTrack->SeekToTime(fTrackSlider->MainTime()); fPlayFrame = fPlayTrack->CurrentFrame(); @@ -1002,6 +1006,10 @@ extern "C" status_t DecodedFormat__11BMediaTrackP12media_format(BMediaTrack *sel void RecorderWindow::UpdatePlayFile() { + fScopeView->CancelRendering(); + StopPlaying(); + StopRecording(); + // Get selection. int32 selIdx = fSoundList->CurrentSelection(); SoundListItem* pItem = dynamic_cast(fSoundList->ItemAt(selIdx)); @@ -1027,6 +1035,7 @@ RecorderWindow::UpdatePlayFile() ErrorAlert("get the file to play", err); delete fPlayFile; fPlayFile = NULL; + RemoveCurrentSoundItem(); return; } @@ -1049,6 +1058,8 @@ RecorderWindow::UpdatePlayFile() if (!fPlayTrack) { ErrorAlert("get the file to play", err); delete fPlayFile; + fPlayFile = NULL; + RemoveCurrentSoundItem(); return; } @@ -1086,10 +1097,7 @@ RecorderWindow::UpdatePlayFile() fDuration->SetText(durationString.String()); fTrackSlider->SetTotalTime(duration, true); - fScopeView->SetMainTime(fTrackSlider->LeftTime()); - fScopeView->SetTotalTime(duration); - fScopeView->SetRightTime(fTrackSlider->RightTime()); - fScopeView->SetLeftTime(fTrackSlider->LeftTime()); + fScopeView->SetTotalTime(duration, true); fScopeView->RenderTrack(fPlayTrack, fPlayFormat); fPlayFrames = fPlayTrack->CountFrames(); @@ -1150,6 +1158,14 @@ RecorderWindow::AddSoundItem(const BEntry& entry, bool temp) fSoundList->Select(fSoundList->IndexOf(listItem)); } + +void +RecorderWindow::RemoveCurrentSoundItem() { + BListItem *item = fSoundList->RemoveItem(fSoundList->CurrentSelection()); + delete item; +} + + void RecorderWindow::RecordFile(void * cookie, bigtime_t timestamp, void * data, size_t size, const media_raw_audio_format & format) @@ -1236,3 +1252,69 @@ RecorderWindow::RefsReceived(BMessage *msg) } } } + + +void +RecorderWindow::CopyTarget(BMessage *msg) +{ + const char *type = NULL; + if (msg->FindString("be:types", &type) == B_OK) { + if (!strcasecmp(type, B_FILE_MIME_TYPE)) { + const char *name; + entry_ref dir; + if (msg->FindString("be:filetypes") == B_OK + && msg->FindString("name", &name) == B_OK + && msg->FindRef("directory", &dir) == B_OK) { + BDirectory directory(&dir); + BFile file(&directory, name, O_RDWR | O_TRUNC); + + // seek time + bigtime_t start = fTrackSlider->LeftTime(); + fPlayTrack->SeekToTime(&start); + + // write data + bigtime_t diffTime = fTrackSlider->RightTime() - fTrackSlider->LeftTime(); + int64 framesToWrite = (int64) (diffTime * fPlayFormat.u.raw_audio.frame_rate / 1000000LL); + int32 frameSize = (fPlayFormat.u.raw_audio.format & 0xf) + * fPlayFormat.u.raw_audio.channel_count; + + wave_struct header; + header.riff.riff_id = FOURCC('R','I','F','F'); + header.riff.len = (frameSize * framesToWrite) + 36; + header.riff.wave_id = FOURCC('W','A','V','E'); + header.format_chunk.fourcc = FOURCC('f','m','t',' '); + header.format_chunk.len = sizeof(header.format); + header.format.format_tag = 1; + header.format.channels = fPlayFormat.u.raw_audio.channel_count; + header.format.samples_per_sec = (uint32)fPlayFormat.u.raw_audio.frame_rate; + header.format.avg_bytes_per_sec = (uint32)(fPlayFormat.u.raw_audio.frame_rate + * fPlayFormat.u.raw_audio.channel_count + * (fPlayFormat.u.raw_audio.format & 0xf)); + header.format.bits_per_sample = (fPlayFormat.u.raw_audio.format & 0xf) * 8; + header.format.block_align = frameSize; + header.data_chunk.fourcc = FOURCC('d','a','t','a'); + header.data_chunk.len = frameSize * framesToWrite; + file.Seek(0, SEEK_SET); + file.Write(&header, sizeof(header)); + + char *data = (char *)malloc(fPlayFormat.u.raw_audio.buffer_size); + + while (framesToWrite > 0) { + int64 frames = 0; + status_t err = fPlayTrack->ReadFrames(data, &frames); + if (frames <= 0 || err != B_OK) + break; + file.Write(data, frames * frameSize); + framesToWrite -= frames; + } + + file.Sync(); + free(data); + BNodeInfo nodeInfo(&file); + // set type + } + } else { + + } + } +} diff --git a/src/apps/soundrecorder/RecorderWindow.h b/src/apps/soundrecorder/RecorderWindow.h index 9e16d7c70f..d7655f6c00 100644 --- a/src/apps/soundrecorder/RecorderWindow.h +++ b/src/apps/soundrecorder/RecorderWindow.h @@ -67,6 +67,7 @@ public: }; void AddSoundItem(const BEntry& entry, bool temp = false); + void RemoveCurrentSoundItem(); private: BMediaRoster * fRoster; @@ -164,6 +165,7 @@ static void PlayFile(void * cookie, void * data, size_t size, const media_raw_au static void NotifyPlayFile(void * cookie, BSoundPlayer::sound_player_notification code, ...); void RefsReceived(BMessage *msg); + void CopyTarget(BMessage *msg); }; #endif /* RECORDERWINDOW_H */ diff --git a/src/apps/soundrecorder/ScopeView.cpp b/src/apps/soundrecorder/ScopeView.cpp index 34d3907476..555a949f22 100644 --- a/src/apps/soundrecorder/ScopeView.cpp +++ b/src/apps/soundrecorder/ScopeView.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include "DrawingTidbits.h" @@ -132,7 +133,7 @@ ScopeView::RenderLoop() int32 previewIndex = 0; - while (fMediaTrack->ReadFrames(samples, &frames) == B_OK) { + while (fIsRendering && fMediaTrack->ReadFrames(samples, &frames) == B_OK) { //TRACE("reading block\n"); framesIndex = 0; @@ -153,8 +154,6 @@ ScopeView::RenderLoop() sum = 0; } } - - } TRACE("finished computing, rendering\n"); @@ -187,9 +186,14 @@ ScopeView::SetMainTime(bigtime_t timestamp) void -ScopeView::SetTotalTime(bigtime_t timestamp) +ScopeView::SetTotalTime(bigtime_t timestamp, bool reset) { fTotalTime = timestamp; + if (reset) { + fMainTime = 0; + fLeftTime = 0; + fRightTime = fTotalTime; + } Invalidate(); TRACE("invalidate done\n"); } @@ -222,6 +226,13 @@ ScopeView::RenderTrack(BMediaTrack *track, media_format format) } +void +ScopeView::CancelRendering() +{ + fIsRendering = false; +} + + void ScopeView::FrameResized(float width, float height) { @@ -232,6 +243,28 @@ ScopeView::FrameResized(float width, float height) } +void +ScopeView::MouseDown(BPoint position) +{ + if (!fMediaTrack) + return; + + uint32 buttons; + BPoint point; + GetMouse(&point, &buttons); + + if (buttons & B_PRIMARY_MOUSE_BUTTON) { + // fill the drag message + BMessage drag(B_SIMPLE_DATA); + drag.AddInt32("be:actions", B_COPY_TARGET); + drag.AddString("be:clip_name", "Audio Clip"); + drag.AddString("be:types", B_FILE_MIME_TYPE); + + DragMessage(&drag, Bounds()); + } +} + + void ScopeView::InitBitmap() { diff --git a/src/apps/soundrecorder/ScopeView.h b/src/apps/soundrecorder/ScopeView.h index 29e39d487c..4554d8f811 100644 --- a/src/apps/soundrecorder/ScopeView.h +++ b/src/apps/soundrecorder/ScopeView.h @@ -24,9 +24,11 @@ public: void SetMainTime(bigtime_t timestamp); void SetLeftTime(bigtime_t timestamp); void SetRightTime(bigtime_t timestamp); - void SetTotalTime(bigtime_t timestamp); + void SetTotalTime(bigtime_t timestamp, bool reset); void RenderTrack(BMediaTrack *track, media_format format); + void CancelRendering(); virtual void FrameResized(float width, float height); + virtual void MouseDown(BPoint position); private: void Run(); void Quit(); diff --git a/src/apps/soundrecorder/TrackSlider.cpp b/src/apps/soundrecorder/TrackSlider.cpp index af89dbedb2..b4befb0d00 100644 --- a/src/apps/soundrecorder/TrackSlider.cpp +++ b/src/apps/soundrecorder/TrackSlider.cpp @@ -364,6 +364,7 @@ TrackSlider::SetMainTime(bigtime_t timestamp, bool reset) fLeftTime = 0; fBitmapView->fLeftX = 14 + (fBitmapView->fRight - 15) * ((double)fLeftTime / fTotalTime); fBitmapView->fRightX = 15 + (fBitmapView->fRight - 16) * ((double)fRightTime / fTotalTime); + RenderBitmap(); } Invalidate(); } @@ -380,6 +381,7 @@ TrackSlider::SetTotalTime(bigtime_t timestamp, bool reset) fBitmapView->fPositionX = 15 + (fBitmapView->fRight - 14) * ((double)fMainTime / fTotalTime); fBitmapView->fLeftX = 14 + (fBitmapView->fRight - 15) * ((double)fLeftTime / fTotalTime); fBitmapView->fRightX = 15 + (fBitmapView->fRight - 16) * ((double)fRightTime / fTotalTime); + RenderBitmap(); Invalidate(); }