void generate_items_sequence(std::shared_ptr<breakpoint_envelope> env, const char* fn) { SetCursorContext(1, NULL); Main_OnCommand(40182, 0); // select all items Main_OnCommand(40006, 0); // remove selected items MediaTrack* dest_track = GetTrack(nullptr, 0); double timepos = 0.0; double seqlen = 30.0; int sanity = 0; while (timepos < seqlen) { create_item_result r = create_item_with_take_and_source(dest_track, fn); SetMediaItemPosition(r.item, timepos, false); double srclen = GetMediaSourceLength(r.src, nullptr); double normtime = 1.0 / seqlen * timepos; double directionfactor = skip_near_zero(-1.0 + 2.0*env->interpolate(normtime), 0.02); if (directionfactor < 0.0) { //readbg() << "item " << sanity << " should be reversed\n"; SetMediaItemSelected(r.item, true); Main_OnCommand(41051, 0); // toggle reverse SetMediaItemSelected(r.item, false); } double absfactor = fabs(directionfactor); double itemlen = bound_value(0.02, srclen*absfactor, srclen); SetMediaItemLength(r.item, itemlen, false); SetMediaItemTakeInfo_Value(r.take, "D_PLAYRATE", 1.0 / absfactor); timepos += srclen*absfactor; ++sanity; if (sanity > 1000) { readbg() << "too many items created!\n"; break; } } UpdateArrange(); }
void ImportJson(COMMAND_T* pCommand) { char* selectedFile = BrowseForFiles("Select Auphonic JSON file", 0, 0, false, "Auphonic JSON (*.json)\0*.json\0All Files (*.*)\0*.*\0"); if((selectedFile != 0) && (strlen(selectedFile) > 0)) { std::string rawData = GetFileContents(std::string(selectedFile)); std::string errorMessage; json11::Json cookedData = json11::Json::parse(rawData, errorMessage); if(errorMessage.empty() == true) { std::map<std::string, std::set<ActivityItem>> trackActivities; const json11::Json statistics = cookedData["statistics"]; for(const json11::Json& track : statistics["tracks"].array_items()) { const std::string trackId = track["identifier"].string_value(); if(trackId.empty() == false) { std::set<ActivityItem> activities; for(const json11::Json& activity : track["activity"].array_items()) { const double begin = activity.array_items()[0].number_value(); const double end = activity.array_items()[1].number_value(); activities.insert(ActivityItem(begin, end)); } if(activities.empty() == false) { trackActivities.insert(std::map<std::string, std::set<ActivityItem>>::value_type(trackId, activities)); } else { ErrorMessage("No activities for track"); } } else { ErrorMessage("Found invalid track identifier"); } } std::map<std::string, MediaTrack*> reaperTracks; for(int i = 0; i < GetNumTracks(); ++i) { MediaTrack* reaperTrack = GetTrack(0, i); AssertTrue(reaperTrack != 0); std::string trackName = MediaTrack_GetStringProperty(reaperTrack, "P_NAME"); reaperTracks[trackName] = reaperTrack; } for(const std::map<std::string, std::set<ActivityItem>>::value_type& trackActivity : trackActivities) { const std::string& trackId = trackActivity.first; std::map<std::string, MediaTrack*>::iterator trackIterator = reaperTracks.find(trackId); if(trackIterator != reaperTracks.end()) { MediaTrack* reaperTrack = trackIterator->second; if(reaperTrack != 0) { MediaItem* reaperItem = AddMediaItemToTrack(reaperTrack); if(reaperItem != 0) { const std::set<ActivityItem>& activities = trackActivity.second; for(const ActivityItem& activity : activities) { SetMediaItemPosition(reaperItem, activity.Begin(), true); SetMediaItemLength(reaperItem, activity.Duration(), true); MediaItem_SetStringProperty(reaperItem, "P_NOTES", trackId); // int color = MediaTrack_GetIntProperty(reaperTrack, "I_CUSTOMCOLOR"); int color = MediaTrack_GetProperty<int>(reaperTrack, "I_CUSTOMCOLOR"); char buffer[4096] = {0}; sprintf(buffer, "%s (%.2fs)", trackId.c_str(), activity.Duration()); const char* activityLabel = AllocCopyString(buffer); AddProjectMarker2(0, true, activity.Begin(), activity.End(), activityLabel, 0, color); } } else { ErrorMessage("Failed to add activity"); } } else { ErrorMessage("Failed to find track"); } } } } else { ErrorMessage(errorMessage.c_str()); } } }
// Import local files from m3u/pls playlists onto a new track void PlaylistImport(COMMAND_T* ct) { char cPath[256]; vector<SPlaylistEntry> filelist; GetProjectPath(cPath, 256); string listpath = BrowseForFiles(__LOCALIZE("Import playlist","sws_mbox"), cPath, NULL, false, "Playlist files (*.m3u,*.pls)\0*.m3u;*.pls\0All Files (*.*)\0*.*\0"); string ext = ParseFileExtension(listpath); // Decide what kind of playlist we have if(ext == "m3u") { ParseM3U(listpath, filelist); } else if(ext == "pls") { ParsePLS(listpath, filelist); } if(filelist.empty()) { ShowMessageBox(__LOCALIZE("Failed to import playlist. No files found.","sws_mbox"), __LOCALIZE("Import playlist","sws_mbox"), 0); return; } // Validate files vector<string> badfiles; for(int i = 0, c = (int)filelist.size(); i < c; i++) { SPlaylistEntry e = filelist[i]; if(!file_exists(e.path.c_str())) { badfiles.push_back(e.path); } } // If files can't be found, ask user what to do. bool includeMissing = false; if(!badfiles.empty()) { stringstream ss; ss << __LOCALIZE("Cannot find some files. Create items anyway?\n","sws_mbox"); unsigned int limit = min((int)badfiles.size(), 9); // avoid enormous messagebox for(unsigned int i = 0; i < limit; i++) { ss << "\n " << badfiles[i]; } if(badfiles.size() > limit) { ss << "\n +" << badfiles.size() - limit << __LOCALIZE(" more files","sws_mbox"); } switch(ShowMessageBox(ss.str().c_str(), __LOCALIZE("Import playlist","sws_mbox"), 3)) { case 6 : // Yes includeMissing = true; break; case 7 : // No break; default : return; } } Undo_BeginBlock2(NULL); // Add new track int idx = GetNumTracks(); int panMode = 5; // Stereo pan double panLaw = 1.0; // 0dB InsertTrackAtIndex(idx, false); MediaTrack *pTrack = GetTrack(NULL, idx); GetSetMediaTrackInfo(pTrack, "P_NAME", (void*) listpath.c_str()); GetSetMediaTrackInfo(pTrack, "I_PANMODE", (void*) &panMode); GetSetMediaTrackInfo(pTrack, "D_PANLAW", (void*) &panLaw); SetOnlyTrackSelected(pTrack); // Add new items to track double pos = 0.0; for(int i = 0, c = (int)filelist.size(); i < c; i++) { SPlaylistEntry e = filelist[i]; //TODO: Would be better if missing files were offline rather than just empty items. PCM_source *pSrc = PCM_Source_CreateFromFile(e.path.c_str()); if(pSrc || includeMissing) { MediaItem *pItem = AddMediaItemToTrack(pTrack); if(pItem) { MediaItem_Take *pTake = AddTakeToMediaItem(pItem); if(pTake) { GetSetMediaItemTakeInfo(pTake, "P_SOURCE", pSrc); GetSetMediaItemTakeInfo(pTake, "P_NAME", (void*) e.title.c_str()); SetMediaItemPosition(pItem, pos, false); SetMediaItemLength(pItem, e.length, false); pos += e.length; } } } } Undo_EndBlock2(NULL, SWS_CMD_SHORTNAME(ct), UNDO_STATE_ITEMS|UNDO_STATE_TRACKCFG); TrackList_AdjustWindows(false); UpdateTimeline(); Main_OnCommand(40047, 0); // Build missing peaks }