2006-04-13 Aaron Bockover <aaron@abock.org>

* src/SongFrame.cs: Fixed frame creation

    * src/Device.cs: Fixed send track support; added DeleteSong support

    * src/Utility.cs: Added FreeStringPtr

    * src/Song.cs: Added set accessors to common frame properties


svn path=/trunk/njb-sharp/; revision=59453
This commit is contained in:
Aaron Bockover 2006-04-13 15:36:26 +00:00
Родитель b77a5bd76d
Коммит 624045aeee
5 изменённых файлов: 190 добавлений и 46 удалений

Просмотреть файл

@ -1,3 +1,13 @@
2006-04-13 Aaron Bockover <aaron@abock.org>
* src/SongFrame.cs: Fixed frame creation
* src/Device.cs: Fixed send track support; added DeleteSong support
* src/Utility.cs: Added FreeStringPtr
* src/Song.cs: Added set accessors to common frame properties
2005-12-22 Aaron Bockover <aaron@aaronbock.net> 2005-12-22 Aaron Bockover <aaron@aaronbock.net>
* configure.ac: Bump to 0.2.2 * configure.ac: Bump to 0.2.2

Просмотреть файл

@ -121,6 +121,13 @@ namespace Njb
[DllImport("libnjb")] [DllImport("libnjb")]
private static extern int NJB_Get_Track_fd(IntPtr njb, uint trackid, uint size, int fd, private static extern int NJB_Get_Track_fd(IntPtr njb, uint trackid, uint size, int fd,
NjbXferCallback cb, IntPtr data); NjbXferCallback cb, IntPtr data);
[DllImport("libnjb")]
private static extern int NJB_Send_Track(IntPtr njb, IntPtr path, HandleRef songid,
NjbXferCallback cb, IntPtr data, out uint trackid);
[DllImport("libnjb")]
private static extern int NJB_Delete_Track(IntPtr njb, uint trackid);
[DllImport("libnjbglue")] [DllImport("libnjbglue")]
private static extern IntPtr NJB_Glue_Get_Device(int index); private static extern IntPtr NJB_Glue_Get_Device(int index);
@ -131,7 +138,7 @@ namespace Njb
[DllImport("libnjbglue")] [DllImport("libnjbglue")]
private static extern IntPtr NJB_Glue_Device_Get_Usb_Bus_Path(IntPtr njb); private static extern IntPtr NJB_Glue_Device_Get_Usb_Bus_Path(IntPtr njb);
public event TransferProgressHandler ReadProgressChanged; public event TransferProgressHandler ProgressChanged;
public Device(Discoverer discoverer, int index) public Device(Discoverer discoverer, int index)
{ {
@ -144,13 +151,18 @@ namespace Njb
return NJB_Open(Handle) != -1; return NJB_Open(Handle) != -1;
} }
public void Dispose() public void Close()
{ {
if(Handle != IntPtr.Zero) { if(Handle != IntPtr.Zero) {
NJB_Close(Handle); NJB_Close(Handle);
} }
} }
public void Dispose()
{
Close();
}
public bool Capture() public bool Capture()
{ {
return NJB_Capture(Handle) != -1; return NJB_Capture(Handle) != -1;
@ -367,12 +379,12 @@ namespace Njb
if(NJB_Get_Track_fd(Handle, (uint)song.Id, song.FileSize, if(NJB_Get_Track_fd(Handle, (uint)song.Id, song.FileSize,
stream.Handle, delegate(ulong sent, ulong total, IntPtr buf, uint len, IntPtr data) { stream.Handle, delegate(ulong sent, ulong total, IntPtr buf, uint len, IntPtr data) {
if(ReadProgressChanged != null) { if(ProgressChanged != null) {
TransferProgressArgs args = new TransferProgressArgs(); TransferProgressArgs args = new TransferProgressArgs();
args.Current = sent; args.Current = sent;
args.Total = total; args.Total = total;
args.Song = song; args.Song = song;
ReadProgressChanged(this, args); ProgressChanged(this, args);
} }
}, IntPtr.Zero) == -1) { }, IntPtr.Zero) == -1) {
stream.Close(); stream.Close();
@ -382,6 +394,37 @@ namespace Njb
stream.Close(); stream.Close();
} }
public void SendSong(Song song, string path)
{
IntPtr path_ptr = Utility.Utf8StringToPtr(path);
try {
uint trackid;
if(NJB_Send_Track(Handle, path_ptr, song.Handle,
delegate(ulong sent, ulong total, IntPtr buf, uint len, IntPtr data) {
if(ProgressChanged != null) {
TransferProgressArgs args = new TransferProgressArgs();
args.Current = sent;
args.Total = total;
args.Song = song;
ProgressChanged(this, args);
}
}, IntPtr.Zero, out trackid) == -1) {
throw new ApplicationException("Could not transfer song");
}
} finally {
Utility.FreeStringPtr(path_ptr);
}
}
public void DeleteSong(Song song)
{
if(NJB_Delete_Track(Handle, (uint)song.Id) == -1) {
throw new ApplicationException("Could not delete song " + song.Id);
}
}
public ICollection GetDataFiles() public ICollection GetDataFiles()
{ {
ArrayList list = new ArrayList(); ArrayList list = new ArrayList();

Просмотреть файл

@ -77,6 +77,9 @@ namespace Njb
[DllImport("libnjb")] [DllImport("libnjb")]
private static extern IntPtr NJB_Songid_Getframe(HandleRef handle); private static extern IntPtr NJB_Songid_Getframe(HandleRef handle);
[DllImport("libnjb")]
private static extern void NJB_Songid_Addframe(HandleRef handle, IntPtr frame);
private HandleRef handle; private HandleRef handle;
private int id; private int id;
private int nframes; private int nframes;
@ -154,88 +157,98 @@ namespace Njb
return frame.DataInt; return frame.DataInt;
} }
public string Album { private void AddFrame(SongFrame frame)
get { {
return GetFrameString(FrameName.Album); if(frame == null) {
return;
} }
NJB_Songid_Addframe(Handle, frame.Handle);
}
private void AddFrame(string label, string value)
{
AddFrame(SongFrame.New(label, value));
}
private void AddFrame(string label, uint value)
{
AddFrame(SongFrame.New(label, value));
}
private void AddFrame(string label, ushort value)
{
AddFrame(SongFrame.New(label, value));
}
public string Album {
get { return GetFrameString(FrameName.Album); }
set { AddFrame(FrameName.Album, value); }
} }
public string Artist { public string Artist {
get { get { return GetFrameString(FrameName.Artist); }
return GetFrameString(FrameName.Artist); set { AddFrame(FrameName.Artist, value); }
}
} }
public uint Bitrate { public uint Bitrate {
get { get { return GetFrameInt(FrameName.Bitrate); }
return GetFrameInt(FrameName.Bitrate); set { AddFrame(FrameName.Bitrate, value); }
}
} }
public string Codec { public string Codec {
get { get { return GetFrameString(FrameName.Codec); }
return GetFrameString(FrameName.Codec); set { AddFrame(SongFrame.NewCodec(value)); }
}
} }
public string Comment { public string Comment {
get { get { return GetFrameString(FrameName.Comment); }
return GetFrameString(FrameName.Comment); set { AddFrame(FrameName.Comment, value); }
}
} }
public string FileName { public string FileName {
get { get { return GetFrameString(FrameName.FileName); }
return GetFrameString(FrameName.FileName); set { AddFrame(FrameName.FileName, value); }
}
} }
public uint FileSize { public uint FileSize {
get { get { return GetFrameInt(FrameName.FileSize); }
return GetFrameInt(FrameName.FileSize); set { AddFrame(FrameName.FileSize, value); }
}
} }
public string Folder { public string Folder {
get { get { return GetFrameString(FrameName.Folder); }
return GetFrameString(FrameName.Folder); set { AddFrame(FrameName.Folder, value); }
}
} }
public string Genre { public string Genre {
get { get { return GetFrameString(FrameName.Genre); }
return GetFrameString(FrameName.Genre); set { AddFrame(FrameName.Genre, value); }
}
} }
public TimeSpan Duration { public TimeSpan Duration {
get { get { return new TimeSpan(GetFrameShort(FrameName.Length) * TimeSpan.TicksPerSecond); }
return new TimeSpan(GetFrameShort(FrameName.Length) * TimeSpan.TicksPerSecond); set { AddFrame(FrameName.Length, (ushort)value.TotalSeconds); }
}
} }
public bool IsProtected { public bool IsProtected {
get { get { return GetFrameShort(FrameName.Protected) != 0; }
return GetFrameShort(FrameName.Protected) != 0; set { AddFrame(FrameName.Protected, (uint)(value == true ? 1 : 0)); }
}
} }
public string Title { public string Title {
get { get { return GetFrameString(FrameName.Title); }
return GetFrameString(FrameName.Title); set { AddFrame(FrameName.Title, value); }
}
} }
public ushort TrackNumber { public ushort TrackNumber {
get { get { return GetFrameShort(FrameName.TrackNumber); }
return GetFrameShort(FrameName.TrackNumber); set { AddFrame(FrameName.TrackNumber, value); }
}
} }
public ushort Year { public ushort Year {
get { get { return GetFrameShort(FrameName.Year); }
return GetFrameShort(FrameName.Year); set { AddFrame(FrameName.Year, value); }
}
} }
public ICollection GetFrames() public ICollection GetFrames()
@ -271,4 +284,12 @@ namespace Njb
return text.ToString(); return text.ToString();
} }
} }
public sealed class Codec
{
public static readonly string Mp3 = "MP3";
public static readonly string Wma = "WMA";
public static readonly string Wav = "WAV";
public static readonly string Audible = "AA";
}
} }

