Author: jwilcox
Date: 2005-06-18 16:31:58 -0400 (Sat, 18 Jun 2005)
New Revision: 46191
Added:
trunk/ipod-sharp/src/Equalizer.cs
Modified:
trunk/ipod-sharp/ChangeLog
trunk/ipod-sharp/src/Device.cs
trunk/ipod-sharp/src/Makefile.am
trunk/ipod-sharp/src/SongDatabase.cs
trunk/ipod-sharp/tests/IPodTests.cs
trunk/ipod-sharp/tests/ipod-test-db-v12-a.tar.gz
Log:
2005-06-18 James Willcox <[EMAIL PROTECTED]>
* src/Equalizer.cs: added
* src/Device.cs: add equalizer bits
* src/SongDatabase.cs: clean up some
* tests/IPodTests.cs: add equalizer tests
* tests/tests/ipod-test-db-v12-a.tar.gz: add a EQ file, since it
was missing one
Modified: trunk/ipod-sharp/ChangeLog
===================================================================
--- trunk/ipod-sharp/ChangeLog 2005-06-18 20:24:19 UTC (rev 46190)
+++ trunk/ipod-sharp/ChangeLog 2005-06-18 20:31:58 UTC (rev 46191)
@@ -1,3 +1,11 @@
+2005-06-18 James Willcox <[EMAIL PROTECTED]>
+
+ * src/Equalizer.cs: added
+ * src/Device.cs: add equalizer bits
+ * src/SongDatabase.cs: clean up some
+ * tests/IPodTests.cs: add equalizer tests
+ * tests/tests/ipod-test-db-v12-a.tar.gz: add a EQ file, since it was
missing one
+
2005-06-17 James Willcox <[EMAIL PROTECTED]>
* src/Device.cs: update bindings to handle the recently added
Modified: trunk/ipod-sharp/src/Device.cs
===================================================================
--- trunk/ipod-sharp/src/Device.cs 2005-06-18 20:24:19 UTC (rev 46190)
+++ trunk/ipod-sharp/src/Device.cs 2005-06-18 20:31:58 UTC (rev 46191)
@@ -1,5 +1,6 @@
using System;
+using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
@@ -34,6 +35,13 @@
[DllImport ("ipoddevice")]
private static extern IntPtr ipod_device_get_type ();
+ private ArrayList equalizers = new ArrayList ();
+ private EqualizerContainerRecord eqsrec;
+
+ private string EqDbPath {
+ get { return this.MountPoint +
"/iPod_Control/iTunes/iTunesEQPresets"; }
+ }
+
public DeviceModel Model {
get {
uint rawtype = (uint) GetProperty ("device-model").Val;
@@ -101,6 +109,14 @@
}
}
+ public Equalizer[] Equalizers {
+ get {
+ lock (equalizers) {
+ return (Equalizer[]) equalizers.ToArray (typeof
(Equalizer));
+ }
+ }
+ }
+
public SongDatabase SongDatabase {
get {
if (!IsIPod) {
@@ -118,11 +134,25 @@
}
protected Device (IntPtr ptr) : base (ptr) {
+ if (Raw == IntPtr.Zero) {
+ throw new DeviceException (this, "Failed to create device");
+ }
+
+ LoadEqualizers ();
}
public Device (string mountOrDevice) : this (ipod_device_new
(mountOrDevice)) {
- if (Raw == IntPtr.Zero) {
- throw new DeviceException (this, "Failed to create device");
+ }
+
+ private void LoadEqualizers () {
+ using (BinaryReader reader = new BinaryReader (File.Open
(EqDbPath, FileMode.Open))) {
+ eqsrec = new EqualizerContainerRecord ();
+ eqsrec.Read (reader);
+
+ foreach (EqualizerRecord eqrec in eqsrec.EqualizerRecords) {
+ Equalizer eq = new Equalizer (eqrec);
+ equalizers.Add (eq);
+ }
}
}
@@ -149,17 +179,53 @@
public void Save () {
IntPtr error = IntPtr.Zero;
-
+
if (!ipod_device_save (Raw, out error)) {
- GLib.GException exc = new GLib.GException (error);
- throw new DeviceException (this, exc.Message, exc);
+ if (!error.Equals (IntPtr.Zero)) {
+ GLib.GException exc = new GLib.GException (error);
+ throw new DeviceException (this, exc.Message, exc);
+ } else {
+ throw new DeviceException (this, "Failed to save device");
+ }
}
+
+ try {
+ // Back up the eq db
+ File.Copy (EqDbPath, EqDbPath + ".bak", true);
+
+ // Save the eq db
+ using (BinaryWriter writer = new BinaryWriter (new FileStream
(EqDbPath, FileMode.Create))) {
+ eqsrec.Save (writer);
+ }
+ } catch (Exception e) {
+ // restore the backup
+ File.Copy (EqDbPath + ".bak", EqDbPath, true);
+ }
}
public void Debug () {
ipod_device_debug (Raw);
}
+ public Equalizer CreateEqualizer () {
+ lock (equalizers) {
+ EqualizerRecord rec = new EqualizerRecord ();
+ Equalizer eq = new Equalizer (rec);
+
+ eqsrec.Add (rec);
+ equalizers.Add (eq);
+
+ return eq;
+ }
+ }
+
+ public void RemoveEqualizer (Equalizer eq) {
+ lock (equalizers) {
+ equalizers.Remove (eq);
+ eqsrec.Remove (eq.EqualizerRecord);
+ }
+ }
+
public static Device[] ListDevices () {
GLib.List list = new GLib.List (ipod_device_list_devices ());
Added: trunk/ipod-sharp/src/Equalizer.cs
===================================================================
--- trunk/ipod-sharp/src/Equalizer.cs 2005-06-18 20:24:19 UTC (rev 46190)
+++ trunk/ipod-sharp/src/Equalizer.cs 2005-06-18 20:31:58 UTC (rev 46191)
@@ -0,0 +1,175 @@
+using System;
+using System.IO;
+using System.Collections;
+using System.Text;
+
+namespace IPod {
+
+ public class Equalizer {
+
+ private EqualizerRecord record;
+
+ internal EqualizerRecord EqualizerRecord {
+ get { return record; }
+ set { record = value; }
+ }
+
+ public string Name {
+ get { return record.PresetName; }
+ set { record.PresetName = value; }
+ }
+
+ public int PreAmp {
+ get { return record.PreAmp; }
+ set { record.PreAmp = value; }
+ }
+
+ public int BandCount {
+ get { return record.BandCount; }
+ }
+
+ // Some kind of better API would be nicer here.
+ // Perhaps percentage values?
+ public int[] BandValues {
+ get { return record.BandValues; }
+ set {
+ if (value.Length != BandCount)
+ throw new InvalidOperationException ("array length must be
equal to BandCount");
+
+ // FIXME: maybe we should just normalize it to -1200/1200?
+ foreach (int band in value) {
+ if (band < -1200 || band > 1200)
+ throw new InvalidOperationException ("band values must
be between -1200 and 1200");
+ }
+
+ record.BandValues = value;
+ }
+ }
+
+ internal Equalizer (EqualizerRecord record) {
+ this.record = record;
+ }
+ }
+
+ internal class EqualizerRecord {
+
+ public string PresetName;
+ public int PreAmp;
+ public int BandCount = 5;
+ public int[] BandValues;
+
+ private int largeBandCount = 10;
+ private byte[] largeBandData = new byte[40]; // ten 4 byte integers
+ private string headerName = "pqed";
+ private UInt16 unknownOne;
+
+ private const int PresetNameLength = 510;
+
+ public void Read (byte[] data) {
+ headerName = Encoding.ASCII.GetString (data, 0, 4);
+
+ // this is allegedly the length of the preset name
+ // but that doesn't seem to be the case. It contains values
+ // anywhere between 8 and 14 that I've seen, and the preset name
+ // is always 510 bytes.
+ unknownOne = BitConverter.ToUInt16 (data, 4);
+ PresetName = Encoding.ASCII.GetString (data, 6,
PresetNameLength).Trim ((char) 0);
+ PreAmp = BitConverter.ToInt32 (data, 6 + PresetNameLength);
+ largeBandCount = BitConverter.ToInt32 (data, 10 +
PresetNameLength);
+ largeBandData = new byte[4 * largeBandCount];
+ Array.Copy (data, 14 + PresetNameLength, largeBandData, 0, 4 *
largeBandCount);
+
+ int offset = 14 + PresetNameLength + (4 * largeBandCount);
+ BandCount = BitConverter.ToInt32 (data, offset);
+ BandValues = new int[BandCount];
+
+ offset += 4;
+ for (int i = 0; i < BandCount; i++) {
+ BandValues[i] = BitConverter.ToInt32 (data, offset);
+ offset += 4;
+ }
+
+ }
+
+ public void Save (BinaryWriter writer) {
+ writer.Write (Encoding.ASCII.GetBytes (headerName));
+ writer.Write (unknownOne);
+
+ byte[] nameBytes = Encoding.ASCII.GetBytes (PresetName);
+ writer.Write (nameBytes, 0,
+ nameBytes.Length > PresetNameLength ?
PresetNameLength : nameBytes.Length);
+
+ // pad the name
+ if (PresetNameLength > nameBytes.Length) {
+ writer.Write (new byte[PresetNameLength - nameBytes.Length]);
+ }
+
+ writer.Write (PreAmp);
+ writer.Write (largeBandCount);
+ writer.Write (largeBandData);
+ writer.Write (BandCount);
+
+ for (int i = 0; i < BandCount; i++) {
+ writer.Write (BandValues[i]);
+ }
+ }
+ }
+
+ internal class EqualizerContainerRecord {
+
+ private int headerId;
+ private int unknownOne;
+ private int unknownTwo;
+ private int eqSize;
+
+ private ArrayList equalizers = new ArrayList ();
+
+ public EqualizerRecord[] EqualizerRecords {
+ get {
+ return (EqualizerRecord[]) equalizers.ToArray (typeof
(EqualizerRecord));
+ }
+ }
+
+ public void Remove (EqualizerRecord record) {
+ equalizers.Remove (record);
+ }
+
+ public void Add (EqualizerRecord record) {
+ equalizers.Add (record);
+ }
+
+ public void Read (BinaryReader reader) {
+ byte[] header = reader.ReadBytes (8);
+
+ headerId = BitConverter.ToInt32 (header, 0);
+ int headerSize = BitConverter.ToInt32 (header, 4);
+
+ byte[] remainder = reader.ReadBytes (headerSize - 8);
+ unknownOne = BitConverter.ToInt32 (remainder, 0);
+ unknownTwo = BitConverter.ToInt32 (remainder, 4);
+ int numEq = BitConverter.ToInt32 (remainder, 8);
+ eqSize = BitConverter.ToInt32 (remainder, 12);
+
+ for (int i = 0; i < numEq; i++) {
+ EqualizerRecord eqrec = new EqualizerRecord ();
+ eqrec.Read (reader.ReadBytes (eqSize));
+
+ Add (eqrec);
+ }
+ }
+
+ public void Save (BinaryWriter writer) {
+ writer.Write (headerId);
+ writer.Write (24 + Record.PadLength);
+ writer.Write (unknownOne);
+ writer.Write (unknownTwo);
+ writer.Write (equalizers.Count);
+ writer.Write (eqSize);
+ writer.Write (new byte[Record.PadLength]);
+
+ foreach (EqualizerRecord eq in equalizers) {
+ eq.Save (writer);
+ }
+ }
+ }
+}
Modified: trunk/ipod-sharp/src/Makefile.am
===================================================================
--- trunk/ipod-sharp/src/Makefile.am 2005-06-18 20:24:19 UTC (rev 46190)
+++ trunk/ipod-sharp/src/Makefile.am 2005-06-18 20:31:58 UTC (rev 46191)
@@ -12,6 +12,7 @@
$(srcdir)/DeviceModel.cs \
$(srcdir)/DeviceException.cs \
$(srcdir)/DeviceBusyException.cs \
+ $(srcdir)/Equalizer.cs \
$(srcdir)/Song.cs \
$(srcdir)/Playlist.cs \
$(srcdir)/SongDatabase.cs
Modified: trunk/ipod-sharp/src/SongDatabase.cs
===================================================================
--- trunk/ipod-sharp/src/SongDatabase.cs 2005-06-18 20:24:19 UTC (rev
46190)
+++ trunk/ipod-sharp/src/SongDatabase.cs 2005-06-18 20:31:58 UTC (rev
46191)
@@ -23,7 +23,7 @@
internal abstract class Record {
- protected const int PadLength = 2;
+ public const int PadLength = 2;
public string Name;
public int HeaderOne; // usually the size of this record
@@ -837,9 +837,15 @@
private ArrayList playlists = new ArrayList ();
private Device device;
- private string dbPath;
- private string musicDirectory;
+ private string SongDbPath {
+ get { return device.MountPoint + "/iPod_Control/iTunes/iTunesDB"; }
+ }
+
+ private string MusicPath {
+ get { return device.MountPoint + "/iPod_Control/Music/" +
MusicFolder; }
+ }
+
public Song[] Songs {
get {
return (Song[]) songs.ToArray (typeof (Song));
@@ -869,10 +875,6 @@
playlists.Clear ();
}
- private string GetDbPath () {
- return device.MountPoint + "/iPod_Control/iTunes/iTunesDB";
- }
-
private void LoadPlayCounts () {
string path = device.MountPoint + "/iPod_Control/iTunes/Play
Counts";
@@ -940,10 +942,8 @@
lock (this) {
this.device = device;
- this.dbPath = GetDbPath ();
- this.musicDirectory = device.MountPoint +
"/iPod_Control/Music/" + MusicFolder;
- using (BinaryReader reader = new BinaryReader (new FileStream
(dbPath, FileMode.Open))) {
+ using (BinaryReader reader = new BinaryReader (new FileStream
(SongDbPath, FileMode.Open))) {
Clear ();
@@ -1009,11 +1009,11 @@
CheckFreeSpace ();
// Back up the current song db
- File.Copy (dbPath, dbPath + ".bak", true);
-
+ File.Copy (SongDbPath, SongDbPath + ".bak", true);
+
try {
// Save the songs db
- using (BinaryWriter writer = new BinaryWriter (new FileStream
(dbPath, FileMode.Create))) {
+ using (BinaryWriter writer = new BinaryWriter (new FileStream
(SongDbPath, FileMode.Create))) {
dbrec.Save (dbrec, writer);
}
@@ -1022,8 +1022,8 @@
File.Delete (song.Filename);
}
- if (!Directory.Exists (musicDirectory))
- Directory.CreateDirectory (musicDirectory);
+ if (!Directory.Exists (MusicPath))
+ Directory.CreateDirectory (MusicPath);
int completed = 0;
@@ -1039,8 +1039,8 @@
if (handler != null)
handler (this, null, songsToAdd.Count, songsToAdd.Count);
} catch (Exception e) {
- // rollback the songs db
- File.Copy (dbPath + ".bak", dbPath, true);
+ // rollback the song db
+ File.Copy (SongDbPath + ".bak", SongDbPath, true);
throw e;
}
}
Modified: trunk/ipod-sharp/tests/IPodTests.cs
===================================================================
--- trunk/ipod-sharp/tests/IPodTests.cs 2005-06-18 20:24:19 UTC (rev 46190)
+++ trunk/ipod-sharp/tests/IPodTests.cs 2005-06-18 20:31:58 UTC (rev 46191)
@@ -14,8 +14,6 @@
private const string testdir = "/tmp/ipod-sharp-tests";
private const string playlistName = "Foo Playlist";
-
- // The following tests should all succeed.
[SetUp]
public void SetUp () {
@@ -91,7 +89,16 @@
return null;
}
+ // The following tests should all succeed.
+
[Test]
+ public void SimpleTest () {
+ Device device = OpenDevice ();
+ device.Save ();
+ device.SongDatabase.Save ();
+ }
+
+ [Test]
public void AddSongTest () {
SongDatabase db = OpenDevice ().SongDatabase;
@@ -327,18 +334,92 @@
}
[Test]
- public void DeviceVolumeTest () {
+ public void DeviceVolumeSizeTest () {
Device device = OpenDevice ();
Assert.IsTrue (device.VolumeSize > 0);
}
+ private Equalizer FindEq (Device device, string name) {
+ foreach (Equalizer eq in device.Equalizers) {
+ if (eq.Name == name)
+ return eq;
+ }
+
+ return null;
+ }
+
+ private Equalizer AddEq (Device device) {
+ Equalizer eq = device.CreateEqualizer ();
+
+ eq.Name = "my name";
+ eq.PreAmp = 50;
+
+ int[] bands = new int[] { 200, 20, 10, 5, 300 };
+ eq.BandValues = bands;
+
+ return eq;
+ }
+
[Test]
- public void SimpleTest () {
- SongDatabase db = OpenDevice ().SongDatabase;
- db.Save ();
+ public void AddEqualizerTest () {
+ Device device = OpenDevice ();
+
+ Equalizer eq = AddEq (device);
+ string name = eq.Name;
+ int preamp = eq.PreAmp;
+ int[] bands = eq.BandValues;
+
+ device.Save ();
+ device = GetDevice ();
+
+ eq = FindEq (device, name);
+
+ Assert.IsNotNull (eq);
+ Assert.AreEqual (name, eq.Name);
+ Assert.AreEqual (preamp, eq.PreAmp);
+ Assert.AreEqual (bands, eq.BandValues);
}
+ [Test]
+ public void RemoveEqualizerTest () {
+ Device device = OpenDevice ();
+
+ string name = AddEq (device).Name;
+
+ device.Save ();
+ device = GetDevice ();
+
+ Equalizer eq = FindEq (device, name);
+
+ Assert.IsNotNull (eq);
+
+ device.RemoveEqualizer (eq);
+ device.Save ();
+
+ device = GetDevice ();
+
+ Assert.IsNull (FindEq (device, name));
+ }
+
+ [Test]
+ public void EqualizerOverflowTest () {
+ Device device = OpenDevice ();
+
+ Equalizer eq = AddEq (device);
+
+ // the iTunesDB file only allows 510 chars here, so intentionally
+ // overflow it to make sure it gets truncated properly.
+ eq.Name = "my eq".PadRight (1000);
+
+ device.Save ();
+ device = GetDevice ();
+
+ eq = FindEq (device, "my eq".PadRight (510));
+
+ Assert.IsNotNull (eq);
+ }
+
// The following tests should all "fail".
[Test]
@@ -420,5 +501,27 @@
Playlist otg = GetOTGPlaylist (db);
db.RemovePlaylist (otg);
}
+
+ [Test]
+ [ExpectedException (typeof (InvalidOperationException))]
+ public void EqualizerNotEnoughBandsTest () {
+ Device device = OpenDevice ();
+
+ Equalizer eq = AddEq (device);
+ eq.BandValues = new int[] { 5, 4, 3 };
+
+ device.Save ();
+ }
+
+ [Test]
+ [ExpectedException (typeof (InvalidOperationException))]
+ public void EqualizerInvalidBandsTest () {
+ Device device = OpenDevice ();
+
+ Equalizer eq = AddEq (device);
+ eq.BandValues = new int[] { 5, 4, 5000, 0, 0 };
+
+ device.Save ();
+ }
}
}
Modified: trunk/ipod-sharp/tests/ipod-test-db-v12-a.tar.gz
===================================================================
(Binary files differ)
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches