AudioMange.c 14.0 KB
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
/****************************************
 * 
 *   音频文件管理模块
 * 
 * *************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "./AudioMange.h"
#include "JZsdkLib.h"


/******************************
 * 
 *  创建新的歌曲节点
 *  输入 新的歌曲节点的地址,歌曲信息结构体
 * 
 * ***********************************/
T_JZsdkReturnCode Jzsdk_Create_AndioMange_Node(struct AudioMange_Node **newNode, const struct AudioMange_audio_struct audioInfo)
{
    if (audioInfo.FileName == NULL)
    {
        JZSDK_LOG_ERROR("歌曲节点创建失败, 无歌曲信息");
        return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;
    }

    if (*newNode != NULL)
    {
        JZSDK_LOG_ERROR("歌曲节点创建失败, 输入的节点已非空");
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;
    }  

    *newNode = (struct AudioMange_Node*)malloc(sizeof(struct AudioMange_Node)); 
    if (*newNode == NULL) 
    {  
        JZSDK_LOG_ERROR("歌曲节点创建失败");
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER; 
    }  
    
    //输入歌曲信息
    memcpy(&(*newNode)->audioInfo, &audioInfo, sizeof(struct AudioMange_audio_struct)); 

    (*newNode)->prev = NULL;    
    (*newNode)->next = NULL;    

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}

/******************************
 * 
 *  添加新的歌曲节点到记录的链表
 *  输入 记录链表,尾部位置,以及新的节点
 * 
 * ***********************************/