Просмотреть файл

@ -65,6 +65,20 @@ namespace Njb
[DllImport("libnjb")] [DllImport("libnjb")]
private static extern IntPtr NJB_Songid_Findframe(HandleRef handle, IntPtr label); private static extern IntPtr NJB_Songid_Findframe(HandleRef handle, IntPtr label);
[DllImport("libnjb")]
private static extern IntPtr NJB_Songid_Frame_New_String(IntPtr label, IntPtr value);
[DllImport("libnjb")]
private static extern IntPtr NJB_Songid_Frame_New_Uint16(IntPtr label, ushort value);
[DllImport("libnjb")]
private static extern IntPtr NJB_Songid_Frame_New_Uint32(IntPtr label, uint value);
[DllImport("libnjb")]
private static extern IntPtr NJB_Songid_Frame_New_Codec(IntPtr codec);
private IntPtr handle;
public static SongFrame Find(Song song, string label) public static SongFrame Find(Song song, string label)
{ {
IntPtr ptr = NJB_Songid_Findframe(song.Handle, Utility.Utf8StringToPtr(label)); IntPtr ptr = NJB_Songid_Findframe(song.Handle, Utility.Utf8StringToPtr(label));
@ -75,9 +89,56 @@ namespace Njb
return new SongFrame(ptr); return new SongFrame(ptr);
} }
public static SongFrame New(string label, string value)
{
if(value == null) {
return null;
}
IntPtr label_ptr = Utility.Utf8StringToPtr(label);
IntPtr value_ptr = Utility.Utf8StringToPtr(value);
SongFrame frame = new SongFrame(NJB_Songid_Frame_New_String(label_ptr, value_ptr));
Utility.FreeStringPtr(label_ptr);
Utility.FreeStringPtr(value_ptr);
return frame;
}
public static SongFrame New(string label, uint value)
{
IntPtr label_ptr = Utility.Utf8StringToPtr(label);
SongFrame frame = new SongFrame(NJB_Songid_Frame_New_Uint32(label_ptr, value));
Utility.FreeStringPtr(label_ptr);
return frame;
}
public static SongFrame New(string label, ushort value)
{
IntPtr label_ptr = Utility.Utf8StringToPtr(label);
SongFrame frame = new SongFrame(NJB_Songid_Frame_New_Uint16(label_ptr, value));
Utility.FreeStringPtr(label_ptr);
return frame;
}
public static SongFrame NewCodec(string codec)
{
IntPtr codec_ptr = Utility.Utf8StringToPtr(codec);
SongFrame frame = new SongFrame(NJB_Songid_Frame_New_Codec(codec_ptr));
Utility.FreeStringPtr(codec_ptr);
return frame;
}
public SongFrame(IntPtr framePtr) public SongFrame(IntPtr framePtr)
{ {
if(framePtr == IntPtr.Zero) {
throw new ApplicationException("Could not create SongFrame");
}
label = Utility.PtrToUtf8String(NJB_Glue_Song_Frame_Get_Label(framePtr)); label = Utility.PtrToUtf8String(NJB_Glue_Song_Frame_Get_Label(framePtr));
handle = framePtr;
switch(NJB_Glue_Song_Frame_Get_Type(framePtr)) { switch(NJB_Glue_Song_Frame_Get_Type(framePtr)) {
case 0x00: case 0x00:
@ -97,6 +158,10 @@ namespace Njb
} }
} }
public IntPtr Handle {
get { return handle; }
}
public string Label { public string Label {
get { get {
return label; return label;

Просмотреть файл

@ -60,5 +60,10 @@ namespace Njb
Marshal.WriteByte(memalloc, bytes.Length, 0); Marshal.WriteByte(memalloc, bytes.Length, 0);
return memalloc; return memalloc;
} }
public static void FreeStringPtr(IntPtr ptr)
{
Marshal.FreeHGlobal(ptr);
}
} }
} }