summaryrefslogtreecommitdiff
path: root/src/CueSheets/Banshee.CueSheets/CueSheet/CueSheet.cs
blob: 0d44e15d20c7d83591222890d602a0a1463011ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//
// CueSheet.cs
//
// Authors:
//   Hans Oesterholt <hans@oesterholt.net>
//
// Copyright (C) 2013 Hans Oesterholt
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Text.RegularExpressions;
using Banshee.CueSheets;
using Banshee.Collection;
using Banshee.Base;
using Banshee.Playlists.Formats;
using System.Collections.Generic;
using Hyena;

namespace Banshee.CueSheets
{
	public class CueSheet : MemoryTrackListModel
	{
		private string 				_image_file_name;
		private string				_img_full_path;
		private string				_music_file_name;
		private string 				_title;
		private string 				_performer;
		private CueSheetEntry [] 	_tracks;
		private string				_cuefile;
		private string				_directory;
		private string				_basedir;
		
		private string 				_year;
		private string				_composer;
		private string				_subtitle;
		private string 				_cddbId;

		public string id() {
			return "title="+_title+";performer="+_performer+
				   ";year="+_year+";composer="+_composer;
		}
		
		public string genre() {
			int n=_basedir.Length;
			string d=_directory.Substring (n);
			string r=Tools.firstpart(d);
			return r;
		}
		
		private void append(CueSheetEntry e) {
			if (_tracks==null) { 
				_tracks=new CueSheetEntry[1];
				_tracks[0]=e;
			} else {
				CueSheetEntry [] es=new CueSheetEntry[_tracks.Length+1];
				int i=0;
				for(i=0;i<_tracks.Length;i++) {
					es[i]=_tracks[i];
				}
				es[i]=e;
				_tracks=es;
			}
			this.Add (e);
		}
		
		public string imageFileName() {
			return _image_file_name;
		}
		
		public string imageFullFileName() {
			return _img_full_path;
		}
		
		public string cueFile() {
			return _cuefile;
		}
		
		public string musicFileName() {
			return _music_file_name;
		}
		
		public string title() {
			return _title;
		}
		
		public string performer() {
			return _performer;
		}
		
		public string composer() {
			return _composer;
		}
		
		public string subtitle() {
			return _subtitle;
		}
		
		public string year() {
			return _year;
		}
		
		public string cddbId() {
			return _cddbId;
		}
		
		public CueSheetEntry entry(int i) {
			return _tracks[i];
		}
		
		public int nEntries() {
			if (_tracks==null) { return 0; }
			else {
				return _tracks.Length;
			}
		}
		
		public int searchIndex(double offset) {
			int k,N;
			for(k=0,N=nEntries ();k<N && offset>_tracks[k].offset ();k++);
			return k-1;
		}
		
		public void  resetArt() {
			if (_img_full_path!=null && _img_full_path!="") {
				if (File.Exists (_img_full_path)) {
					string aaid=CoverArtSpec.CreateArtistAlbumId (_performer,_title);
					string path=CoverArtSpec.GetPathForNewFile(aaid,_img_full_path);
					if (File.Exists (path)) { File.Delete (path); }
					File.Copy (_img_full_path,path);
					int i,N;
					for(i=0,N=nEntries ();i<N;i++) {
						entry (i).setArtWorkId(aaid);
					}
				}
			}
		}
		
		public string getArtId() {
			string aaid=CoverArtSpec.CreateArtistAlbumId (_performer,_title);
			if (!CoverArtSpec.CoverExists (aaid)) {
				if (File.Exists (_img_full_path)) {
					string path=CoverArtSpec.GetPathForNewFile (aaid,_img_full_path);
					if (!File.Exists (path)) {
						File.Copy (_img_full_path,path);
					}
				}
			} else {
				if (File.Exists (_img_full_path)) {
					string path=CoverArtSpec.GetPathForNewFile (aaid,_img_full_path);
					if (File.Exists (path)) { File.Delete (path); }
					if (!File.Exists (path)) {
						File.Copy (_img_full_path,path);
					}
				}
			}
			return aaid;
		}
		
		public bool eq(string s,string begin) {
			if (begin.Length>s.Length) { return false; }
			else {
				if (s.Substring(0,begin.Length).ToLower ()==begin.ToLower ()) {
					return true;
				} else {
					return false;
				}
			}
		}
		
		public override string ToString() {
			return "Performer: "+this.performer ()+", Title: "+this.title ()+"\ncuefile: "+this.cueFile()+"\nwave: "+this.musicFileName();
		}
		
		public void SetPerformer(string p) {
			_performer=p;
		}
		
		public void SetTitle(string t) {
			_title=t;
		}
		
		public void SetComposer(string c) {
			_composer=c;
		}
		
		public void SetSubtitle(string s) {
			_subtitle=s;
		}
		