T_JZsdkReturnCode Jzsdk_AddAudioNodeToCircularList(struct AudioMange_Node **DirectoryNode, struct AudioMange_Node **TailNode, struct AudioMange_Node **newNode)
{
    if ((*newNode) == NULL) 
    {  
        JZSDK_LOG_ERROR("新音乐节点链接失败"); 
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    // 如果链表为空,新节点既是头节点也是尾节点  
    if ( (*DirectoryNode) == NULL) 
    {  
        (*DirectoryNode) = (*newNode);  
        (*DirectoryNode)->next = (*newNode);
        (*DirectoryNode)->prev = (*newNode);
        (*TailNode) = (*newNode);
    } 
    else
    {  
        // 将新节点添加到链表的末尾  
        (*newNode)->next = (*DirectoryNode); // 新节点的next指向头节点  
        (*newNode)->prev = (*TailNode); // 新节点的prev指向尾节点
        (*TailNode)->next = (*newNode); // 尾节点的next指向新节点 
        (*DirectoryNode)->prev = (*newNode); // 头节点的prev指向新节点 
        (*TailNode) = (*newNode); // 更新尾节点
    }

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS; 
}

//根据当前的索引值,获取当前的歌曲节点  
T_JZsdkReturnCode getAudioMange_NodeAtIndex(struct AudioMange_Node *head,struct AudioMange_Node **currentNode, int index) 
{  
    if (head == NULL) 
    {  
        JZSDK_LOG_ERROR("索引地址为空");
        return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
    }  
      
    struct AudioMange_Node *current = head;  
    int currentIndex = 0;  

    do  
    {  
        if (currentIndex == index) 
        {  
            (*currentNode) = current;
            return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
        }   
        current = current->next;  
        currentIndex++;  
    } while (current != head); // 遍历直到回到头节点  
    
    return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE; // 如果没有找到指定索引的节点,返回NULL  
}  

// // 根据文件名删除音频节点  
// void deleteAudioMange_NodeByName(struct AudioMange_Node **head, const char *fileName) {  
//     struct AudioMange_Node *current = *head;  
//     struct AudioMange_Node *prev = NULL;  
      
//     while (current != NULL) {  
//         if (strcmp(current->audioInfo.FileName, fileName) == 0) {  
//             // 找到要删除的节点  
//             if (prev == NULL) {  
//                 // 如果要删除的节点是头节点  
//                 *head = current->next;  
//             } else {  
//                 // 更新相邻节点的指针  
//                 prev->next = current->next;  
//             }  
              
//             if (current->next != NULL) {  
//                 current->next->prev = prev; // 如果被删除节点不是尾节点,则更新其后继节点的prev指针  
//             }  
              
//             // 释放被删除节点的内存  
//             free(current);  
//             return;  
//         }  
//         prev = current;  
//         current = current->next;  
//     }  
      
//     // 如果没有找到要删除的节点  
//     printf("Node with filename '%s' not found in the list.\n", fileName);  
// }


/*************
 * 
 * 删除节点
 *  
 *  
 * ***************************/
T_JZsdkReturnCode deleteAudioMange_NodeByName(struct AudioMange_Node **head, const unsigned char *deleteFileName)
{  
    int flag = JZ_FLAGCODE_OFF;
    if ((*head) == NULL) 
    {  
        JZSDK_LOG_ERROR("找不到删除的音乐列表"); 
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    // 如果链表中只有一个节点  
    if ((*head)->next == *head)   
    {  
        if (strcmp((*head)->audioInfo.FileName, deleteFileName) == 0)  
        {  
            free(*head);  
            *head = NULL;  
            return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
        }  
        else  
        {  
            return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
        }  
    }  
  
    struct AudioMange_Node *current = *head;   
    struct AudioMange_Node *prev = NULL;  
  
    // 寻找被删除节点在列表中的位置  
    do    
    {    
        if (strcmp(current->audioInfo.FileName, deleteFileName) == 0)   
        {  
            break;  
        }  
        prev = current;  
        current = current->next;  
    } while (current != *head); // 遍历直到回到头节点  
  
    if (current == *head) // 没有找到要删除的节点  
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
    }  
  
    // 删除节点  
    prev->next = current->next;  
    current->next->prev = prev;  
    free(current);  
  
    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
}

/*************
 * 
 * 查询歌曲是否在链表中 
 *  // 如果找到匹配的歌曲名,返回成功表示歌曲在链表中 
 *  // 遍历完整个链表后仍未找到,返回失败表示歌曲不在链表中 
 * ***************************/ 
T_JZsdkReturnCode JZsdk_isSongInAudioList(struct AudioMange_Node *head, const struct AudioMange_audio_struct songToFind) 
{  
    if (head == NULL)  
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    struct AudioMange_Node *current = head;
    do  
    {  
        if (strcmp(current->audioInfo.FileName, songToFind.FileName) == 0) 
        {  
            // 如果找到匹配的歌曲名,返回成功表示歌曲在链表中  
            return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
        }   
        current = current->next;  
    } while (current != head); // 遍历直到回到头节点  

    // 遍历完整个链表后仍未找到,返回0表示歌曲不在链表中  
    return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
}  

/*************
 * 
 * 查询歌曲是否在链表中, 并记录下当前节点的序号
 *  // 如果找到匹配的歌曲名,返回成功表示歌曲在链表中 
 *  // 遍历完整个链表后仍未找到,返回失败表示歌曲不在链表中 
 * ***************************/ 
T_JZsdkReturnCode JZsdk_isSongInAudioList_AndSaveCurrentIndex(struct AudioMange_Node *head, const struct AudioMange_audio_struct songToFind, int *index) 
{  
    if (head == NULL)  
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    int currentIndex = 0;

    struct AudioMange_Node *current = head;
    do  
    {  
        if (strcmp(current->audioInfo.FileName, songToFind.FileName) == 0) 
        {  
            // 如果找到匹配的歌曲名,返回成功表示歌曲在链表中
            *index = currentIndex;  
            return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
        }   
        currentIndex++;
        current = current->next;  
    } while (current != head); // 遍历直到回到头节点  

    currentIndex = 0;
    *index = currentIndex;  

    // 遍历完整个链表后仍未找到,返回0表示歌曲不在链表中  
    return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
}  

/*************
 * 
 * 查询歌曲是否在链表中, 并记录下当前节,与索引值
 *  // 如果找到匹配的歌曲名,返回成功表示歌曲在链表中 
 *  // 遍历完整个链表后仍未找到,返回失败表示歌曲不在链表中 
 * ***************************/ 
T_JZsdkReturnCode JZsdk_isSongInAudioList_AndSaveCurrentNode(struct AudioMange_Node *head, const struct AudioMange_audio_struct songToFind, struct AudioMange_Node *CurrentNode, int *index) 
{  
    if (head == NULL)  
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  

    int currentIndex = 0;

    struct AudioMange_Node *current = head;
    do  
    {  
        if (strcmp(current->audioInfo.FileName, songToFind.FileName) == 0) 
        {  
            CurrentNode = current;
            *index = currentIndex;  
            // 如果找到匹配的歌曲名,返回成功表示歌曲在链表中  
            return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
        }   
        currentIndex++;
        current = current->next;  
    } while (current != head); // 遍历直到回到头节点  

    currentIndex = 0;
    *index = currentIndex; 

    // 遍历完整个链表后仍未找到,返回0表示歌曲不在链表中  
    return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
}  

// /*************
//  * 
//  * 查询歌曲是否在链表中,并比文件大小
//  *  // 如果找到匹配的歌曲名,且大小相同,返回成功
//  *  // 否则,返回失败
//  * ***************************/ 
// T_JZsdkReturnCode JZsdk_isSongInAudioList_And_CompareSize(struct AudioMange_Node *head, const struct AudioMange_audio_struct songToFind) 
// {  
//     struct AudioMange_Node *current = head;  
//     while (current != NULL) {  
//         if (strcmp(current->audioInfo.FileName, songToFind.FileName) == 0) {  
//             // 如果找到匹配的歌曲名,表示歌曲在链表中  
//             //接着比对大小
//             if (current->audioInfo.FileSize == songToFind.FileSize)
//             {
//                 printf("是大小相同\n");
//                 return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
//             }
//             else
//             {
//                 printf("是大小不同\n");
//                 return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;   
//             }
//         }  
//         current = current->next;  
//     }  
//     // 遍历完整个链表后仍未找到,返回0表示歌曲不在链表中
//     printf("是不存在\n");  
//     return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
// }  

// 查询歌曲的信息
T_JZsdkReturnCode JZsdk_GetSongInDudioList_AudioInfo(struct AudioMange_Node *head,struct AudioMange_audio_struct *songToFind) 
{  
    struct AudioMange_Node *current = head;  
    while (current != NULL) {  
        if (strcmp(current->audioInfo.FileName, songToFind->FileName) == 0) 
        {  
            songToFind->FileNameLen = current->audioInfo.FileNameLen;
            songToFind->BitRate = current->audioInfo.BitRate;
            songToFind->FileSize = current->audioInfo.FileSize;
            songToFind->FileType = current->audioInfo.FileType;
            songToFind->PlayTime = current->audioInfo.PlayTime;
            songToFind->Time = current->audioInfo.Time;
            return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
        }  
        current = current->next;  
    }  
    // 遍历完整个链表后仍未找到,返回0表示歌曲不在链表中  
    return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
}  

// //设置歌曲的信息
// T_JZsdkReturnCode JZsdk_SetSongInDudioList_AudioInfo(struct AudioMange_Node *head, const struct AudioMange_audio_struct songToFind)
// {
//     struct AudioMange_Node *current = head;  
//     while (current != NULL) {  
//         if (strcmp(current->audioInfo.FileName, songToFind.FileName) == 0) 
//         {  
//             current->audioInfo.BitRate  = songToFind.BitRate;
//             current->audioInfo.FileSize  = songToFind.FileSize;
//             current->audioInfo.FileType  = songToFind.FileType;
//             current->audioInfo.PlayTime  = songToFind.PlayTime;
//             current->audioInfo.Time  = songToFind.Time;
//             return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
//         }  
//         current = current->next;  
//     }  
//     // 遍历完整个链表后仍未找到,返回0表示歌曲不在链表中  
//     return JZ_ERROR_SYSTEM_MODULE_CODE_FAILURE;  
// }

// 打印当前歌曲信息  
void printAudioInfo(struct AudioMange_Node *current) {  
    if (current) {  
        JZSDK_LOG_INFO("File Name: %s File Size: %d BitRate: %d FileType: %d PlayTime: %d Time: %d", 
        current->audioInfo.FileName, current->audioInfo.FileSize, current->audioInfo.BitRate, current->audioInfo.FileType, current->audioInfo.PlayTime, current->audioInfo.Time);  
    } else {  
        printf("No audio found.\n");  
    }  
}  

//打印歌曲列表
T_JZsdkReturnCode JZsdk_PrintfAuidoNode_AudioList(struct AudioMange_Node *head)
{
    if (head == NULL)  
    {  
        return JZ_ERROR_SYSTEM_MODULE_CODE_INVALID_PARAMETER;  
    }  
  
    struct AudioMange_Node *current = head;  
    do  
    {  
        printAudioInfo(current);  
        current = current->next;  
    } while (current != head); // 遍历直到回到头节点  
  
    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;  
}

// 释放链表内存  
T_JZsdkReturnCode Jzsdk_Free_AndioMange_Node(struct AudioMange_Node **head) 
{  
    struct AudioMange_Node *current = *head;  
    struct AudioMange_Node *temp;  

    do {  
        temp = current;  
        current = current->next; // 移动到下一个节点  
        free(temp); // 释放当前节点  
    } while (current != *head); // 当我们再次回到头节点时停止  
    *head = NULL;  

    return JZ_ERROR_SYSTEM_MODULE_CODE_SUCCESS;
}