Path: blob/master/editor/audio/audio_stream_preview.h
9896 views
/**************************************************************************/1/* audio_stream_preview.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/os/thread.h"33#include "core/templates/safe_refcount.h"34#include "scene/main/node.h"35#include "servers/audio/audio_stream.h"3637class AudioStreamPreview : public RefCounted {38GDCLASS(AudioStreamPreview, RefCounted);39friend class AudioStream;40Vector<uint8_t> preview;41float length;4243friend class AudioStreamPreviewGenerator;44uint64_t version = 1;4546public:47uint64_t get_version() const { return version; }48float get_length() const;49float get_max(float p_time, float p_time_next) const;50float get_min(float p_time, float p_time_next) const;5152AudioStreamPreview();53};5455class AudioStreamPreviewGenerator : public Node {56GDCLASS(AudioStreamPreviewGenerator, Node);5758static AudioStreamPreviewGenerator *singleton;5960struct Preview {61Ref<AudioStreamPreview> preview;62Ref<AudioStream> base_stream;63Ref<AudioStreamPlayback> playback;64SafeFlag generating;65ObjectID id;66Thread *thread = nullptr;6768// Needed for the bookkeeping of the Map69void operator=(const Preview &p_rhs) {70preview = p_rhs.preview;71base_stream = p_rhs.base_stream;72playback = p_rhs.playback;73generating.set_to(generating.is_set());74id = p_rhs.id;75thread = p_rhs.thread;76}77Preview(const Preview &p_rhs) {78preview = p_rhs.preview;79base_stream = p_rhs.base_stream;80playback = p_rhs.playback;81generating.set_to(generating.is_set());82id = p_rhs.id;83thread = p_rhs.thread;84}85Preview() {}86};8788HashMap<ObjectID, Preview> previews;8990static void _preview_thread(void *p_preview);9192void _update_emit(ObjectID p_id);9394protected:95void _notification(int p_what);96static void _bind_methods();9798public:99static AudioStreamPreviewGenerator *get_singleton() { return singleton; }100101Ref<AudioStreamPreview> generate_preview(const Ref<AudioStream> &p_stream);102103AudioStreamPreviewGenerator();104};105106107