33import os
44import requests
55from .configs import options , cors , approot , cache_root , media_root , apiurl , LINKS
6- from iiif_prezi3 import Manifest , config , Annotation , AnnotationPage , AnnotationPageRef , Canvas , Manifest , ResourceItem , ServiceItem , Choice , Collection , ManifestRef , CollectionRef , ResourceItem1 , CanvasRef
6+ from iiif_prezi3 import Manifest , config , Annotation , AnnotationPage , AnnotationPageRef , Canvas , Manifest , ResourceItem , ServiceItem , Choice , Collection , ManifestRef , CollectionRef , ResourceItem1 , AccompanyingCanvas , CanvasRef
77from urllib .parse import urlparse , parse_qs , quote
88import json
99import math
@@ -477,7 +477,7 @@ def addSeeAlso(manifest, identifier, files):
477477 "Djvu XML" : "OCR Data" ,
478478 "Scandata" : "OCR Data" ,
479479 "Archive BitTorrent" : "Torrent" ,
480- "Metadata" : "Metadata" ,
480+ "Metadata" : "Metadata"
481481 }
482482
483483 for file in files :
@@ -490,6 +490,60 @@ def addSeeAlso(manifest, identifier, files):
490490 "format" : seeAlso ['format' ]
491491 })
492492
493+ def addWaveform (identifier , slugged_id , filename , hard_code_size = True ):
494+ """
495+ Create an IIIF AccompanyingCanvas representing a waveform image.
496+
497+ This function generates an IIIF AccompanyingCanvas containing a waveform image,
498+ associated with an audio file. By default, the image dimensions are hardcoded,
499+ but if `hard_code_size` is False, the image's width and height will be retrieved
500+ dynamically from a IIIF image server.
501+
502+ Parameters:
503+ identifier (str): The archive.org identifier for the resource (e.g. item ID).
504+ slugged_id (str): A slugified version of the identifier used in the canvas ID.
505+ filename (str): The filename of the waveform image (PNG).
506+ hard_code_size (bool): If True, sets the image size to 800x200; if False, fetches dimensions from IIIF image server.
507+
508+ Returns:
509+ AccompanyingCanvas: An IIIF-compliant AccompanyingCanvas object representing the waveform image.
510+ """
511+
512+ # This should be the Wave form
513+ accompanying_canvas = AccompanyingCanvas (
514+ id = f"{ URI_PRIFIX } /{ identifier } /{ slugged_id } /canvas/accompanying" ,
515+ label = { "en" : ["Waveform" ]}
516+ )
517+ if hard_code_size :
518+ width = 800
519+ height = 200
520+ body = ResourceItem (id = f"https://archive.org/download/{ identifier } /{ filename .replace (' ' , '%20' )} " , type = "Image" , width = width , height = height )
521+ body .format = "image/jpeg"
522+ else :
523+ imgId = f"{ identifier } /{ filename } " .replace ('/' ,'%2f' )
524+ imgURL = f"{ IMG_SRV } /3/{ imgId } " .replace (' ' , '%20' )
525+ # Find the width and height from the image server
526+ body = ResourceItem (id = "http://example.com" , type = "Image" )
527+ infoJson = body .set_hwd_from_iiif (imgURL )
528+
529+ service = ServiceItem (id = infoJson ['id' ], profile = infoJson ['profile' ], type = infoJson ['type' ])
530+ body .service = [service ]
531+ body .id = f'{ infoJson ["id" ]} /full/max/0/default.jpg'
532+ body .format = "image/jpeg"
533+
534+ width = infoJson ['width' ]
535+ height = infoJson ['height' ]
536+
537+ annotation = Annotation (id = f"{ accompanying_canvas .id } /anno" , motivation = 'painting' , body = body , target = accompanying_canvas .id )
538+
539+ annotationPage = AnnotationPage (id = f"{ accompanying_canvas .id } /annoPage" )
540+ annotationPage .add_item (annotation )
541+
542+ accompanying_canvas .add_item (annotationPage )
543+ accompanying_canvas .height = height
544+ accompanying_canvas .width = width
545+
546+ return accompanying_canvas
493547
494548def addRendering (manifest , identifier , files ):
495549 manifest .rendering = []
@@ -784,6 +838,18 @@ def create_manifest3(identifier, domain=None, page=None):
784838 label = {"none" : [format ]},
785839 duration = float (file ['length' ]))
786840 body .items .append (r )
841+
842+ if "Spectrogram" in derivatives [file ['name' ]]:
843+ c .seeAlso = [{
844+ "id" : f"https://archive.org/download/{ identifier } /{ normalised_id .replace (' ' , '%20' )} _spectrogram.png" ,
845+ "type" : "Image" ,
846+ "label" : {"en" : ["Spectrogram" ]},
847+ "format" : "image/png"
848+ }]
849+
850+ if "PNG" in derivatives [file ['name' ]]:
851+ # This should be the Wave form
852+ c .accompanyingCanvas = addWaveform (identifier , slugged_id , derivatives [file ['name' ]]["PNG" ]["name" ])
787853 else :
788854 # todo: deal with instances where there are no derivatives for whatever reason
789855 body = ResourceItem (
0 commit comments