/*************************************************************************************** Analyze - Sample 1 Copyright (C) 2000-2001 by Matthew T. Ashland All Rights Reserved. Feel free to use this code in any way that you like. This example opens an APE file and displays some basic information about it. To use it, just type Sample 1.exe followed by a file name and it'll display information about that file. Notes for use in a new project: -you need to include "MACLib.lib" in the included libraries list -life will be easier if you set the [MAC SDK]\\Shared directory as an include directory and an additional library input path in the project settings -set the runtime library to "Mutlithreaded" WARNING: -This class driven system for using Monkey's Audio is still in development, so I can't make any guarantees that the classes and libraries won't change before everything gets finalized. Use them at your own risk. ***************************************************************************************/ // includes #include #include #include #include "All.h" #include "GlobalFunctions.h" #include "MACLib.h" #include "CharacterHelper.h" #include "APETag.h" #define CPPVAL ( VALUE (*) (...) ) extern "C" { static VALUE mMac; static VALUE cInfo; static VALUE rb_cinfo_new(VALUE self); static VALUE rb_mac_info(VALUE module, VALUE filename); void Init_rmac() { mMac = rb_define_module("Mac"); cInfo = rb_define_class_under(mMac,"Info",rb_cObject); #define NA 16 const char *attr2[NA] = { "version","compression_level","samplerate","bps","channels","peak","length", "filesize", "id3_tag", "ape_tag", "artist","album","title","genre","track","year" }; for (int i = 0; i < NA; i++) { rb_define_attr(cInfo, attr2[i], 1, 0); } rb_define_module_function(mMac,"info", CPPVAL rb_mac_info ,1); } } VALUE rb_mac_info(VALUE module, VALUE filename) { VALUE info=rb_class_new_instance(0,NULL,cInfo); /////////////////////////////////////////////////////////////////////////////// // variable declares /////////////////////////////////////////////////////////////////////////////// int nRetVal = 0; // generic holder for return values char cTempBuffer[256]; ZeroMemory(&cTempBuffer[0], 256); // generic buffer for string stuff char * pFilename = STR2CSTR(filename); // the file to open IAPEDecompress * pAPEDecompress = NULL; // APE interface CSmartPtr spInput; spInput.Assign(GetUTF16FromANSI(pFilename), TRUE); //* /////////////////////////////////////////////////////////////////////////////// // open the file and error check /////////////////////////////////////////////////////////////////////////////// pAPEDecompress = CreateIAPEDecompress(spInput, &nRetVal); if (pAPEDecompress == NULL) { rb_raise(rb_eIOError, "Error opening APE file. (error code %d)", nRetVal); } /////////////////////////////////////////////////////////////////////////////// // display some information about the file /////////////////////////////////////////////////////////////////////////////// // file format information char ape_version[5]; sprintf(ape_version,"%02.3f",float(pAPEDecompress->GetInfo(APE_INFO_FILE_VERSION)/1000.0)); rb_iv_set(info,"@version", rb_str_new2(ape_version)); switch (pAPEDecompress->GetInfo(APE_INFO_COMPRESSION_LEVEL)) { case COMPRESSION_LEVEL_FAST: rb_iv_set(info,"@compression_level",rb_str_new2("Fast")); break; case COMPRESSION_LEVEL_NORMAL: rb_iv_set(info,"@compression_level",rb_str_new2("Normal")); break; case COMPRESSION_LEVEL_HIGH: rb_iv_set(info,"@compression_level",rb_str_new2("High")); break; case COMPRESSION_LEVEL_EXTRA_HIGH: rb_iv_set(info,"@compression_level",rb_str_new2("Extra High")); break; } // audio format information rb_iv_set(info,"@samplerate",INT2FIX(pAPEDecompress->GetInfo(APE_INFO_SAMPLE_RATE))); rb_iv_set(info,"@bps",INT2FIX(pAPEDecompress->GetInfo(APE_INFO_BITS_PER_SAMPLE))); rb_iv_set(info,"@channels",INT2FIX(pAPEDecompress->GetInfo(APE_INFO_CHANNELS))); rb_iv_set(info,"@peak",INT2FIX(pAPEDecompress->GetInfo(APE_INFO_PEAK_LEVEL))); // size and duration information rb_iv_set(info,"@length",rb_float_new((pAPEDecompress->GetInfo(APE_INFO_LENGTH_MS) / 1000.00))); rb_iv_set(info,"@filesize",INT2FIX(pAPEDecompress->GetInfo(APE_INFO_APE_TOTAL_BYTES))); // tag information CAPETag * pAPETag = (CAPETag *) pAPEDecompress->GetInfo(APE_INFO_TAG); BOOL bHasID3Tag = pAPETag->GetHasID3Tag(); BOOL bHasAPETag = pAPETag->GetHasAPETag(); rb_iv_set(info,"@id3_tag",(bHasID3Tag?Qtrue:Qfalse)); rb_iv_set(info,"@ape_tag",(bHasAPETag?Qtrue:Qfalse)); if (bHasID3Tag || bHasAPETag) { if (bHasAPETag) { char ape_tag_version[5]; sprintf(ape_tag_version,"%05.3f",float(pAPETag->GetAPETagVersion()/1000.0)); rb_iv_set(info,"@ape_tag_version",rb_str_new2(ape_tag_version)); } // iterate through all the tag fields BOOL bFirst = TRUE; CAPETagField * pTagField; // while (pAPETag->GetNextTagField(bFirst, &pTagField)) int index = 0; while ((pTagField = pAPETag->GetTagField(index)) != NULL) { bFirst = FALSE; index ++; // output the tag field properties (don't output huge fields like images, etc.) if (pTagField->GetFieldValueSize() > 128) { //printf("\t%s: --- too much data to display ---\r\n", GetANSIFromUTF16(pTagField->GetFieldName())); } else { const wchar_t *fieldName; const char *fieldValue; char *value; fieldName = pTagField->GetFieldName(); fieldValue = pTagField->GetFieldValue(); strcpy(cTempBuffer,"@"); strcat(cTempBuffer,(char *)GetUTF8FromUTF16(fieldName)); cTempBuffer[1]=tolower(cTempBuffer[1]); rb_iv_set(info,cTempBuffer,rb_str_new2(fieldValue)); } } } /////////////////////////////////////////////////////////////////////////////// // cleanup (just delete the object /////////////////////////////////////////////////////////////////////////////// delete pAPEDecompress; /////////////////////////////////////////////////////////////////////////////// // quit /////////////////////////////////////////////////////////////////////////////// return info; }