		public void SetYear(string y) {
			_year=y;
		}
		
		
		public void SetImagePath(string path) {
			Hyena.Log.Information ("SetImagePath: "+path);
			if (path!=null && path!="") {
				if (File.Exists (path)) {
					string fn=Tools.basename(path); 
					string fnp=Tools.makefile(_directory,fn);
					if (!File.Exists (fnp)) {
						File.Copy (path,fnp);
					}
					_image_file_name=fn;
					_img_full_path=fnp;
				}
			}
		}
		
		public void ClearTracks() {
			base.Clear ();
			_tracks=null;
		}
		
		public CueSheetEntry AddTrack(string e_title,string e_perf,double e_offset) {
			int nr=0;
			if (_tracks!=null) {
				nr=_tracks.Length;
			}
			string aaid=getArtId ();
			CueSheetEntry e=new CueSheetEntry(_music_file_name,aaid,nr,-1,e_title,e_perf,_title,e_offset);
			append (e);
			int i,N;
			for(i=0,N=nEntries ();i<N;i++) {
				_tracks[i].setNrOfTracks(N);
				if (i<N-1) {
					_tracks[i].setLength (_tracks[i+1].offset ()-_tracks[i].offset ());
				} else {
					_tracks[i].setLength (-1.0);
				}
			}
			return e;
		}
		
		private string indent="";
		
		private void wrtl(System.IO.StreamWriter wrt,string key,string val,bool rem=false) {
			string r="";
			if (rem) { r="REM "; }
			wrt.WriteLine (indent+r+key.ToUpper ()+" \""+val+"\"");
		}
		
		private void wrtl_file(System.IO.StreamWriter wrt,string file) {
			wrt.WriteLine (indent+"FILE \""+file+"\" WAVE");
		}
		
		private void wrtl_track(System.IO.StreamWriter wrt,int index) {
			wrt.WriteLine (indent+"TRACK "+String.Format ("{0:00}",index)+" AUDIO");
		}
		
		private void wrtl_index(System.IO.StreamWriter wrt,int inr,double offset) {
			int t=(int) (offset*100.0);
			int m=t/(100*60);
			int s=(t/100)%60;
			int hs=t%100; 
			wrt.WriteLine (indent+"INDEX "+String.Format ("{0:00}",inr)+" "+
			               				   String.Format("{0:00}",m)+":"+
			               				   String.Format ("{0:00}",s)+":"+
				                   		   String.Format ("{0:00}",hs)
				                   );		
		}
		
		public void Save() {
			if (!File.Exists (_cuefile+".bck")) {
				File.Copy (_cuefile,_cuefile+".bck");
			}
			System.IO.StreamWriter wrt=new System.IO.StreamWriter(_cuefile);
			resetArt ();
			indent="";
			wrtl (wrt,"creator","Banshee CueSheets Extension",true);
			wrtl (wrt,"creator-version",CS_Info.Version (),true);
			wrtl (wrt,"banshee-aaid",getArtId (),true);
			wrtl (wrt,"image",_image_file_name,true);
			wrtl (wrt,"composer",_composer,true);
			wrtl (wrt,"subtitle",_subtitle,true);
			wrtl (wrt,"year",_year,true);
			wrtl (wrt,"cddbid",_cddbId,true);
			wrtl (wrt,"performer",_performer);
			wrtl (wrt,"title",_title);
			string mfn=Tools.basename(_music_file_name);
			wrtl_file (wrt,mfn);
			
			int i,N;
			for(i=0,N=nEntries ();i<N;i++) {
				CueSheetEntry e=_tracks[i];
				writeEntry(wrt,e,i);
			}
			
			wrt.Close ();
		}
		
		void writeEntry(StreamWriter wrt,CueSheetEntry e,int i) {
			indent="  ";
			wrtl_track(wrt,i+1);
			indent="    ";
			wrtl (wrt,"title",e.title ());
			wrtl (wrt,"performer",e.performer ());
			wrtl (wrt,"piece",e.getPiece (),true);
			wrtl (wrt,"composer",e.getComposer (),true);
			wrtl_index(wrt,1,e.offset ());
		}
		
		static private Regex unq1=new Regex("^[\"]");
		static private Regex unq2=new Regex("[\"]$");
	
		private string unquote(string s) {
			return unq1.Replace (unq2.Replace (s,""),"");
		}
		
		public void iLoad() {
			string filename=_cuefile;
			using (System.IO.StreamReader sr = System.IO.File.OpenText(filename)) {
				iLoad (sr);
			}
		}
		
