Пример #1
0
int LoadProject(const char *fn, ProjectStateContext *ctx) // return 0=ok,
{
  // no need to check extension here, it's done for us
  FILE *fp=fopen(fn,"rt");
  if (!fp) return -1; // error!
  
  ctx->AddLine("<REAPER_PROJECT 0.1");
  // set any project settings here (this example doesnt need to)


  // add a track
  ctx->AddLine("<TRACK");
    ctx->AddLine("NAME \"m3u playlist\"");


  double lastfadeouttime=0.0;
  double curpos=0.0;
  // add an item per playlist item
  for (;;)
  {
    char buf[1024];
    buf[0]=0;
    fgets(buf,sizeof(buf),fp);
    if (!buf[0]) break;

    if (buf[strlen(buf)-1]=='\n') buf[strlen(buf)-1]=0;

    if (!buf[0]) continue;

    if (buf[0]!='#' && !strstr(buf,"://"))
    {
      double thisl=30.0;
      char ofn[2048];
      resolve_fn(buf,ofn,sizeof(ofn));
      PCM_source *src=PCM_Source_CreateFromFile(ofn);
      if (src) 
      {
        thisl=src->GetLength();
        delete src;
      }
      else
      {
        // todo: use extended info from the m3u
      }
      char *fnptr=ofn;
      // todo: extended m3u info
      while (*fnptr) fnptr++;
      while (fnptr>=ofn && *fnptr !='\\' && *fnptr != '/') fnptr--;
      fnptr++;

      double thisfadeouttime=3.0; // default to 3s xfades
      if (thisfadeouttime>thisl)
      {
        thisfadeouttime=0.01;
        if (lastfadeouttime+thisfadeouttime>thisl)
          lastfadeouttime=thisl-thisfadeouttime;
      }

      ctx->AddLine("<ITEM");
        ctx->AddLine("POSITION %.8f",curpos);
        ctx->AddLine("LENGTH %.8f",thisl);
        ctx->AddLine("FADEIN 1 0.01 %f",lastfadeouttime);
        ctx->AddLine("FADEOUT 1 0.01 %f",thisfadeouttime);
        ctx->AddLine("LOOP 0"); // turn off looping
        ctx->AddLine("NAME \"%s\"",fnptr); 
        ctx->AddLine("SRCFN \"%s\"",ofn);
      ctx->AddLine(">");
      
      lastfadeouttime=thisfadeouttime;
      curpos+=thisl-thisfadeouttime;
    }
  }
  ctx->AddLine(">"); // <TRACK
  
  ctx->AddLine(">"); // <REAPER_PROJECT

  fclose(fp);
  return 0;
}
Пример #2
0
// 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
}