I'm trying to create LAME mp3 encoder GUI with Objective-C. I've learned many thing and copied many codes from iTunes-LAME.app. This time, I copied and modified id3tag writing function from iTunes-LAME.app. But in the copied and modified function, I got corrupted characters with 2byte language. I think may be something wrong with it but I can't figure out what is wrong. Here's the function. Please help me!!
EDIT: My modified id3tag writing function is works fine for single-byte language and numeric values.
struct id3_tag *id3tag;
struct id3_frame *frame = NULL;
frame = id3_frame_new(ID3_FRAME_TITLE);
id3tag = id3_tag_new();
id3_tag_attachframe(id3tag, frame);
id3_ucs4_t *ucs4 = id3_utf8_ucs4duplicate((const id3_utf8_t*)@"日本語".UTF8String);
if (id3_field_setstrings(&frame->fields[1], 1, &ucs4) == -1) {
NSLog(@"Unable to set frame %s",ID3_FRAME_TITLE);
id3_frame_delete(frame);
}
id3tag->options = 0;
id3tag->flags |= ID3_TAG_FLAG_FOOTERPRESENT;
id3_length_t len = id3_tag_render(id3tag, NULL);
char *buf = (char*)malloc(len);
len = id3_tag_render(id3tag, (id3_byte_t *)buf);
const char *filename=@"/Users/wayfarer/Desktop/01 ヴィーナスは眠れない.mp3".UTF8String;
int fd = open(filename, O_RDWR, 0644);
long fsize = lseek(fd, 0, SEEK_END);
long p = fsize;
int chunkSize = 1024 * 128;
buf = (char*)malloc(chunkSize);
while (p > 0) {
p -= chunkSize;
if (p < 0) {
chunkSize += p;
p = 0;
}
pread(fd, buf, chunkSize, p);
pwrite(fd, buf, chunkSize, p+len);
}
lseek(fd, 0, SEEK_SET);
write(fd, buf, len);
free(buf);
close(fd);
id3_tag_delete(id3tag);