		public void iLoad(StreamReader sr) {
		
			_composer="";
			_year="";
			_subtitle="";
			_cddbId="";
			
			Boolean _in_tracks=false;
			string e_perf="";
			string e_title="";
			double e_offset=-1.0;
			string e_piece="";
			string e_composer="";
			string aaid="";
			int nr=0;
			
			//string filename=_cuefile;
			string directory=_directory;
			
            string line = "";
        	while ((line = sr.ReadLine()) != null) {
				line=line.Trim ();
				if (line!="") {
					//Console.WriteLine ("it="+_in_tracks+", "+line);
					if (!_in_tracks) {
						if (eq(line,"performer")) { 
							_performer=unquote(line.Substring (9).Trim ());
						} else if (eq(line,"title")) {
							_title=unquote(line.Substring (5).Trim ());
						} else if (eq(line,"file")) { 
							_music_file_name=line.Substring (4).Trim ();
							Match m=Regex.Match (_music_file_name,"([\"][^\"]+[\"])");
							_music_file_name=m.ToString ();
							_music_file_name=unquote(_music_file_name).Trim ();
							_music_file_name=Tools.makefile(directory,_music_file_name);
						} else if (line.Substring(0,5).ToLower ()=="track") {
							_in_tracks=true;
						} else if (eq(line,"rem")) {
							//Hyena.Log.Information (line);
							line=line.Substring (3).Trim ();
							if (eq(line,"image")) { 
								_image_file_name=line.Substring (5).Trim ();
								_image_file_name=unquote(_image_file_name).Trim ();
								_img_full_path=Tools.makefile(directory,_image_file_name);
							} else if (eq (line,"composer")) {
								_composer=unquote(line.Substring (8).Trim ());
							} else if (eq (line,"subtitle")) {
								_subtitle=unquote(line.Substring (8).Trim ());
							} else if (eq (line,"year")) {
								_year=unquote(line.Substring (4).Trim ());
							} else if (eq (line,"cddbid")) {
								_cddbId=unquote(line.Substring (6).Trim ());
							}
						}
					} 
					
					
					if (_in_tracks) {
						if (aaid=="") { aaid=getArtId (); }

						//Console.WriteLine ("line="+line);
						if (eq(line,"track")) { 
							if (e_offset>=0) {
								nr+=1;
								CueSheetEntry e=new CueSheetEntry(_music_file_name,aaid,nr,-1,e_title,e_perf,_title,e_offset);
								e.setComposer (e_composer);
								e.setPiece (e_piece);
								append (e);
								if (nr>1) {
									CueSheetEntry ePrev;
									ePrev=this.entry (nr-2);
									ePrev.setLength(e.offset ()-ePrev.offset());
								}
							}
							e_perf=_performer;
							e_title="";
							e_composer=_composer;
							e_offset=-1.0;
						} else if (eq(line,"title")) {
							e_title=unquote(line.Substring (5).Trim ());
						} else if (eq(line,"performer")) { 
							e_perf=unquote(line.Substring (9).Trim ());
						} else if (eq(line,"rem")) {
							line=line.Substring (3).Trim ();
							if (eq (line,"composer")) {
								e_composer=unquote(line.Substring (8).Trim ());
							} else if (eq(line,"piece")) {
								e_piece=unquote(line.Substring (5).Trim ());
							} 
						} else if (eq(line,"index")) { 
							string s=line.Substring (5).Trim ();
							s=Regex.Replace (s,"^\\s*[0-9]+\\s*","");
							string []parts=Regex.Split(s,"[:]");
							int min=Convert.ToInt32(parts[0]);
							int secs=Convert.ToInt32(parts[1]);
							int hsecs=Convert.ToInt32(parts[2]);
							e_offset=min*60+secs+(hsecs/100.0);
						}
					}
				}
	        }
			//Console.WriteLine ("Last entry adding");
			if (e_offset>=0) {
				nr+=1;
				CueSheetEntry e=new CueSheetEntry(_music_file_name,aaid,nr,-1,e_title,e_perf,_title,e_offset);
				e.setComposer (e_composer);
				e.setPiece (e_piece);
				append (e);
				if (nr>1) {
					CueSheetEntry ePrev;
					ePrev=this.entry (nr-2);
					ePrev.setLength(e.offset ()-ePrev.offset());
				}
			}
			//Console.WriteLine ("Last entry added");
			
			{
				int i;
				for(i=0;i<nEntries();i++) {
					entry (i).setNrOfTracks(nr);
				}
				//Console.WriteLine ("Ready");
			}
		}		
		
		public void load(CueSheet s) {
			load (s._cuefile,s._directory,s._basedir);
		}
		
		public void load(string filename,string directory,string basedir) {
			Clear ();
			_cuefile=filename;
			_basedir=basedir;
			_directory=directory;
			try {
				iLoad();
			} catch(System.Exception e) {
				Console.WriteLine ("CueSheet: Cannot load "+filename);
				Console.WriteLine (e.ToString ());
			}
		}
		
		public override void Clear() {
			base.Clear ();
			_cuefile="";
			_image_file_name="";
			_img_full_path="";
			_music_file_name="";
			_title="";
			_performer="";
			_tracks=null;
			_basedir="";
			_directory="";
		}
		
		public CueSheet() {
			Clear ();
		}
		
		public CueSheet(string filename) {
			Clear ();
			load (filename,"","");
		}
		
		public CueSheet (string filename,string directory,string basedir) {
			Clear ();
			load (filename,directory,basedir);
		}
	}
}